forked from saurabh/orbit4-5
				
			
		
			
	
	
		
			152 lines
		
	
	
		
			4.0 KiB
		
	
	
	
		
			Ruby
		
	
	
	
		
		
			
		
	
	
			152 lines
		
	
	
		
			4.0 KiB
		
	
	
	
		
			Ruby
		
	
	
	
| 
								 | 
							
								class AttributeField
							 | 
						||
| 
								 | 
							
								  include Mongoid::Document
							 | 
						||
| 
								 | 
							
								  include Mongoid::Timestamps
							 | 
						||
| 
								 | 
							
								  include Mongoid::Attributes::Dynamic
							 | 
						||
| 
								 | 
							
								  include ::AttributeFieldsHelper
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  field :key, type: String
							 | 
						||
| 
								 | 
							
								  field :af_count
							 | 
						||
| 
								 | 
							
								  field :title, type: String, localize: true
							 | 
						||
| 
								 | 
							
								  field :markup, default: "text_field"
							 | 
						||
| 
								 | 
							
								  field :option_list, type: Hash,default: {}
							 | 
						||
| 
								 | 
							
								  field :markup_options, type: Hash
							 | 
						||
| 
								 | 
							
								  field :built_in, type: Boolean, default: false
							 | 
						||
| 
								 | 
							
								  field :disabled, type: Boolean, default: false
							 | 
						||
| 
								 | 
							
								  field :to_delete, type: Boolean,default: false
							 | 
						||
| 
								 | 
							
								  
							 | 
						||
| 
								 | 
							
								  field :to_search, type: Boolean, default: false
							 | 
						||
| 
								 | 
							
								  field :to_show,type: Boolean,default: true
							 | 
						||
| 
								 | 
							
								  
							 | 
						||
| 
								 | 
							
								  field :typeA, type: Hash, default: {cross_lang: false}
							 | 
						||
| 
								 | 
							
								  field :typeB, type: Hash, default: {}
							 | 
						||
| 
								 | 
							
								  field :typeC, type: Hash, default: {calendar: "west_calendar", format: "format3"}
							 | 
						||
| 
								 | 
							
								  field :typeD, type: Hash, default: {cross_lang: false}
							 | 
						||
| 
								 | 
							
								  field :typeE, type: Hash, default: {}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  belongs_to :role
							 | 
						||
| 
								 | 
							
								  has_many :attribute_values, autosave: true, dependent: :destroy
							 | 
						||
| 
								 | 
							
								  before_save :check_option_list
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  def markup_value
							 | 
						||
| 
								 | 
							
								    get_data["option_list"]
							 | 
						||
| 
								 | 
							
								  end
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  def add_more
							 | 
						||
| 
								 | 
							
								     (get_data["add_more"] == "true" ? true : false) rescue false
							 | 
						||
| 
								 | 
							
								  end
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  def locale
							 | 
						||
| 
								 | 
							
								    get_data["cross_lang"] == "true" ? false : true
							 | 
						||
| 
								 | 
							
								  end
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  def self_defined_markup_options?
							 | 
						||
| 
								 | 
							
								    (self.role.method(self[:key].pluralize.to_sym)  && self.role.method(self[:key].pluralize+"_for_"+markup)) rescue false
							 | 
						||
| 
								 | 
							
								  end
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  def option_list
							 | 
						||
| 
								 | 
							
								    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.role.method(self[:key].pluralize+"_for_"+markup)
							 | 
						||
| 
								 | 
							
								      return  (method.call rescue {})
							 | 
						||
| 
								 | 
							
								    elsif self[:option_list].nil?  || (self[:option_list].empty?)
							 | 
						||
| 
								 | 
							
								      return {}
							 | 
						||
| 
								 | 
							
								    else
							 | 
						||
| 
								 | 
							
								      return self[:option_list]
							 | 
						||
| 
								 | 
							
								    end
							 | 
						||
| 
								 | 
							
								  end
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  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
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  def panel
							 | 
						||
| 
								 | 
							
								    panel  = LIST[:markups][self[:markup]]["panel"]
							 | 
						||
| 
								 | 
							
								  end
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  def get_data
							 | 
						||
| 
								 | 
							
								    self[panel]
							 | 
						||
| 
								 | 
							
								  end
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  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
							 | 
						||
| 
								 | 
							
								  
							 | 
						||
| 
								 | 
							
								  def is_disabled?
							 | 
						||
| 
								 | 
							
								    self.disabled
							 | 
						||
| 
								 | 
							
								  end
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  def self.add_attribute_field(role,role_param, attribute_field_id=nil,field_status)
							 | 
						||
| 
								 | 
							
								    @field_name = 'role'
							 | 
						||
| 
								 | 
							
								    if field_status.eql?(true)
							 | 
						||
| 
								 | 
							
								      @attribute_field_counter = role.attribute_fields.count rescue nil
							 | 
						||
| 
								 | 
							
								      @attribute_field = self.find(attribute_field_id) rescue nil
							 | 
						||
| 
								 | 
							
								      @attribute_field.update(role_param)
							 | 
						||
| 
								 | 
							
								      @attribute_field.save
							 | 
						||
| 
								 | 
							
								      @attribute_field[:af_count] = @attribute_field_counter
							 | 
						||
| 
								 | 
							
								    else
							 | 
						||
| 
								 | 
							
								      @attribute_field_counter = role.attribute_fields.count rescue nil
							 | 
						||
| 
								 | 
							
								      @attribute_field = role.attribute_fields.build(role_param) rescue nil
							 | 
						||
| 
								 | 
							
								      @attribute_field.save  
							 | 
						||
| 
								 | 
							
								      @attribute_field[:af_count] = @attribute_field_counter
							 | 
						||
| 
								 | 
							
								    end
							 | 
						||
| 
								 | 
							
								    return @attribute_field
							 | 
						||
| 
								 | 
							
								  end
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  protected
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  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
							 | 
						||
| 
								 | 
							
								  end
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								   def check_add_more_convert(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
							 | 
						||
| 
								 | 
							
								  end
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  def cross_lang_convert(opt) 
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 | 
							
								  end
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  def check_option_list
							 | 
						||
| 
								 | 
							
								    self[:option_list] = self[panel]["option_list"] rescue nil
							 | 
						||
| 
								 | 
							
								  end
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  def add_more_convert(opt) 
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  end
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								end
							 |