This repository has been archived on 2024-03-16. You can view files and clone it, but cannot push or open issues or pull requests.
orbit-4-1/app/models/user/attribute_value.rb

72 lines
2.1 KiB
Ruby

class AttributeValue
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::MultiParameterAttributes
field :key
belongs_to :attribute_field
belongs_to :user
before_save :check_key
# NO_MULTI_TAG = ["select","date","radio_button","checkbox","date_durnation"]
def check_key
binding.pry if attribute_field.nil?
self.key = attribute_field.key
end
def method_missing(*field)
if field.size < 1
self[field[0]]
else
self[(field[0].to_s.delete "=")] = field[1]
end
end
def get_value_by_locale(locale)
case self.attribute_field.markup
when "text_field"
self.attribute_field.locale ? self[locale] : self[:value]
when "select"
markup_values = self.attribute_field.self_defined_markup_options? ? self.attribute_field.markup_value : eval(self.attribute_field.markup_value)
markup_values[self[:value]][locale.to_s] rescue 'NoData'
when "text_area"
self.attribute_field.locale ? self[locale] : self[:value]
when "date"
Date.new(self[:value]["(1i)"].to_i,self[:value]["(2i)"].to_i,self[:value]["(3i)"].to_i) rescue nil
when "addr"
self[:value]
when "radio_button"
markup_values = eval(self.attribute_field.markup_value)
markup_values[self[:value].first[0]][locale.to_s]
when "checkbox"
markup_values = eval(self.attribute_field.markup_value)
self[:value].keys.collect{|key| markup_values[key][locale.to_s] }.join(",")
when "date_durnation"
self[:value]
else
self.attribute_field.locale ? self[locale] : self[:value]
end
end
def get_values
unless ['select','checkbox','radio_button'].include?(self.attribute_field.markup )
if self.attribute_field.locale && LIST[:markups][self.attribute_field.markup]["muti_lang_input_supprt"]
return Hash[VALID_LOCALES.collect{|lang| [lang,get_value_by_locale(lang.to_sym)]}]
else
return get_value_by_locale("")
end
else
if self.attribute_field.markup == "select"
self[:value]
else
self[:value].keys rescue self[:value]
end
end
end
end