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

Assign and initialize Spree::Config earlier, before rails initializers #2178

Merged
merged 5 commits into from
Aug 30, 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: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

- Remove Skeleton Grid CSS from the admin and complete its transition to Bootstrap. [\#2127](https://github.com/solidusio/solidus/pull/2127) ([graygilmore](https://github.com/graygilmore))

- Move preferences namespace to /lib. Initializes the Spree::Config object in its own file. [\#2178](https://github.com/solidusio/solidus/pull/2178) ([cbrunsdon](https://github.com/cbrunsdon))

## Solidus 2.3.0 (unreleased)

- Rails 5.1 [\#1895](https://github.com/solidusio/solidus/pull/1895) ([jhawthorn](https://github.com/jhawthorn))
Expand Down
2 changes: 2 additions & 0 deletions core/app/models/spree/payment_method.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'spree/preferences/statically_configurable'

module Spree
# A base class which is used for implementing payment methods.
#
Expand Down
133 changes: 0 additions & 133 deletions core/app/models/spree/preferences/preferable.rb

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#
require "spree/core/search/base"
require "spree/core/search/variant"
require 'spree/preferences/configuration'

module Spree
class AppConfiguration < Preferences::Configuration
Expand Down
3 changes: 3 additions & 0 deletions core/lib/spree/config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require 'spree/app_configuration'

Spree::Config = Spree::AppConfiguration.new
4 changes: 4 additions & 0 deletions core/lib/spree/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,7 @@ class GatewayError < RuntimeError; end
require 'spree/permission_sets'

require 'spree/core/price_migrator'

require 'spree/preferences/store'
require 'spree/preferences/static_model_preferences'
require 'spree/preferences/scoped_store'
1 change: 0 additions & 1 deletion core/lib/spree/core/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ class Engine < ::Rails::Engine

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

initializer "spree.default_permissions", before: :load_config_initializers do |_app|
Expand Down
4 changes: 3 additions & 1 deletion core/lib/spree/core/environment.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'spree/config'

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

def initialize
@calculators = Calculators.new
@preferences = Spree::AppConfiguration.new
@preferences = Spree::Config
@promotions = Spree::Promo::Environment.new
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'spree/preferences/preferable'

module Spree::Preferences
# This takes the preferrable methods and adds some
# syntatic sugar to access the preferences
Expand Down
140 changes: 140 additions & 0 deletions core/lib/spree/preferences/preferable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
# Preferable allows defining preference accessor methods.
#
# A class including Preferable must implement #preferences which should return
# an object responding to .fetch(key), []=(key, val), and .delete(key).
#
# The generated writer method performs typecasting before assignment into the
# preferences object.
#
# Examples:
#
# # Spree::Base includes Preferable and defines preferences as a serialized
# # column.
# class Settings < Spree::Base
# preference :color, :string, default: 'red'
# preference :temperature, :integer, default: 21
# end
#
# s = Settings.new
# s.preferred_color # => 'red'
# s.preferred_temperature # => 21
#
# s.preferred_color = 'blue'
# s.preferred_color # => 'blue'
#
# # Typecasting is performed on assignment
# s.preferred_temperature = '24'
# s.preferred_color # => 24
#
# # Modifications have been made to the .preferences hash
# s.preferences #=> {color: 'blue', temperature: 24}
#
# # Save the changes. All handled by activerecord
# s.save!

require 'spree/preferences/preferable_class_methods'

module Spree
module Preferences
module Preferable
extend ActiveSupport::Concern

included do
extend Spree::Preferences::PreferableClassMethods
end

# Get a preference
# @param name [#to_sym] name of preference
# @return [Object] The value of preference +name+
def get_preference(name)
has_preference! name
send self.class.preference_getter_method(name)
end

# Set a preference
# @param name [#to_sym] name of preference
# @param value [Object] new value for preference +name+
def set_preference(name, value)
has_preference! name
send self.class.preference_setter_method(name), value
end

# @param name [#to_sym] name of preference
# @return [Symbol] The type of preference +name+
def preference_type(name)
has_preference! name
send self.class.preference_type_getter_method(name)
end

# @param name [#to_sym] name of preference
# @return [Object] The default for preference +name+
def preference_default(name)
has_preference! name
send self.class.preference_default_getter_method(name)
end

# Raises an exception if the +name+ preference is not defined on this class
# @param name [#to_sym] name of preference
def has_preference!(name)
raise NoMethodError.new "#{name} preference not defined" unless has_preference? name
end

# @param name [#to_sym] name of preference
# @return [Boolean] if preference exists on this class
def has_preference?(name)
defined_preferences.include?(name.to_sym)
end

# @return [Array<Symbol>] All preferences defined on this class
def defined_preferences
self.class.defined_preferences
end

# @return [Hash{Symbol => Object}] Default for all preferences defined on this class
def default_preferences
Hash[
defined_preferences.map do |preference|
[preference, preference_default(preference)]
end
]
end

private

def convert_preference_value(value, type)
return nil if value.nil?
case type
when :string, :text
value.to_s
when :password
value.to_s
when :decimal
begin
value.to_s.to_d
rescue ArgumentError
BigDecimal.new(0)
end
when :integer
value.to_i
when :boolean
if !value ||
value == 0 ||
value =~ /\A(f|false|0)\Z/i ||
(value.respond_to?(:empty?) && value.empty?)
false
else
true
end
when :array
raise TypeError, "Array expected got #{value.inspect}" unless value.is_a?(Array)
value
when :hash
raise TypeError, "Hash expected got #{value.inspect}" unless value.is_a?(Hash)
value
else
value
end
end
end
end
end