Sha256: 70708e2556ea0594914a33c9f831ef1e5b2ab7de47491c3af670c5d2dfbe88d8

Contents?: true

Size: 1.96 KB

Versions: 37

Compression:

Stored size: 1.96 KB

Contents

module Spree::Preferences
  module PreferableClassMethods
    def defined_preferences
      []
    end

    def preference(name, type, options = {})
      options.assert_valid_keys(:default)
      default = options[:default]
      default = ->{ options[: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

    def preference_getter_method(name)
      "preferred_#{name}".to_sym
    end

    def preference_setter_method(name)
       "preferred_#{name}=".to_sym
    end

    def preference_default_getter_method(name)
      "preferred_#{name}_default".to_sym
    end

    def preference_type_getter_method(name)
      "preferred_#{name}_type".to_sym
    end
  end
end

Version data entries

37 entries across 37 versions & 1 rubygems

Version Path
solidus_core-2.4.2 lib/spree/preferences/preferable_class_methods.rb
solidus_core-2.3.1 app/models/spree/preferences/preferable_class_methods.rb
solidus_core-2.4.1 lib/spree/preferences/preferable_class_methods.rb
solidus_core-2.2.2 app/models/spree/preferences/preferable_class_methods.rb
solidus_core-2.1.1 app/models/spree/preferences/preferable_class_methods.rb
solidus_core-2.0.3 app/models/spree/preferences/preferable_class_methods.rb
solidus_core-1.4.2 app/models/spree/preferences/preferable_class_methods.rb
solidus_core-1.3.2 app/models/spree/preferences/preferable_class_methods.rb
solidus_core-2.4.0 lib/spree/preferences/preferable_class_methods.rb
solidus_core-2.4.0.rc1 lib/spree/preferences/preferable_class_methods.rb
solidus_core-2.4.0.beta1 lib/spree/preferences/preferable_class_methods.rb
solidus_core-2.3.0 app/models/spree/preferences/preferable_class_methods.rb
solidus_core-2.3.0.rc3 app/models/spree/preferences/preferable_class_methods.rb
solidus_core-2.3.0.rc2 app/models/spree/preferences/preferable_class_methods.rb
solidus_core-2.3.0.rc1 app/models/spree/preferences/preferable_class_methods.rb
solidus_core-2.3.0.beta1 app/models/spree/preferences/preferable_class_methods.rb
solidus_core-2.0.2 app/models/spree/preferences/preferable_class_methods.rb
solidus_core-1.4.1 app/models/spree/preferences/preferable_class_methods.rb
solidus_core-2.2.1 app/models/spree/preferences/preferable_class_methods.rb
solidus_core-2.2.0 app/models/spree/preferences/preferable_class_methods.rb