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

Allow access to Spree::Core::Environment through Spree::Config #2291

Merged
merged 4 commits into from
Nov 3, 2017
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
2 changes: 1 addition & 1 deletion core/app/models/concerns/spree/calculated_adjustments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def model_name_without_spree_namespace
end

def spree_calculators
Rails.application.config.spree.calculators
Spree::Config.environment.calculators
end
end

Expand Down
2 changes: 1 addition & 1 deletion core/app/models/spree/calculator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def self.description
def self.calculators
Spree::Deprecation.warn("Calling .calculators is deprecated. Please access through Rails.application.config.spree.calculators")

Rails.application.config.spree.calculators
Spree::Config.environment.calculators
end

def to_s
Expand Down
2 changes: 1 addition & 1 deletion core/app/models/spree/payment_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class << self
def providers
Spree::Deprecation.warn 'Spree::PaymentMethod.providers is deprecated and will be deleted in Solidus 3.0. ' \
'Please use Rails.application.config.spree.payment_methods instead'
Rails.application.config.spree.payment_methods
Spree::Config.environment.payment_methods
end

def available(display_on = nil, store: nil)
Expand Down
2 changes: 1 addition & 1 deletion core/app/models/spree/promotion_action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class PromotionAction < Spree::Base
belongs_to :promotion, class_name: 'Spree::Promotion', inverse_of: :promotion_actions

scope :of_type, ->(t) { where(type: Array.wrap(t).map(&:to_s)) }
scope :shipping, -> { of_type(Rails.application.config.spree.promotions.shipping_actions.to_a) }
scope :shipping, -> { of_type(Spree::Config.environment.promotions.shipping_actions.to_a) }

# Updates the state of the order or performs some other action depending on
# the subclass options will contain the payload from the event that
Expand Down
4 changes: 2 additions & 2 deletions core/app/models/spree/stock/simple_coordinator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module Stock
# * Combine allocated and on hand inventory into a single shipment per-location
#
# After allocation, splitters are run on each Package (as configured in
# Rails.application.config.spree.stock_splitters)
# Spree::Config.environment.stock_splitters)
#
# Finally, shipping rates are calculated using the class configured as
# Spree::Config.stock.estimator_class.
Expand All @@ -20,7 +20,7 @@ class SimpleCoordinator
def initialize(order, inventory_units = nil)
@order = order
@inventory_units = inventory_units || InventoryUnitBuilder.new(order).units
@splitters = Rails.application.config.spree.stock_splitters
@splitters = Spree::Config.environment.stock_splitters
@stock_locations = Spree::StockLocation.active

@inventory_units_by_variant = @inventory_units.group_by(&:variant)
Expand Down
77 changes: 77 additions & 0 deletions core/lib/spree/app_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
require "spree/core/search/base"
require "spree/core/search/variant"
require 'spree/preferences/configuration'
require 'spree/core/environment'

module Spree
class AppConfiguration < Preferences::Configuration
Expand Down Expand Up @@ -381,6 +382,82 @@ def stock
@stock_configuration ||= Spree::Core::StockConfiguration.new
end

def environment
@environment ||= Spree::Core::Environment.new(self).tap do |env|
env.calculators.shipping_methods = %w[
Spree::Calculator::Shipping::FlatPercentItemTotal
Spree::Calculator::Shipping::FlatRate
Spree::Calculator::Shipping::FlexiRate
Spree::Calculator::Shipping::PerItem
Spree::Calculator::Shipping::PriceSack
]

env.calculators.tax_rates = %w[
Spree::Calculator::DefaultTax
]

env.stock_splitters = %w[
Spree::Stock::Splitter::ShippingCategory
Spree::Stock::Splitter::Backordered
]

env.payment_methods = %w[
Spree::PaymentMethod::BogusCreditCard
Spree::PaymentMethod::SimpleBogusCreditCard
Spree::PaymentMethod::StoreCredit
Spree::PaymentMethod::Check
]

env.promotions = Spree::Promo::Environment.new.tap do |promos|
promos.rules = %w[
Spree::Promotion::Rules::ItemTotal
Spree::Promotion::Rules::Product
Spree::Promotion::Rules::User
Spree::Promotion::Rules::FirstOrder
Spree::Promotion::Rules::UserLoggedIn
Spree::Promotion::Rules::OneUsePerUser
Spree::Promotion::Rules::Taxon
Spree::Promotion::Rules::NthOrder
Spree::Promotion::Rules::OptionValue
Spree::Promotion::Rules::FirstRepeatPurchaseSince
Spree::Promotion::Rules::UserRole
]

