Orbit/app/models/user/attribute_value.rb

118 lines
3.5 KiB
Ruby
Raw Normal View History

2011-04-28 14:47:34 +00:00
class AttributeValue
include Mongoid::Document
include Mongoid::Timestamps
2012-10-08 08:58:33 +00:00
include Mongoid::MultiParameterAttributes
2011-04-28 14:47:34 +00:00
field :key
2011-12-23 10:34:21 +00:00
belongs_to :attribute_field
belongs_to :user
2012-10-08 08:58:33 +00:00
before_save :check_key
before_save :data_proc
# NO_MULTI_TAG = ["select","date","radio_button","checkbox","date_durnation"]
2012-10-08 08:58:33 +00:00
def data_proc
# binding.pry
# binding.pry if self.attribute_field.markup == 'radio_button'
2012-11-05 03:30:15 +00:00
# if self.attribute_field
case self.attribute_field.markup
when 'text_field','text_area'
# binding.pry
self[:temp_data].each{|key,val|
self[key] = val
} unless self[:temp_data].nil?
when 'select','date','radio_button'
self["val"] = self[:temp_data]
when 'checkbox'
self["val"] = self[:temp_data].keys rescue {}
end #end of case
else # if not locale
case self.attribute_field.markup
when 'text_field','text_area'
self["val"] = self[:temp_data]
when 'select','date','radio_button'
self["val"] = self[:temp_data]
when 'checkbox'
self["val"] = self[:temp_data].keys rescue {}
end #end of case
2012-11-05 03:30:15 +00:00
# end #of if self.attribute_field
self.unset('temp_data')
end
def value
result=""
if self.attribute_field.locale && (self.attribute_field.markup == "text_field" || self.attribute_field.markup == "text_area")
result= Hash[VALID_LOCALES.collect{|lang| [lang,self[lang.to_sym]]}]
else
result = self["val"]
end
result
end
def value=(value)
#save everything to temp_data waiting for futher process
# binding
self[:temp_data] = value
end
2012-10-08 08:58:33 +00:00
def check_key
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
2011-12-23 10:34:21 +00:00
end
2011-04-28 14:47:34 +00:00
2012-10-08 08:58:33 +00:00
def get_value_by_locale(locale)
2012-11-05 03:30:15 +00:00
2012-10-08 08:58:33 +00:00
case self.attribute_field.markup
when "text_field"
2012-11-05 03:30:15 +00:00
self.attribute_field.locale ? self[locale.to_s] : self.value
2012-10-08 08:58:33 +00:00
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"
2012-11-05 03:30:15 +00:00
self.attribute_field.locale ? self[locale.to_s] : self.value
2012-10-08 08:58:33 +00:00
when "date"
Date.new(self[:val]["(1i)"].to_i,self[:val]["(2i)"].to_i,self[:val]["(3i)"].to_i) rescue nil
2012-10-08 08:58:33 +00:00
when "addr"
self.value
when "radio_button"
2012-10-18 10:36:21 +00:00
markup_values = eval(self.attribute_field.markup_value)
2012-11-05 03:30:15 +00:00
markup_values[:value][locale.to_s]
when "checkbox"
2012-11-05 03:30:15 +00:00
markup_values = self.attribute_field.markup_value
self[:value].keys.collect{|key| markup_values[key][locale.to_s]}.join(",")
when "date_durnation"
self.value
2012-10-08 08:58:33 +00:00
else
2012-11-05 03:30:15 +00:00
self.attribute_field.locale ? self[locale.to_s] : self.value
2012-10-08 08:58:33 +00:00
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
2012-10-08 08:58:33 +00:00
2011-04-28 14:47:34 +00:00
end