Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spree tidy-up #7977

Merged
merged 7 commits into from
Jul 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/controllers/api/v0/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 0 additions & 8 deletions app/controllers/metal_decorator.rb

This file was deleted.

2 changes: 0 additions & 2 deletions app/models/calculator/price_sack.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
80 changes: 80 additions & 0 deletions app/models/concerns/calculated_adjustments.rb
Original file line number Diff line number Diff line change
@@ -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
12 changes: 5 additions & 7 deletions app/models/concerns/payment_method_distributors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion app/models/enterprise_fee.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

class EnterpriseFee < ApplicationRecord
include Spree::Core::CalculatedAdjustments
include CalculatedAdjustments

acts_as_paranoid

Expand Down
1 change: 1 addition & 0 deletions app/models/spree/gateway.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

module Spree
class Gateway < PaymentMethod
acts_as_taggable
include PaymentMethodDistributors

delegate_belongs_to :provider, :authorize, :purchase, :capture, :void, :credit
Expand Down
7 changes: 1 addition & 6 deletions app/models/spree/payment_method.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion app/models/spree/shipping_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/models/spree/tax_rate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 0 additions & 8 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
9 changes: 0 additions & 9 deletions config/initializers/spree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down
1 change: 0 additions & 1 deletion lib/spree/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
80 changes: 0 additions & 80 deletions lib/spree/core/calculated_adjustments.rb

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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) }

Expand Down