Files
openfoodnetwork/app/models/spree/preference.rb
Ana Nunes da Silva 5fde1cc7cd Fix duplicate branch in Spree::Preference#value method
:string, :text and :password cases have the same value
2024-04-02 10:20:17 +01:00

41 lines
1.1 KiB
Ruby

# frozen_string_literal: true
module Spree
class Preference < ApplicationRecord
serialize :value, coder: YAML
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, :password
self[:value].to_s
when :decimal
BigDecimal(self[:value].to_s, exception: false)&.round(2, BigDecimal::ROUND_HALF_UP) ||
self[:value]
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