Class: Spree::Preferences::PreferenceDifferentiator

Inherits:
Object
  • Object
show all
Defined in:
lib/spree/preferences/preference_differentiator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_class) ⇒ PreferenceDifferentiator

Returns a new instance of PreferenceDifferentiator.



8
9
10
# File 'lib/spree/preferences/preference_differentiator.rb', line 8

def initialize(config_class)
  @config_class = config_class
end

Instance Attribute Details

#config_classObject (readonly)

Returns the value of attribute config_class.



6
7
8
# File 'lib/spree/preferences/preference_differentiator.rb', line 6

def config_class
  @config_class
end

Instance Method Details

#call(from:, to:) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/spree/preferences/preference_differentiator.rb', line 12

def call(from:, to:)
  preferences_from = config_class.new.load_defaults(from)
  preferences_to = config_class.new.load_defaults(to)
  config_class.versioned_preferences.reduce({}) do |changes, pref_key|
    value_from = preferences_from[pref_key]
    value_to = preferences_to[pref_key]
    if value_from == value_to
      changes
    else
      changes.merge(
        pref_key => { from: value_from, to: value_to }
      )
    end
  end
end