Module: Spree::Preferences::PreferableClassMethods
- Defined in:
- lib/spree/preferences/preferable_class_methods.rb
Constant Summary collapse
- DEFAULT_ADMIN_FORM_PREFERENCE_TYPES =
%i( boolean decimal integer password string text )
Instance Method Summary collapse
-
#allowed_admin_form_preference_types ⇒ Array
List of preference types allowed as form fields in the Solidus admin.
- #defined_preferences ⇒ Object
- #preference(name, type, options = {}) ⇒ Object
- #preference_default_getter_method(name) ⇒ Object
- #preference_getter_method(name) ⇒ Object
- #preference_setter_method(name) ⇒ Object
- #preference_type_getter_method(name) ⇒ Object
Instance Method Details
#allowed_admin_form_preference_types ⇒ Array
List of preference types allowed as form fields in the Solidus admin
Overwrite this method in your class that includes Spree::Preferable
if you want to provide more fields. If you do so, you also need to provide a preference field partial that lives in:
app/views/spree/admin/shared/preference_fields/
84 85 86 |
# File 'lib/spree/preferences/preferable_class_methods.rb', line 84 def allowed_admin_form_preference_types DEFAULT_ADMIN_FORM_PREFERENCE_TYPES end |
#defined_preferences ⇒ Object
14 15 16 |
# File 'lib/spree/preferences/preferable_class_methods.rb', line 14 def defined_preferences [] end |
#preference(name, type, options = {}) ⇒ Object
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 |
# File 'lib/spree/preferences/preferable_class_methods.rb', line 18 def preference(name, type, = {}) .assert_valid_keys(:default) default = [:default] default = ->{ [:default] } unless default.is_a?(Proc) # The defined preferences on a class are all those defined directly on # that class as well as those defined on ancestors. # We store these as a class instance variable on each class which has a # preference. super() collects preferences defined on ancestors. singleton_preferences = (@defined_singleton_preferences ||= []) singleton_preferences << name.to_sym define_singleton_method :defined_preferences do super() + singleton_preferences end # 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| value = convert_preference_value(value, type) preferences[name] = value # 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 end |
#preference_default_getter_method(name) ⇒ Object
67 68 69 |
# File 'lib/spree/preferences/preferable_class_methods.rb', line 67 def preference_default_getter_method(name) "preferred_#{name}_default".to_sym end |
#preference_getter_method(name) ⇒ Object
59 60 61 |
# File 'lib/spree/preferences/preferable_class_methods.rb', line 59 def preference_getter_method(name) "preferred_#{name}".to_sym end |
#preference_setter_method(name) ⇒ Object
63 64 65 |
# File 'lib/spree/preferences/preferable_class_methods.rb', line 63 def preference_setter_method(name) "preferred_#{name}=".to_sym end |
#preference_type_getter_method(name) ⇒ Object
71 72 73 |
# File 'lib/spree/preferences/preferable_class_methods.rb', line 71 def preference_type_getter_method(name) "preferred_#{name}_type".to_sym end |