Sha256: 9db6d7654ae5fb9ebe948613bc2e97422c2365862b3aed3fb642d9b90a620987

Contents?: true

Size: 1006 Bytes

Versions: 4

Compression:

Stored size: 1006 Bytes

Contents

class Spree::Preference < ActiveRecord::Base
  attr_accessible :key, :value_type, :value

  validates :key, :presence => true
  validates :value_type, :presence => true

  scope :valid, lambda { where(Spree::Preference.arel_table[:key].not_eq(nil)).where(Spree::Preference.arel_table[:value_type].not_eq(nil)) }

  # The type conversions here should match
  # the ones in spree::preferences::preferrable#convert_preference_value
  def value
    if self[:value_type].present?
      case self[:value_type].to_sym
      when :string, :text
        self[:value].to_s
      when :password
        self[:value].to_s
      when :decimal
        BigDecimal.new(self[:value].to_s).round(2, BigDecimal::ROUND_HALF_UP)
      when :integer
        self[:value].to_i
      when :boolean
        (self[:value].to_s =~ /^[t|1]/i) != nil
      else
        self[:value].is_a?(String) ? YAML.load(self[:value]) : self[:value]
      end
    else
      self[:value]
    end
  end

  def raw_value
    self[:value]
  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
spree_core-1.3.5 app/models/spree/preference.rb
spree_core-1.3.4 app/models/spree/preference.rb
spree_core-1.3.3 app/models/spree/preference.rb
spree_core-1.3.2 app/models/spree/preference.rb