diff --git a/app/controllers/api/v0/base_controller.rb b/app/controllers/api/v0/base_controller.rb index 304fd990a43..1f89cb6c685 100644 --- a/app/controllers/api/v0/base_controller.rb +++ b/app/controllers/api/v0/base_controller.rb @@ -41,6 +41,10 @@ def respond_with_conflict(json_hash) private + def spree_current_user + @spree_current_user ||= request.env['warden'].user + end + # Use logged in user (spree_current_user) for API authentication (current_api_user) def authenticate_user return if @current_api_user = spree_current_user diff --git a/app/controllers/metal_decorator.rb b/app/controllers/metal_decorator.rb deleted file mode 100644 index 9e9364fa813..00000000000 --- a/app/controllers/metal_decorator.rb +++ /dev/null @@ -1,8 +0,0 @@ -# frozen_string_literal: true - -# For the API -ActionController::Metal.class_eval do - def spree_current_user - @spree_current_user ||= request.env['warden'].user - end -end diff --git a/app/models/calculator/price_sack.rb b/app/models/calculator/price_sack.rb index 6ee294d6a1a..a36783afe1c 100644 --- a/app/models/calculator/price_sack.rb +++ b/app/models/calculator/price_sack.rb @@ -1,7 +1,5 @@ # frozen_string_literal: false -# For #to_d method on Ruby 1.8 -require 'bigdecimal/util' require 'spree/localized_number' module Calculator diff --git a/app/models/concerns/calculated_adjustments.rb b/app/models/concerns/calculated_adjustments.rb new file mode 100644 index 00000000000..af55dfa5b2f --- /dev/null +++ b/app/models/concerns/calculated_adjustments.rb @@ -0,0 +1,80 @@ +# frozen_string_literal: true + +require "active_support/concern" + +module CalculatedAdjustments + extend ActiveSupport::Concern + + included do + has_one :calculator, as: :calculable, class_name: "Spree::Calculator", dependent: :destroy + accepts_nested_attributes_for :calculator + validates :calculator, presence: true + end + + class_methods do + def calculators + spree_calculators.__send__(model_name_without_spree_namespace) + end + + private + + def model_name_without_spree_namespace + to_s.tableize.gsub('/', '_').sub('spree_', '') + end + + def spree_calculators + Rails.application.config.spree.calculators + end + end + + def calculator_type + calculator.class.to_s if calculator + end + + def calculator_type=(calculator_type) + klass = calculator_type.constantize if calculator_type + self.calculator = klass.new if klass && !calculator.is_a?(klass) + end + + # Creates a new adjustment for the target object + # (which is any class that has_many :adjustments) and sets amount based on the + # calculator as applied to the given calculable (Order, LineItems[], Shipment, etc.) + # By default the adjustment will not be considered mandatory + def create_adjustment(label, adjustable, mandatory = false, state = "closed", tax_category = nil) + amount = compute_amount(adjustable) + return if amount.zero? && !mandatory + + adjustment_attributes = { + amount: amount, + originator: self, + order: order_object_for(adjustable), + label: label, + mandatory: mandatory, + state: state, + tax_category: tax_category + } + + if adjustable.respond_to?(:adjustments) + adjustable.adjustments.create(adjustment_attributes) + else + adjustable.create_adjustment(adjustment_attributes) + end + end + + # Calculate the amount to be used when creating an adjustment + # NOTE: May be overriden by classes where this module is included into. + def compute_amount(calculable) + calculator.compute(calculable) + end + + private + + def order_object_for(target) + # Temporary method for adjustments transition. + if target.is_a? Spree::Order + target + elsif target.respond_to?(:order) + target.order + end + end +end diff --git a/app/models/concerns/payment_method_distributors.rb b/app/models/concerns/payment_method_distributors.rb index d9ac7ccb681..44973efb923 100644 --- a/app/models/concerns/payment_method_distributors.rb +++ b/app/models/concerns/payment_method_distributors.rb @@ -8,12 +8,10 @@ module PaymentMethodDistributors extend ActiveSupport::Concern - def self.included(base) - base.class_eval do - has_and_belongs_to_many :distributors, join_table: 'distributors_payment_methods', - class_name: 'Enterprise', - foreign_key: 'payment_method_id', - association_foreign_key: 'distributor_id' - end + included do + has_and_belongs_to_many :distributors, join_table: 'distributors_payment_methods', + class_name: 'Enterprise', + foreign_key: 'payment_method_id', + association_foreign_key: 'distributor_id' end end diff --git a/app/models/enterprise_fee.rb b/app/models/enterprise_fee.rb index 91b262e279d..9ceed010200 100644 --- a/app/models/enterprise_fee.rb +++ b/app/models/enterprise_fee.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true class EnterpriseFee < ApplicationRecord - include Spree::Core::CalculatedAdjustments + include CalculatedAdjustments acts_as_paranoid diff --git a/app/models/spree/gateway.rb b/app/models/spree/gateway.rb index 6a0320835bd..b28350cf1b0 100644 --- a/app/models/spree/gateway.rb +++ b/app/models/spree/gateway.rb @@ -5,6 +5,7 @@ module Spree class Gateway < PaymentMethod + acts_as_taggable include PaymentMethodDistributors delegate_belongs_to :provider, :authorize, :purchase, :capture, :void, :credit diff --git a/app/models/spree/payment_method.rb b/app/models/spree/payment_method.rb index c055842951f..26eec433a04 100644 --- a/app/models/spree/payment_method.rb +++ b/app/models/spree/payment_method.rb @@ -1,11 +1,10 @@ # frozen_string_literal: true require 'concerns/payment_method_distributors' -require 'spree/core/calculated_adjustments' module Spree class PaymentMethod < ApplicationRecord - include Spree::Core::CalculatedAdjustments + include CalculatedAdjustments include PaymentMethodDistributors acts_as_taggable @@ -94,10 +93,6 @@ def supports?(_source) end def init - unless _reflections.key?(:calculator) - self.class.include Spree::Core::CalculatedAdjustments - end - self.calculator ||= ::Calculator::FlatRate.new(preferred_amount: 0) end diff --git a/app/models/spree/shipping_method.rb b/app/models/spree/shipping_method.rb index 931186f28b9..53d77d60c28 100644 --- a/app/models/spree/shipping_method.rb +++ b/app/models/spree/shipping_method.rb @@ -2,7 +2,7 @@ module Spree class ShippingMethod < ApplicationRecord - include Spree::Core::CalculatedAdjustments + include CalculatedAdjustments DISPLAY = [:both, :front_end, :back_end].freeze acts_as_paranoid diff --git a/app/models/spree/tax_rate.rb b/app/models/spree/tax_rate.rb index f0a7be6e111..0bc308f971c 100644 --- a/app/models/spree/tax_rate.rb +++ b/app/models/spree/tax_rate.rb @@ -15,7 +15,7 @@ def validate(record) module Spree class TaxRate < ApplicationRecord acts_as_paranoid - include Spree::Core::CalculatedAdjustments + include CalculatedAdjustments belongs_to :zone, class_name: "Spree::Zone", inverse_of: :tax_rates belongs_to :tax_category, class_name: "Spree::TaxCategory", inverse_of: :tax_rates diff --git a/config/application.rb b/config/application.rb index c4f609a4a43..175f191f459 100644 --- a/config/application.rb +++ b/config/application.rb @@ -34,14 +34,6 @@ module Openfoodnetwork class Application < Rails::Application - - config.to_prepare do - # Load application's model / class decorators - Dir.glob(File.join(File.dirname(__FILE__), "../app/**/*_decorator*.rb")) do |c| - Rails.configuration.cache_classes ? require(c) : load(c) - end - end - config.after_initialize do # We need this here because the test env file loads before the Spree engine is loaded Spree::Core::Engine.routes.default_url_options[:host] = 'test.host' if Rails.env == 'test' diff --git a/config/initializers/spree.rb b/config/initializers/spree.rb index 321d7a93513..f17d1d83a7f 100644 --- a/config/initializers/spree.rb +++ b/config/initializers/spree.rb @@ -8,15 +8,6 @@ require 'spree/core' -# Due to a bug in ActiveRecord we need to load the tagging code in Gateway which -# should have inherited it from its parent PaymentMethod. -# We have to call it before loading the PaymentMethod decorator because the -# tagging code won't load twice within the inheritance chain. -# https://github.com/openfoodfoundation/openfoodnetwork/issues/3121 -Spree::Gateway.class_eval do - acts_as_taggable -end - Spree.config do |config| config.site_url = ENV['SITE_URL'] if ENV['SITE_URL'] config.site_name = ENV['SITE_NAME'] if ENV['SITE_NAME'] diff --git a/lib/spree/core.rb b/lib/spree/core.rb index 0b766960d1d..d737c6e7da8 100644 --- a/lib/spree/core.rb +++ b/lib/spree/core.rb @@ -47,7 +47,6 @@ def self.config require 'spree/core/delegate_belongs_to' require 'spree/core/permalinks' require 'spree/core/token_resource' -require 'spree/core/calculated_adjustments' require 'spree/core/product_duplicator' require 'spree/core/gateway_error' diff --git a/lib/spree/core/calculated_adjustments.rb b/lib/spree/core/calculated_adjustments.rb deleted file mode 100644 index 3e06a951797..00000000000 --- a/lib/spree/core/calculated_adjustments.rb +++ /dev/null @@ -1,80 +0,0 @@ -# frozen_string_literal: true - -module Spree - module Core - module CalculatedAdjustments - def self.included(klass) - klass.class_eval do - has_one :calculator, class_name: "Spree::Calculator", as: :calculable, dependent: :destroy - accepts_nested_attributes_for :calculator - validates :calculator, presence: true - - def self.calculators - spree_calculators.__send__(model_name_without_spree_namespace) - end - - def calculator_type - calculator.class.to_s if calculator - end - - def calculator_type=(calculator_type) - klass = calculator_type.constantize if calculator_type - self.calculator = klass.new if klass && !calculator.is_a?(klass) - end - - # Creates a new adjustment for the target object - # (which is any class that has_many :adjustments) and sets amount based on the - # calculator as applied to the given calculable (Order, LineItems[], Shipment, etc.) - # By default the adjustment will not be considered mandatory - def create_adjustment(label, adjustable, mandatory = false, state = "closed", tax_category = nil) - amount = compute_amount(adjustable) - return if amount.zero? && !mandatory - - adjustment_attributes = { - amount: amount, - originator: self, - order: order_object_for(adjustable), - label: label, - mandatory: mandatory, - state: state, - tax_category: tax_category - } - - if adjustable.respond_to?(:adjustments) - adjustable.adjustments.create(adjustment_attributes) - else - adjustable.create_adjustment(adjustment_attributes) - end - end - - # Calculate the amount to be used when creating an adjustment - # NOTE: May be overriden by classes where this module is included into. - def compute_amount(calculable) - calculator.compute(calculable) - end - - def self.model_name_without_spree_namespace - to_s.tableize.gsub('/', '_').sub('spree_', '') - end - private_class_method :model_name_without_spree_namespace - - def self.spree_calculators - Rails.application.config.spree.calculators - end - private_class_method :spree_calculators - - private - - def order_object_for(target) - # Temporary method for adjustments transition. - if target.is_a? Spree::Order - target - elsif target.respond_to?(:order) - target.order - end - end - end - end - end - end -end diff --git a/spec/lib/spree/core/calculated_adjustments_spec.rb b/spec/models/concerns/calculated_adjustments_spec.rb similarity index 98% rename from spec/lib/spree/core/calculated_adjustments_spec.rb rename to spec/models/concerns/calculated_adjustments_spec.rb index 218ef63c92a..b024c679e40 100644 --- a/spec/lib/spree/core/calculated_adjustments_spec.rb +++ b/spec/models/concerns/calculated_adjustments_spec.rb @@ -5,7 +5,7 @@ # Its pretty difficult to test this module in isolation b/c it needs to work in conjunction # with an actual class that extends ActiveRecord::Base and has a corresponding table in the DB. # So we'll just test it using Order and ShippingMethod. These classes are including the module. -describe Spree::Core::CalculatedAdjustments do +describe CalculatedAdjustments do let(:calculator) { build(:calculator) } let(:tax_rate) { Spree::TaxRate.new(calculator: calculator) }