Sha256: 2df139a846fb8d8a2698989245de66a8fa6678465a90dd65c376c1d7405cc4c6

Contents?: true

Size: 995 Bytes

Versions: 15

Compression:

Stored size: 995 Bytes

Contents

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

  validates :key, presence: true
  validates :value_type, presence: true

  scope :valid, -> { 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

15 entries across 15 versions & 1 rubygems

Version Path
spree_core-2.0.13 app/models/spree/preference.rb
spree_core-2.0.12 app/models/spree/preference.rb
spree_core-2.0.11 app/models/spree/preference.rb
spree_core-2.0.10 app/models/spree/preference.rb
spree_core-2.0.9 app/models/spree/preference.rb
spree_core-2.0.8 app/models/spree/preference.rb
spree_core-2.0.7 app/models/spree/preference.rb
spree_core-2.0.6 app/models/spree/preference.rb
spree_core-2.0.5 app/models/spree/preference.rb
spree_core-2.0.4 app/models/spree/preference.rb
spree_core-2.0.3 app/models/spree/preference.rb
spree_core-2.0.2 app/models/spree/preference.rb
spree_core-2.0.1 app/models/spree/preference.rb
spree_core-2.0.0 app/models/spree/preference.rb
spree_core-2.0.0.rc1 app/models/spree/preference.rb