add_more ok

This commit is contained in:
Matthew K. Fu JuYuan authored and chris committed 2012-11-20 09:52:50 +08:00
1 parent 69bf552fa7
commit be448d42c9
12 files changed
+645 -489

No files matched your search

+56 -10
View File
@@ -13,10 +13,10 @@ class AttributeField
field :built_in, :type => Boolean, :default => false
field :disabled, :type => Boolean, :default => false
field :to_delete,:type=> Boolean,:default => false
field :typeA,:type=> Hash,:default=>{}
field :typeA,:type=> Hash,:default=>{:cross_lang=>false}
field :typeB,:type=> Hash,:default=>{}
field :typeC,:type=> Hash,:default=>{:claendar=>"west_claendar"}
field :typeD,:type=> Hash,:default=>{}
field :typeC,:type=> Hash,:default=>{:claendar=>"west_claendar",:format=>"format3"}
field :typeD,:type=> Hash,:default=>{:cross_lang=>false}
field :typeE,:type=> Hash,:default=>{}
@@ -30,6 +30,11 @@ class AttributeField
has_many :attribute_values,:autosave => true, :dependent => :destroy
before_save :check_option_list
# validates_uniqueness_of :key
def markup_value
get_data["option_list"]
end
def add_more
(get_data["add_more"] == "true" ? true : false) rescue false
end
@@ -47,11 +52,11 @@ class AttributeField
(self.attribute.role.method(self[:key].pluralize.to_sym) && self.attribute.role.method(self[:key].pluralize+"_for_"+markup)) rescue false
end
def markup_value=(var)
if !self_defined_markup_options?
self[:markup_value] = (eval(var) rescue {})
end
end
# def markup_value=(var)
# if !self_defined_markup_options?
# self[:markup_value] = (eval(var) rescue {})
# end
# end
def option_list
if self_defined_markup_options?
@@ -82,7 +87,7 @@ class AttributeField
end
def role
self.attribute.role
self.attribute.role
end
def panel
@@ -138,7 +143,48 @@ class AttributeField
# def select_list_options
# self.list_options.to_a.join(', ')
# end
def add_more_convert(opt)
case opt
when :to_add_more
self.attribute_values.each do |av|
VALID_LOCALES.each do |loc|
splited_str = av[loc].split(",") rescue []
av["val"] = [] if av["val"].nil?
splited_str.each_with_index{|value,index| av["val"][index] = av["val"][index].nil? ? {loc=>value} : av["val"][index].merge(loc=>value) }
# av[loc] = Hash[splited_str.each_with_index.map{|t,index| [index.to_s,t]}] rescue {"0"=>av[loc].to_s}
end
av.save
end #of self.attribute_values.each
when :to_no_add_more
self.attribute_values.each do |av|
VALID_LOCALES.each do |loc|
if av["val"].kind_of? Array
av[loc] = av["val"].collect{|t| t[loc]}.join(",")
else
av[loc] = av["val"]
end
# av[loc] = av["val"].invert.keys.join(",") rescue av["val"]
# av.save(:validate => false)
end
av.unset("val")
av.save
end #of self.attribute_values.each
end
end
def typeA=(var)
if self["typeA"]["add_more"] != var["add_more"]
case var["add_more"]
when "true" #from no-add_more to add_more
add_more_convert(:to_add_more)
else #from add_more to no-add_more
add_more_convert(:to_no_add_more)
end # of case
end # of if
self["typeA"] = var
end
def is_built_in?
self.built_in
end
+59 -46
View File
@@ -1,9 +1,8 @@
class AttributeValue
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::MultiParameterAttributes
include AttributeValuesHelper
field :key
belongs_to :attribute_field
@@ -14,48 +13,52 @@ class AttributeValue
# NO_MULTI_TAG = ["select","date","radio_button","checkbox","date_durnation"]
def data_proc
# binding.pry
# binding.pry if self.attribute_field.markup == 'radio_button'
# 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
# end #of if self.attribute_field
self.unset('temp_data')
def add_more_counter
index_max = self["val"].count rescue 0
index_max == 0 ? 1 : index_max
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
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
self[:temp_data].each{|key,val|
self[key] = val
} if(!self.attribute_field.get_data[:cross_lang])
self["val"] = self[:temp_data] if(self.attribute_field.get_data[:cross_lang])
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 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] ? 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
# binding
self[:temp_data] = value
end
@@ -70,27 +73,32 @@ class AttributeValue
self[(field[0].to_s.delete "=")] = field[1]
end
end
def get_value_by_locale(locale)
def get_value_by_locale(locale,add_more_index=nil)
case self.attribute_field.markup
when "text_field"
self.attribute_field.locale ? self[locale.to_s] : self.value
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 : eval(self.attribute_field.markup_value)
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"
Date.new(self[:val]["(1i)"].to_i,self[:val]["(2i)"].to_i,self[:val]["(3i)"].to_i) rescue nil
get_date_by_format
when "addr"
self.value
when "radio_button"
markup_values = eval(self.attribute_field.markup_value)
markup_values[:value][locale.to_s]
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].keys.collect{|key| markup_values[key][locale.to_s]}.join(",")
self.value.collect{|key| markup_values[key][locale.to_s]}.join(",")
when "date_durnation"
self.value
else
@@ -113,5 +121,10 @@ class AttributeValue
# end
# end
# end
protected
def get_date
Date.new(self[:val]["(1i)"].to_i,self[:val]["(2i)"].to_i,self[:val]["(3i)"].to_i) rescue nil
end
end