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-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
|
2012-11-03 21:56:29 +00:00
|
|
|
# field :locale, :type => Boolean, :default => true
|
2012-10-12 10:09:02 +00:00
|
|
|
# field :list_options, :type => Array
|
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
|
|
|
|
field :typeA,:type=> Hash,:default=>{}
|
|
|
|
field :typeB,:type=> Hash,:default=>{}
|
|
|
|
field :typeC,:type=> Hash,:default=>{:claendar=>"west_claendar"}
|
|
|
|
field :typeD,:type=> Hash,:default=>{}
|
|
|
|
field :typeE,:type=> Hash,:default=>{}
|
|
|
|
|
2012-10-12 10:09:02 +00:00
|
|
|
|
2012-09-13 10:39:27 +00:00
|
|
|
#field :title, localize: true
|
|
|
|
|
2012-11-03 21:56:29 +00:00
|
|
|
field :title, localize: true
|
|
|
|
|
2012-09-13 10:39:27 +00:00
|
|
|
|
2012-01-24 07:12:06 +00:00
|
|
|
belongs_to :attribute
|
2012-09-17 04:06:21 +00:00
|
|
|
# belongs_to :role
|
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
|
2011-12-23 10:34:21 +00:00
|
|
|
# validates_uniqueness_of :key
|
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-05 03:30:15 +00:00
|
|
|
default = true
|
|
|
|
if get_data["locale"].nil?
|
|
|
|
return default
|
|
|
|
else
|
|
|
|
(get_data["locale"] == "true" ? true : false) rescue default
|
|
|
|
end
|
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
|
|
|
|
|
|
|
|
def markup_value=(var)
|
|
|
|
if !self_defined_markup_options?
|
|
|
|
self[:markup_value] = (eval(var) rescue {})
|
|
|
|
end
|
|
|
|
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
|
|
|
|
self.attribute.role
|
|
|
|
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-03 21:56:29 +00:00
|
|
|
# def title_translations
|
|
|
|
# if locale
|
|
|
|
# return self.locale_title_translations
|
|
|
|
# else
|
|
|
|
# return self[:neutral_title] #Hash[VALID_LOCALES.map{|d| [d,neutral_title]}]
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
|
|
|
|
# def title_translations=(var)
|
|
|
|
# if locale
|
|
|
|
# self.locale_title_translations = var
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
|
|
|
|
# def title
|
|
|
|
# if locale
|
|
|
|
# return self.locale_title
|
|
|
|
# else
|
|
|
|
# return self.neutral_title
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
|
|
|
|
# def check_title
|
|
|
|
# if locale
|
|
|
|
# self.locale_title_translations = self[:temp_title]
|
|
|
|
# else
|
|
|
|
# self.neutral_title = self[:temp_title]
|
|
|
|
# end
|
|
|
|
# self.unset("temp_title")
|
|
|
|
# end
|
|
|
|
|
|
|
|
# def title=(var)
|
|
|
|
# self["temp_title"] = var
|
|
|
|
# end
|
2012-09-13 10:39:27 +00:00
|
|
|
|
2012-10-12 10:09:02 +00:00
|
|
|
# # Convert the string list_options into an array
|
|
|
|
# def select_list_options=(var)
|
|
|
|
# self.list_options = var.gsub(' ', '').split(',')
|
|
|
|
# end
|
2011-12-23 10:34:21 +00:00
|
|
|
|
2012-10-12 10:09:02 +00:00
|
|
|
# # Convert the array list_options into a string
|
|
|
|
# def select_list_options
|
|
|
|
# self.list_options.to_a.join(', ')
|
|
|
|
# end
|
2011-12-23 10:34:21 +00:00
|
|
|
|
|
|
|
def is_built_in?
|
|
|
|
self.built_in
|
|
|
|
end
|
|
|
|
|
|
|
|
def is_disabled?
|
|
|
|
self.disabled
|
|
|
|
end
|
|
|
|
|
2012-11-03 21:56:29 +00:00
|
|
|
protected
|
|
|
|
def check_option_list
|
|
|
|
self[:option_list] = self[panel]["option_list"]
|
|
|
|
end
|
|
|
|
|
2011-12-23 10:34:21 +00:00
|
|
|
end
|