Orbit/app/models/user/attribute_value.rb

167 lines
4.8 KiB
Ruby
Raw Permalink 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
2012-11-08 10:59:19 +00:00
include AttributeValuesHelper
2011-04-28 14:47:34 +00:00
field :key
field :address_ext
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
2012-11-12 02:41:41 +00:00
2012-11-08 10:59:19 +00:00
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
2012-11-12 02:41:41 +00:00
self.attribute_field.get_data["cross_lang"] =="true" ? self["val"] : Hash[VALID_LOCALES.collect{|lang| [lang,self[lang.to_sym]]}]
2012-11-08 10:59:19 +00:00
end
2013-08-09 07:07:05 +00:00
when 'select','radio_button','address'
2012-11-08 10:59:19 +00:00
self["val"]
2013-08-09 07:07:05 +00:00
when 'date'
if !self["val"].blank? and !self["val"]['(1i)'].blank?
"#{self["val"]['(1i)']}/#{self["val"]['(2i)']}/#{self["val"]['(3i)']}"
else
self["val"]
end
2012-11-08 10:59:19 +00:00
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
2012-11-08 10:59:19 +00:00
def get_value_by_locale(locale,add_more_index=nil)
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-08 10:59:19 +00:00
case self.attribute_field.add_more
when true
if self.attribute_field.locale
2013-08-09 07:07:05 +00:00
add_more_index.nil? ? self.value.collect{|t| t[locale.to_s]}.join(",") : self.value(add_more_index)[locale]
else
add_more_index.nil? ? self.value.join(",") : self.value(add_more_index)
end
when false
self.attribute_field.locale ? self[locale.to_s] : self.value
2012-11-08 10:59:19 +00:00
end
2012-10-08 08:58:33 +00:00
when "select"
2012-11-08 10:59:19 +00:00
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"
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"
if self.attribute_field.date_is_range?
get_date_by_format(:from) + ' ~ ' + get_date_by_format(:to)
2013-08-09 07:07:05 +00:00
# self.value["from"] + ' ~ ' + self.value["to"]
else
get_date_by_format
2013-08-09 07:07:05 +00:00
# self.value
end
when "address"
self.value[locale.to_s]
when "radio_button"
2012-11-08 10:59:19 +00:00
markup_values = self.attribute_field.markup_value
markup_values[self.value][locale.to_s]
when "checkbox"
2012-11-05 03:30:15 +00:00
markup_values = self.attribute_field.markup_value
2012-11-08 10:59:19 +00:00
self.value.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_date(item = nil)
case item
when :from
2013-08-09 07:07:05 +00:00
# data = self[:val]["from"]
data = self.value["from"]
when :to
2013-08-09 07:07:05 +00:00
# data = self[:val]["to"]
data = self.value["to"]
when nil
2013-08-09 07:07:05 +00:00
# data = self[:val]
data = self.value
end
2013-08-09 07:07:05 +00:00
# Date.new(data["(1i)"].to_i,data["(2i)"].to_i,data["(3i)"].to_i) rescue nil
end
2012-11-12 02:41:41 +00:00
protected
2012-11-08 10:59:19 +00:00
2012-11-12 02:41:41 +00:00
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 "address"
2012-11-20 02:05:38 +00:00
self["val"] = self["temp_data"]
2012-11-12 02:41:41 +00:00
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
2011-04-28 14:47:34 +00:00
end