From 7ef8714e6eb060cfe7e8c786819c944b673d7fa9 Mon Sep 17 00:00:00 2001 From: Filippo Liverani <1382917+filippoliverani@users.noreply.github.com> Date: Fri, 10 Jan 2020 15:10:25 +0100 Subject: [PATCH 1/3] Make ActiveRecord models inherit from Spree::Base Solidus ActiveRecord models should follow Rails 5+ convention of inheriting from an itermediate class to avoid monkey patching ActiveRecord::Base. --- core/app/models/spree/promotion_code_batch.rb | 2 +- core/app/models/spree/promotion_rule_role.rb | 2 +- core/app/models/spree/shipping_rate_tax.rb | 2 +- core/db/default/spree/countries.rb | 2 +- core/lib/solidus/migrations/promotions_with_code_handlers.rb | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/core/app/models/spree/promotion_code_batch.rb b/core/app/models/spree/promotion_code_batch.rb index 699885164cc..85a17be14be 100644 --- a/core/app/models/spree/promotion_code_batch.rb +++ b/core/app/models/spree/promotion_code_batch.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true module Spree - class PromotionCodeBatch < ActiveRecord::Base + class PromotionCodeBatch < Spree::Base class CantProcessStartedBatch < StandardError end diff --git a/core/app/models/spree/promotion_rule_role.rb b/core/app/models/spree/promotion_rule_role.rb index f6dc3b7a022..086daf32502 100644 --- a/core/app/models/spree/promotion_rule_role.rb +++ b/core/app/models/spree/promotion_rule_role.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true module Spree - class PromotionRuleRole < ActiveRecord::Base + class PromotionRuleRole < Spree::Base belongs_to :promotion_rule, class_name: 'Spree::PromotionRule', optional: true belongs_to :role, class_name: 'Spree::Role', optional: true end diff --git a/core/app/models/spree/shipping_rate_tax.rb b/core/app/models/spree/shipping_rate_tax.rb index c3528e4080c..d581b5565db 100644 --- a/core/app/models/spree/shipping_rate_tax.rb +++ b/core/app/models/spree/shipping_rate_tax.rb @@ -6,7 +6,7 @@ module Spree # @attr [Spree::TaxRate] tax_rate The tax rate used to calculate the tax amount # @since 1.3.0 # @see Spree::Tax::ShippingRateTaxer - class ShippingRateTax < ActiveRecord::Base + class ShippingRateTax < Spree::Base belongs_to :shipping_rate, class_name: "Spree::ShippingRate", optional: true belongs_to :tax_rate, class_name: "Spree::TaxRate", optional: true diff --git a/core/db/default/spree/countries.rb b/core/db/default/spree/countries.rb index d374bf3b822..df883a8d3a7 100644 --- a/core/db/default/spree/countries.rb +++ b/core/db/default/spree/countries.rb @@ -5,7 +5,7 @@ # Insert Countries into the spree_countries table, checking to ensure that no # duplicates are created, using as few SQL statements as possible (2) -connection = ApplicationRecord.connection +connection = Spree::Base.connection country_mapper = ->(country) do name = connection.quote country.name diff --git a/core/lib/solidus/migrations/promotions_with_code_handlers.rb b/core/lib/solidus/migrations/promotions_with_code_handlers.rb index e68ca1f55af..c89030e81c9 100644 --- a/core/lib/solidus/migrations/promotions_with_code_handlers.rb +++ b/core/lib/solidus/migrations/promotions_with_code_handlers.rb @@ -3,7 +3,7 @@ module Solidus module Migrations module PromotionWithCodeHandlers - class PromotionCode < ActiveRecord::Base + class PromotionCode < Spree::Base self.table_name = "spree_promotion_codes" end From d5f497bf5faea6cfe76a175cdc3acac151365b43 Mon Sep 17 00:00:00 2001 From: Filippo Liverani <1382917+filippoliverani@users.noreply.github.com> Date: Fri, 10 Jan 2020 16:30:18 +0100 Subject: [PATCH 2/3] Move Permalinks include to Spree::Base Monkey patching directly ActiveRecord::Base is dangerous and discouraged. --- core/app/models/spree/base.rb | 1 + core/lib/spree/core/permalinks.rb | 2 -- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/core/app/models/spree/base.rb b/core/app/models/spree/base.rb index fc9e0922f3f..95916b12d5c 100644 --- a/core/app/models/spree/base.rb +++ b/core/app/models/spree/base.rb @@ -2,6 +2,7 @@ class Spree::Base < ActiveRecord::Base include Spree::Preferences::Preferable + include Spree::Core::Permalinks serialize :preferences, Hash include Spree::RansackableAttributes diff --git a/core/lib/spree/core/permalinks.rb b/core/lib/spree/core/permalinks.rb index a0e39bde427..8e1df41ccc9 100644 --- a/core/lib/spree/core/permalinks.rb +++ b/core/lib/spree/core/permalinks.rb @@ -63,5 +63,3 @@ def save_permalink(permalink_value = to_param) end end end - -ActiveRecord::Base.send :include, Spree::Core::Permalinks From 8b5f2c20ee70293278e412317926c1c090026db2 Mon Sep 17 00:00:00 2001 From: Filippo Liverani <1382917+filippoliverani@users.noreply.github.com> Date: Fri, 10 Jan 2020 16:43:30 +0100 Subject: [PATCH 3/3] Use ActiveSupport::Concern syntax in Permalinks --- core/lib/spree/core/permalinks.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/lib/spree/core/permalinks.rb b/core/lib/spree/core/permalinks.rb index 8e1df41ccc9..8de8981fa5e 100644 --- a/core/lib/spree/core/permalinks.rb +++ b/core/lib/spree/core/permalinks.rb @@ -9,7 +9,7 @@ module Permalinks class_attribute :permalink_options end - module ClassMethods + class_methods do def make_permalink(options = {}) options[:field] ||= :permalink self.permalink_options = options