Sha256: 327841a466d21d22acc2733f61e1194fc0ecc61296b9fbe7a8abdd36fa72f75f

Contents?: true

Size: 1.47 KB

Versions: 29

Compression:

Stored size: 1.47 KB

Contents

# Most of this code is from the preferences plugin available at
# http://github.com/pluginaweek/preferences/tree/master
#
module Spree
  # Adds support for defining preferences on ActiveRecord models.
  module Preferences
    # Represents the definition of a preference for a particular model
    class PreferenceDefinition
      def initialize(name, *args) #:nodoc:
        options = args.extract_options!
        options.assert_valid_keys(:default)

        @type = args.first ? args.first.to_s : 'boolean'

        # Create a column that will be responsible for typecasting
        @column = ActiveRecord::ConnectionAdapters::Column.new(name.to_s, options[:default], @type == 'any' ? nil : @type)
      end

      # The attribute which is being preferenced
      def name
        @column.name
      end

      # The default value to use for the preference in case none have been
      # previously defined
      def default_value
        @column.default
      end

      # Typecasts the value based on the type of preference that was defined
      def type_cast(value)
        if @type == 'any'
          value
        else
          @column.type_cast(value)
        end
      end

      # Typecasts the value to true/false depending on the type of preference
      def query(value)
        unless value = type_cast(value)
          false
        else
          if @column.number?
            !value.zero?
          else
            !value.blank?
          end
        end
      end
    end
  end
end

Version data entries

29 entries across 29 versions & 6 rubygems

Version Path
spree_core-0.70.7 lib/spree_core/preferences/preference_definition.rb
spree_core-0.70.6 lib/spree_core/preferences/preference_definition.rb
apispree_core-0.0.0 lib/spree_core/preferences/preference_definition.rb
My-Commerce_core-1.1.0 lib/spree_core/preferences/preference_definition.rb
My-Commerce_core-1.0.0 lib/spree_core/preferences/preference_definition.rb
MyCommerceapi-1.0.0 core/lib/spree_core/preferences/preference_definition.rb
MyCommerce-0.0.3 core/lib/spree_core/preferences/preference_definition.rb
rfcommerce_core-0.0.3 lib/spree_core/preferences/preference_definition.rb
spree_core-0.60.6 lib/spree_core/preferences/preference_definition.rb
spree_core-0.70.5 lib/spree_core/preferences/preference_definition.rb
spree_core-0.70.4 lib/spree_core/preferences/preference_definition.rb
spree_core-0.60.5 lib/spree_core/preferences/preference_definition.rb
spree_core-0.70.3 lib/spree_core/preferences/preference_definition.rb
spree_core-0.70.2 lib/spree_core/preferences/preference_definition.rb
spree_core-0.50.4 lib/spree_core/preferences/preference_definition.rb
spree_core-0.60.4 lib/spree_core/preferences/preference_definition.rb
spree_core-0.50.3 lib/spree_core/preferences/preference_definition.rb
spree_core-0.60.3 lib/spree_core/preferences/preference_definition.rb
spree_core-0.70.1 lib/spree_core/preferences/preference_definition.rb
spree_core-0.70.0 lib/spree_core/preferences/preference_definition.rb