Skip to content

Commit

Permalink
Extract methods related to persisted preferences to module
Browse files Browse the repository at this point in the history
This removes even more code from Spree::Base, and groups the few lines
of code that are needed for persisted preferences in one module.
  • Loading branch information
mamhoff committed Mar 29, 2021
1 parent 6c32fad commit 2313219
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 19 deletions.
14 changes: 4 additions & 10 deletions core/app/models/spree/base.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require 'spree/preferences/persistable'

class Spree::Base < ActiveRecord::Base
include Spree::Preferences::Preferable
include Spree::Core::Permalinks
Expand All @@ -14,26 +16,18 @@ def preferences
following to lines to your class:
```
class #{self.class.name}
serialize :preferences, Hash
after_initialize :initialize_preference_defaults
include Spree::Preferences::Persistable
...
end
```
WARN
self.class.serialize :preferences, Hash
self.class.after_initialize :initialize_preference_defaults
self.class.include Spree::Preferences::Persistable
value ? YAML.safe_load(value, [Symbol]) : {}
else
value
end
end

def initialize_preference_defaults
if has_attribute?(:preferences)
self.preferences = default_preferences.merge(preferences)
end
end

if Kaminari.config.page_method_name != :page
def self.page(num)
Spree::Deprecation.warn \
Expand Down
5 changes: 3 additions & 2 deletions core/app/models/spree/calculator.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# frozen_string_literal: true

require 'spree/preferences/persistable'

module Spree
class Calculator < Spree::Base
serialize :preferences, Hash
after_initialize :initialize_preference_defaults
include Spree::Preferences::Persistable

belongs_to :calculable, polymorphic: true, optional: true

Expand Down
4 changes: 2 additions & 2 deletions core/app/models/spree/payment_method.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require 'spree/preferences/persistable'
require 'spree/preferences/statically_configurable'

module Spree
Expand All @@ -11,8 +12,7 @@ module Spree
# This class is not meant to be instantiated. Please create instances of concrete payment methods.
#
class PaymentMethod < Spree::Base
serialize :preferences, Hash
after_initialize :initialize_preference_defaults
include Spree::Preferences::Persistable

preference :server, :string, default: 'test'
preference :test_mode, :boolean, default: true
Expand Down
6 changes: 3 additions & 3 deletions core/app/models/spree/promotion_action.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# frozen_string_literal: true

require 'spree/preferences/persistable'

module Spree
# Base class for all types of promotion action.
#
# PromotionActions perform the necessary tasks when a promotion is activated
# by an event and determined to be eligible.
class PromotionAction < Spree::Base
serialize :preferences, Hash
after_initialize :initialize_preference_defaults

include Spree::Preferences::Persistable
include Spree::SoftDeletable

belongs_to :promotion, class_name: 'Spree::Promotion', inverse_of: :promotion_actions, optional: true
Expand Down
5 changes: 3 additions & 2 deletions core/app/models/spree/promotion_rule.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# frozen_string_literal: true

require 'spree/preferences/persistable'

module Spree
# Base class for all promotion rules
class PromotionRule < Spree::Base
serialize :preferences, Hash
after_initialize :initialize_preference_defaults
include Spree::Preferences::Persistable

belongs_to :promotion, class_name: 'Spree::Promotion', inverse_of: :promotion_rules, optional: true

Expand Down
20 changes: 20 additions & 0 deletions core/lib/spree/preferences/persistable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

module Spree
module Preferences
module Persistable
def self.included(klass)
klass.serialize :preferences, Hash
klass.after_initialize :initialize_preference_defaults
end

private

def initialize_preference_defaults
if has_attribute?(:preferences)
self.preferences = default_preferences.merge(preferences)
end
end
end
end
end

0 comments on commit 2313219

Please sign in to comment.