orbit-basic/app/models/user/attribute_value.rb

127 lines
3.8 KiB
Ruby

class AttributeValue
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::MultiParameterAttributes
include AttributeValuesHelper
field :key
belongs_to :attribute_field
belongs_to :user
before_save :check_key
before_save :data_proc
# NO_MULTI_TAG = ["select","date","radio_button","checkbox","date_durnation"]
def add_more_counter
index_max = self["val"].count rescue 0
index_max == 0 ? 1 : index_max
end
def value(index = nil)
result = case self.attribute_field.markup
when 'text_field','text_area'
if self.attribute_field.add_more and (self.attribute_field.markup == "text_field")
index.nil? ? self["val"] : self["val"][index]
# self.attribute_field.get_data[:cross_lang] ? Hash[VALID_LOCALES.collect{|lang| [lang,self[lang.to_sym]]}] : self["val"] #if !self.attribute_field.get_data[:cross_lang]
else
self.attribute_field.get_data["cross_lang"] =="true" ? self["val"] : Hash[VALID_LOCALES.collect{|lang| [lang,self[lang.to_sym]]}]
end
when 'select','date','radio_button'
self["val"]
when 'checkbox'
self["val"]
end #end of case self.attribute_field.markup
end
def value=(value)
#save everything to temp_data waiting for futher process
self[:temp_data] = value
end
def get_value_by_locale(locale,add_more_index=nil)
case self.attribute_field.markup
when "text_field"
case self.attribute_field.add_more
when true
add_more_index.nil? ? self.value.collect{|t| t[locale]}.join(",") : self.value(add_more_index)[locale]
when false
self.attribute_field.locale ? self[locale.to_s] : self.value
end
when "select"
markup_values = self.attribute_field.self_defined_markup_options? ? self.attribute_field.markup_value : self.attribute_field.markup_value
markup_values[self.value][locale.to_s] rescue 'NoData'
when "text_area"
self.attribute_field.locale ? self[locale.to_s] : self.value
when "date"
get_date_by_format
when "addr"
self.value
when "radio_button"
markup_values = self.attribute_field.markup_value
markup_values[self.value][locale.to_s]
when "checkbox"
markup_values = self.attribute_field.markup_value
self.value.collect{|key| markup_values[key][locale.to_s]}.join(",")
when "date_durnation"
self.value
else
self.attribute_field.locale ? self[locale.to_s] : self.value
end
end
protected
def unset_all_lang_values
VALID_LOCALES.each{|t| self.unset t}
end
def data_proc
unless self[:temp_data].nil?
case self.attribute_field.markup
when 'text_field','text_area'
if self.attribute_field.add_more
self["val"] = self["temp_data"]
else # if not add_more
if self.attribute_field.can_muti_lang_input?
self[:temp_data].each do |key,val|
self[key] = val
end if(!self.attribute_field.get_data[:cross_lang])
else
self["val"] = self[:temp_data]
end
end # of self.attribute_field.add_more
when 'select','date','radio_button'
self["val"] = self[:temp_data]
when 'checkbox'
self["val"] = self[:temp_data].keys
end #end of case self.attribute_field.markup
end # of self[:temp_data].nil?
self.unset('temp_data')
self.unset('temp')
end #of data_proc
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
end
def get_date
Date.new(self[:val]["(1i)"].to_i,self[:val]["(2i)"].to_i,self[:val]["(3i)"].to_i) rescue nil
end
end