Module: Spree::Preferences::PreferableClassMethods
- Defined in:
- lib/spree/core/preferences/preferable_class_methods.rb
Instance Method Summary collapse
- #preference(name, type, *args) ⇒ Object
- #preference_change_method(name) ⇒ Object
- #preference_changed_method(name) ⇒ Object
- #preference_default_getter_method(name) ⇒ Object
- #preference_deprecated_getter_method(name) ⇒ Object
- #preference_getter_method(name) ⇒ Object
- #preference_previous_change_method(name) ⇒ Object
- #preference_previous_changed_method(name) ⇒ Object
- #preference_previous_was_method(name) ⇒ Object
- #preference_setter_method(name) ⇒ Object
- #preference_type_getter_method(name) ⇒ Object
- #preference_was_method(name) ⇒ Object
- #prefers_query_method(name) ⇒ Object
Instance Method Details
#preference(name, type, *args) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/spree/core/preferences/preferable_class_methods.rb', line 3 def preference(name, type, *args) = args. .assert_valid_keys(:default, :deprecated, :nullable, :parse_on_set) default = [:default] default = -> { [:default] } unless default.is_a?(Proc) deprecated = [:deprecated] nullable = [:nullable] parse_on_set = [:parse_on_set] # cache_key will be nil for new objects, then if we check if there # is a pending preference before going to default define_method preference_getter_method(name) do preferences.fetch(name) do default.call end end define_method preference_setter_method(name) do |value| if parse_on_set.is_a?(Proc) # Procs that accept more than one arg opt into receiving the # owning record so they can scope (e.g. `normalize_id_preference` # rejecting cross-store IDs). `arity.abs > 1` covers both the # `(value, owner)` and `(value, owner = nil)` shapes. value = parse_on_set.arity.abs > 1 ? parse_on_set.call(value, self) : parse_on_set.call(value) end value = convert_preference_value(value, type, nullable: nullable) preferences[name] = value Spree::Deprecation.warn("`#{name}` is deprecated. #{deprecated}") if deprecated # If this is an activerecord object, we need to inform # ActiveRecord::Dirty that this value has changed, since this is an # in-place update to the preferences hash. preferences_will_change! if respond_to?(:preferences_will_change!) end define_method preference_default_getter_method(name), &default define_method preference_type_getter_method(name) do type end define_method preference_deprecated_getter_method(name) do deprecated end define_method prefers_query_method(name) do preferences.fetch(name).to_b end define_method preference_change_method(name) do preference_change(name, changes) if respond_to?(:changes) end define_method preference_was_method(name) do return unless respond_to?(:changes) preference_change(name, changes)&.first || get_preference(name) end define_method preference_changed_method(name) do respond_to?(:changes) && preference_change(name, changes).present? end define_method preference_previous_change_method(name) do preference_change(name, previous_changes) if respond_to?(:previous_changes) end define_method preference_previous_was_method(name) do return unless respond_to?(:previous_changes) preference_change(name, previous_changes)&.first end define_method preference_previous_changed_method(name) do respond_to?(:previous_changes) && preference_change(name, previous_changes).present? end end |
#preference_change_method(name) ⇒ Object
106 107 108 |
# File 'lib/spree/core/preferences/preferable_class_methods.rb', line 106 def preference_change_method(name) "preferred_#{name}_change".to_sym end |
#preference_changed_method(name) ⇒ Object
114 115 116 |
# File 'lib/spree/core/preferences/preferable_class_methods.rb', line 114 def preference_changed_method(name) "preferred_#{name}_changed?".to_sym end |
#preference_default_getter_method(name) ⇒ Object
90 91 92 |
# File 'lib/spree/core/preferences/preferable_class_methods.rb', line 90 def preference_default_getter_method(name) "preferred_#{name}_default".to_sym end |
#preference_deprecated_getter_method(name) ⇒ Object
94 95 96 |
# File 'lib/spree/core/preferences/preferable_class_methods.rb', line 94 def preference_deprecated_getter_method(name) "preferred_#{name}_deprecated".to_sym end |
#preference_getter_method(name) ⇒ Object
82 83 84 |
# File 'lib/spree/core/preferences/preferable_class_methods.rb', line 82 def preference_getter_method(name) "preferred_#{name}".to_sym end |
#preference_previous_change_method(name) ⇒ Object
118 119 120 |
# File 'lib/spree/core/preferences/preferable_class_methods.rb', line 118 def preference_previous_change_method(name) "preferred_#{name}_previous_change".to_sym end |
#preference_previous_changed_method(name) ⇒ Object
126 127 128 |
# File 'lib/spree/core/preferences/preferable_class_methods.rb', line 126 def preference_previous_changed_method(name) "preferred_#{name}_previously_changed?".to_sym end |
#preference_previous_was_method(name) ⇒ Object
122 123 124 |
# File 'lib/spree/core/preferences/preferable_class_methods.rb', line 122 def preference_previous_was_method(name) "preferred_#{name}_previously_was".to_sym end |
#preference_setter_method(name) ⇒ Object
86 87 88 |
# File 'lib/spree/core/preferences/preferable_class_methods.rb', line 86 def preference_setter_method(name) "preferred_#{name}=".to_sym end |
#preference_type_getter_method(name) ⇒ Object
98 99 100 |
# File 'lib/spree/core/preferences/preferable_class_methods.rb', line 98 def preference_type_getter_method(name) "preferred_#{name}_type".to_sym end |
#preference_was_method(name) ⇒ Object
110 111 112 |
# File 'lib/spree/core/preferences/preferable_class_methods.rb', line 110 def preference_was_method(name) "preferred_#{name}_was".to_sym end |
#prefers_query_method(name) ⇒ Object
102 103 104 |
# File 'lib/spree/core/preferences/preferable_class_methods.rb', line 102 def prefers_query_method(name) "prefers_#{name}?".to_sym end |