promos.actions = %w[
Spree::Promotion::Actions::CreateAdjustment
Spree::Promotion::Actions::CreateItemAdjustments
Spree::Promotion::Actions::CreateQuantityAdjustments
Spree::Promotion::Actions::FreeShipping
]

promos.shipping_actions = %w[
Spree::Promotion::Actions::FreeShipping
]
end

env.calculators.promotion_actions_create_adjustments = %w[
Spree::Calculator::FlatPercentItemTotal
Spree::Calculator::FlatRate
Spree::Calculator::FlexiRate
Spree::Calculator::TieredPercent
Spree::Calculator::TieredFlatRate
]

env.calculators.promotion_actions_create_item_adjustments = %w[
Spree::Calculator::DistributedAmount
Spree::Calculator::FlatRate
Spree::Calculator::FlexiRate
Spree::Calculator::PercentOnLineItem
Spree::Calculator::TieredPercent
]

env.calculators.promotion_actions_create_quantity_adjustments = %w[
Spree::Calculator::PercentOnLineItem
Spree::Calculator::FlatRate
]
end
end

# Default admin VAT location
#
# An object that responds to :state_id and :country_id so it can double as a Spree::Address in
Expand Down
103 changes: 12 additions & 91 deletions core/lib/spree/core/engine.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'spree/config'

module Spree
module Core
class Engine < ::Rails::Engine
Expand All @@ -9,7 +11,7 @@ class Engine < ::Rails::Engine
end

initializer "spree.environment", before: :load_config_initializers do |app|
app.config.spree = Spree::Core::Environment.new
app.config.spree = Spree::Config.environment
end

initializer "spree.default_permissions", before: :load_config_initializers do |_app|
Expand All @@ -19,96 +21,15 @@ class Engine < ::Rails::Engine
end
end

initializer "spree.register.calculators", before: :load_config_initializers do |app|
app.config.spree.calculators.shipping_methods = %w[
Spree::Calculator::Shipping::FlatPercentItemTotal
Spree::Calculator::Shipping::FlatRate
Spree::Calculator::Shipping::FlexiRate
Spree::Calculator::Shipping::PerItem
Spree::Calculator::Shipping::PriceSack
]

app.config.spree.calculators.tax_rates = %w[
Spree::Calculator::DefaultTax
]
end

initializer "spree.register.stock_splitters", before: :load_config_initializers do |app|
app.config.spree.stock_splitters = %w[
Spree::Stock::Splitter::ShippingCategory
Spree::Stock::Splitter::Backordered
]
end

initializer "spree.register.payment_methods", before: :load_config_initializers do |app|
app.config.spree.payment_methods = %w[
Spree::PaymentMethod::BogusCreditCard
Spree::PaymentMethod::SimpleBogusCreditCard
Spree::PaymentMethod::StoreCredit
Spree::PaymentMethod::Check
]
end

# We need to define promotions rules here so extensions and existing apps
# can add their custom classes on their initializer files
initializer 'spree.promo.environment', before: :load_config_initializers do |app|
app.config.spree.promotions = Spree::Promo::Environment.new
app.config.spree.promotions.rules = []
end

initializer 'spree.promo.register.promotion.calculators', before: :load_config_initializers do |app|
app.config.spree.calculators.promotion_actions_create_adjustments = %w[
Spree::Calculator::FlatPercentItemTotal
Spree::Calculator::FlatRate
Spree::Calculator::FlexiRate
Spree::Calculator::TieredPercent
Spree::Calculator::TieredFlatRate
]

app.config.spree.calculators.promotion_actions_create_item_adjustments = %w[
Spree::Calculator::DistributedAmount
Spree::Calculator::FlatRate
Spree::Calculator::FlexiRate
Spree::Calculator::PercentOnLineItem
Spree::Calculator::TieredPercent
]

app.config.spree.calculators.promotion_actions_create_quantity_adjustments = %w[
Spree::Calculator::PercentOnLineItem
Spree::Calculator::FlatRate
]
end

