| 
									
										
										
										
											2011-12-23 10:34:21 +00:00
										 |  |  | class AttributeField | 
					
						
							|  |  |  |   include Mongoid::Document | 
					
						
							|  |  |  |   include Mongoid::Timestamps | 
					
						
							| 
									
										
										
										
											2012-10-15 05:54:29 +00:00
										 |  |  |   include ::AttributeFieldsHelper | 
					
						
							| 
									
										
										
										
											2012-10-08 08:58:33 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-23 10:34:21 +00:00
										 |  |  |   field :key | 
					
						
							| 
									
										
										
										
											2012-12-05 12:32:28 +00:00
										 |  |  |   field :af_count | 
					
						
							| 
									
										
										
										
											2012-11-03 21:56:29 +00:00
										 |  |  |   field :markup ,:default=>"text_field" | 
					
						
							|  |  |  |   field :option_list ,:type => Hash,:default => {} | 
					
						
							| 
									
										
										
										
											2012-10-12 10:09:02 +00:00
										 |  |  |   field :markup_options,:type => Hash | 
					
						
							| 
									
										
										
										
											2011-12-23 10:34:21 +00:00
										 |  |  |   field :built_in, :type => Boolean, :default => false | 
					
						
							|  |  |  |   field :disabled, :type => Boolean, :default => false | 
					
						
							| 
									
										
										
										
											2012-11-03 21:56:29 +00:00
										 |  |  |   field :to_delete,:type=> Boolean,:default => false | 
					
						
							| 
									
										
										
										
											2012-11-08 10:59:19 +00:00
										 |  |  |   field :typeA,:type=> Hash,:default=>{:cross_lang=>false} | 
					
						
							| 
									
										
										
										
											2012-11-03 21:56:29 +00:00
										 |  |  |   field :typeB,:type=> Hash,:default=>{} | 
					
						
							| 
									
										
										
										
											2012-11-27 03:15:15 +00:00
										 |  |  |   field :typeC,:type=> Hash,:default=>{:calendar=>"west_calendar",:format=>"format3"} | 
					
						
							| 
									
										
										
										
											2012-11-08 10:59:19 +00:00
										 |  |  |   field :typeD,:type=> Hash,:default=>{:cross_lang=>false} | 
					
						
							| 
									
										
										
										
											2012-11-03 21:56:29 +00:00
										 |  |  |   field :typeE,:type=> Hash,:default=>{} | 
					
						
							|  |  |  |   field :title, localize: true | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											2012-01-24 07:12:06 +00:00
										 |  |  |   belongs_to :attribute | 
					
						
							| 
									
										
										
										
											2012-10-24 06:46:25 +00:00
										 |  |  |   has_many :attribute_values,:autosave => true, :dependent => :destroy | 
					
						
							| 
									
										
										
										
											2012-11-03 21:56:29 +00:00
										 |  |  |   before_save :check_option_list | 
					
						
							| 
									
										
										
										
											2012-11-08 10:59:19 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   def markup_value | 
					
						
							|  |  |  |     get_data["option_list"] | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-11-03 21:56:29 +00:00
										 |  |  |   def add_more | 
					
						
							|  |  |  |      (get_data["add_more"] == "true" ? true : false) rescue false | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def locale | 
					
						
							| 
									
										
										
										
											2012-11-22 09:49:44 +00:00
										 |  |  |     get_data["cross_lang"] == "true" ? false : true | 
					
						
							| 
									
										
										
										
											2012-11-03 21:56:29 +00:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2012-10-12 10:09:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-24 06:46:25 +00:00
										 |  |  |   def self_defined_markup_options? | 
					
						
							|  |  |  |     (self.attribute.role.method(self[:key].pluralize.to_sym)  && self.attribute.role.method(self[:key].pluralize+"_for_"+markup)) rescue false | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-11-03 21:56:29 +00:00
										 |  |  |   def option_list | 
					
						
							| 
									
										
										
										
											2012-10-24 06:46:25 +00:00
										 |  |  |     if self_defined_markup_options? | 
					
						
							|  |  |  |       #Class need to have corresponding field and value agent  | 
					
						
							|  |  |  |      # Ex: For "status" the class must have field called "statuses" for the relation and "statuses_for_select" for the select function  | 
					
						
							|  |  |  |      method = self.attribute.role.method(self[:key].pluralize+"_for_"+markup) | 
					
						
							|  |  |  |       return  (method.call rescue {}) | 
					
						
							| 
									
										
										
										
											2012-11-03 21:56:29 +00:00
										 |  |  |     elsif self[:option_list].nil?  || (self[:option_list].empty?) | 
					
						
							| 
									
										
										
										
											2012-10-24 06:46:25 +00:00
										 |  |  |       return {} | 
					
						
							|  |  |  |     else | 
					
						
							| 
									
										
										
										
											2012-11-03 21:56:29 +00:00
										 |  |  |       return self[:option_list] | 
					
						
							| 
									
										
										
										
											2012-10-24 06:46:25 +00:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-12 10:09:02 +00:00
										 |  |  |   def markup_options=(var) | 
					
						
							|  |  |  |     self[:markup_options] = (eval(var)  rescue {}) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def markup_options | 
					
						
							|  |  |  |     if self[:markup_options].nil? | 
					
						
							|  |  |  |       return {} | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       Hash[self[:markup_options].map{|key,val|[key.to_sym,val]}]  rescue {} | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-09-17 04:06:21 +00:00
										 |  |  |   def role | 
					
						
							| 
									
										
										
										
											2012-11-08 10:59:19 +00:00
										 |  |  |     self.attribute.role  | 
					
						
							| 
									
										
										
										
											2012-09-17 04:06:21 +00:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-11-03 21:56:29 +00:00
										 |  |  |   def panel | 
					
						
							|  |  |  |     panel  = LIST[:markups][self[:markup]]["panel"] | 
					
						
							| 
									
										
										
										
											2012-09-13 10:39:27 +00:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-11-03 21:56:29 +00:00
										 |  |  |   def get_data | 
					
						
							|  |  |  |     self[panel] | 
					
						
							| 
									
										
										
										
											2012-09-13 10:39:27 +00:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-12 10:09:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-11-12 02:41:41 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   def typeA=(var) | 
					
						
							|  |  |  |     check_add_more_convert(var) | 
					
						
							|  |  |  |     check_cross_lang_convert(var,"typeA") | 
					
						
							|  |  |  |     self["typeA"] = var | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def typeD=(var) | 
					
						
							|  |  |  |     check_cross_lang_convert(var,"typeD") | 
					
						
							|  |  |  |     self["typeD"] = var | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def is_built_in? | 
					
						
							|  |  |  |     self.built_in | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-12-23 10:34:21 +00:00
										 |  |  |    | 
					
						
							| 
									
										
										
										
											2012-11-12 02:41:41 +00:00
										 |  |  |   def is_disabled? | 
					
						
							|  |  |  |     self.disabled | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | protected | 
					
						
							|  |  |  |   def check_option_list | 
					
						
							|  |  |  |     self[:option_list] = self[panel]["option_list"] rescue nil | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2012-11-08 10:59:19 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   def add_more_convert(opt)  | 
					
						
							| 
									
										
										
										
											2012-11-22 09:49:44 +00:00
										 |  |  |     # 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?  | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     #       old_data = av["val"] | 
					
						
							|  |  |  |     #       av["val"] = []  | 
					
						
							|  |  |  |     #       splited_str.each_with_index do |value,index|  | 
					
						
							|  |  |  |     #         av["val"][index] =   {loc=>value}  | 
					
						
							|  |  |  |     #       end | 
					
						
							|  |  |  |     #       av["val"] << old_data | 
					
						
							|  |  |  |     #     end | 
					
						
							|  |  |  |     #     av.unset_all_lang_values | 
					
						
							|  |  |  |     #     av.save | 
					
						
							|  |  |  |     #   end #of self.attribute_values.each | 
					
						
							|  |  |  |     # when :to_no_add_more | 
					
						
							|  |  |  |     #   self.attribute_values.each  do |av| | 
					
						
							|  |  |  |     #     VALID_LOCALES.each  do |loc| | 
					
						
							|  |  |  |     #       binding.pry | 
					
						
							|  |  |  |     #       if av["val"].kind_of? Array | 
					
						
							|  |  |  |     #        av[loc] = av["val"].collect{|t| t[loc]}.join(",") | 
					
						
							|  |  |  |     #       else | 
					
						
							|  |  |  |     #         av[loc] = av["val"] | 
					
						
							|  |  |  |     #       end | 
					
						
							|  |  |  |     #     end | 
					
						
							|  |  |  |     #     av.unset("val") | 
					
						
							|  |  |  |     #     av.save | 
					
						
							|  |  |  |     #   end #of self.attribute_values.each | 
					
						
							|  |  |  |     # end | 
					
						
							| 
									
										
										
										
											2012-11-08 10:59:19 +00:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-11-12 02:41:41 +00:00
										 |  |  |   def cross_lang_convert(opt)  | 
					
						
							| 
									
										
										
										
											2012-11-22 09:49:44 +00:00
										 |  |  |     # case opt | 
					
						
							|  |  |  |     # when :to_cross_lang | 
					
						
							|  |  |  |     #   self.attribute_values.each  do |av|  | 
					
						
							|  |  |  |     #     if add_more | 
					
						
							|  |  |  |     #       av["val"] = av["val"].collect{|t| t.invert.keys.join(",")} | 
					
						
							|  |  |  |     #     else | 
					
						
							|  |  |  |     #       av["val"] = VALID_LOCALES.collect{|t| av[t]}.join(",")  | 
					
						
							|  |  |  |     #       av.unset_all_lang_values  | 
					
						
							|  |  |  |     #     end | 
					
						
							|  |  |  |     #     av.save | 
					
						
							|  |  |  |     #   end #of self.attribute_values.each | 
					
						
							|  |  |  |     # when :to_no_cross_lang | 
					
						
							|  |  |  |     #   default_locale = I18n.default_locale.to_s | 
					
						
							|  |  |  |     #   self.attribute_values.each  do |av| | 
					
						
							|  |  |  |     #     if add_more | 
					
						
							|  |  |  |     #       av["val"] = av["val"].collect{|t| {default_locale => t} } #unless av["val"].nil? | 
					
						
							|  |  |  |     #     else | 
					
						
							|  |  |  |     #       av[default_locale] = av["val"] | 
					
						
							|  |  |  |     #       av.unset("val")  | 
					
						
							|  |  |  |     #     end | 
					
						
							|  |  |  |     #     av.save | 
					
						
							|  |  |  |     #   end #of self.attribute_values.each | 
					
						
							|  |  |  |     # end | 
					
						
							| 
									
										
										
										
											2012-11-12 02:41:41 +00:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def check_add_more_convert(var) | 
					
						
							| 
									
										
										
										
											2012-11-08 10:59:19 +00:00
										 |  |  |     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 | 
					
						
							| 
									
										
										
										
											2011-12-23 10:34:21 +00:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-11-12 02:41:41 +00:00
										 |  |  |   def check_cross_lang_convert(var,field) | 
					
						
							|  |  |  |     if self[field]["cross_lang"] != var["cross_lang"] | 
					
						
							|  |  |  |       case var["cross_lang"] | 
					
						
							|  |  |  |       when "true"  #from no-add_more to add_more | 
					
						
							|  |  |  |         cross_lang_convert(:to_cross_lang)   | 
					
						
							|  |  |  |       else #from add_more to no-add_more | 
					
						
							|  |  |  |         cross_lang_convert(:to_no_cross_lang) | 
					
						
							|  |  |  |       end # of case | 
					
						
							|  |  |  |     end # of if | 
					
						
							| 
									
										
										
										
											2012-11-03 21:56:29 +00:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-23 10:34:21 +00:00
										 |  |  | end |