mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Rails 5 introduced this new class to confine application-specific monkey patches to our models only, and not leak into other libraries using ActiveRecord::Base. https://bigbinary.com/blog/application-record-in-rails-5
43 lines
1.1 KiB
Ruby
43 lines
1.1 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module Spree
|
|
class Preference < ApplicationRecord
|
|
serialize :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(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.safe_load(self[:value]) : self[:value]
|
|
end
|
|
else
|
|
self[:value]
|
|
end
|
|
end
|
|
|
|
def raw_value
|
|
self[:value]
|
|
end
|
|
end
|
|
end
|