initializer 'spree.promo.register.promotion.rules', before: :load_config_initializers do |app|
app.config.spree.promotions.rules = %w[
Spree::Promotion::Rules::ItemTotal
Spree::Promotion::Rules::Product
Spree::Promotion::Rules::User
Spree::Promotion::Rules::FirstOrder
Spree::Promotion::Rules::UserLoggedIn
Spree::Promotion::Rules::OneUsePerUser
Spree::Promotion::Rules::Taxon
Spree::Promotion::Rules::NthOrder
Spree::Promotion::Rules::OptionValue
Spree::Promotion::Rules::FirstRepeatPurchaseSince
Spree::Promotion::Rules::UserRole
]
end

initializer 'spree.promo.register.promotions.actions', before: :load_config_initializers do |app|
app.config.spree.promotions.actions = %w[
Spree::Promotion::Actions::CreateAdjustment
Spree::Promotion::Actions::CreateItemAdjustments
Spree::Promotion::Actions::CreateQuantityAdjustments
Spree::Promotion::Actions::FreeShipping
]
end

initializer 'spree.promo.register.promotions.shipping_actions', before: :load_config_initializers do |app|
app.config.spree.promotions.shipping_actions = %w[
Spree::Promotion::Actions::FreeShipping
]
end
# leave empty initializers for backwards-compatability. Other apps might still rely on these events
initializer "spree.register.calculators", before: :load_config_initializers do; end
initializer "spree.register.stock_splitters", before: :load_config_initializers do; end
initializer "spree.register.payment_methods", before: :load_config_initializers do; end
initializer 'spree.promo.environment', before: :load_config_initializers do; end
initializer 'spree.promo.register.promotion.calculators', before: :load_config_initializers do; end
initializer 'spree.promo.register.promotion.rules', before: :load_config_initializers do; end
initializer 'spree.promo.register.promotions.actions', before: :load_config_initializers do; end
initializer 'spree.promo.register.promotions.shipping_actions', before: :load_config_initializers do; end

# Filter sensitive information during logging
initializer "spree.params.filter", before: :load_config_initializers do |app|
Expand Down
6 changes: 3 additions & 3 deletions core/lib/spree/core/environment.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'spree/config'
require 'spree/core/environment_extension'

module Spree
module Core
Expand All @@ -10,9 +10,9 @@ class Environment

attr_accessor :calculators, :preferences, :promotions

def initialize
def initialize(spree_config)
@calculators = Calculators.new
@preferences = Spree::Config
@preferences = spree_config
@promotions = Spree::Promo::Environment.new
end
end
Expand Down
1 change: 1 addition & 0 deletions core/lib/spree/testing_support/preferences.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module Preferences
def reset_spree_preferences(&config_block)
Spree::Config.instance_variables.each { |iv| Spree::Config.remove_instance_variable(iv) }
Spree::Config.preference_store = Spree::Config.default_preferences
Rails.application.config.spree = Spree::Config.environment

configure_spree_preferences(&config_block) if block_given?
end
Expand Down
2 changes: 1 addition & 1 deletion core/spec/models/spree/app_configuration_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'rails_helper'

RSpec.describe Spree::AppConfiguration, type: :model do
let(:prefs) { Rails.application.config.spree.preferences }
let(:prefs) { Spree::Config }

it "should be available from the environment" do
prefs.layout = "my/layout"
Expand Down
7 changes: 1 addition & 6 deletions core/spec/models/spree/promotion_handler/shipping_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,11 @@ module PromotionHandler
before do
stub_const('CustomShippingAction', custom_klass)

@old_shipping_actions = Rails.application.config.spree.promotions.shipping_actions
Rails.application.config.spree.promotions.shipping_actions = ['CustomShippingAction']
Spree::Config.environment.promotions.shipping_actions = ['CustomShippingAction']

order.order_promotions.create!(promotion: promotion, promotion_code: promotion.codes.first)
end

after do
Rails.application.config.spree.promotions.shipping_actions = @old_shipping_actions
end

let(:action) { custom_klass.new }
let(:custom_klass) { Class.new(Spree::Promotion::Actions::FreeShipping) }
let(:promotion) { create(:promotion, code: 'customshipping', promotion_actions: [action]) }
Expand Down