orbit-basic/app/models/user/attribute_field.rb

121 lines
2.7 KiB
Ruby

class AttributeField
include Mongoid::Document
include Mongoid::Timestamps
include ::AttributeFieldsHelper
field :key
field :markup
field :markup_value ,:type => Hash
field :markup_options,:type => Hash
field :locale, :type => Boolean, :default => true
# field :list_options, :type => Array
field :built_in, :type => Boolean, :default => false
field :disabled, :type => Boolean, :default => false
field :add_more,:type => Boolean, :default => false
#field :title, localize: true
field :locale_title, localize: true
field :neutral_title
field :neutral_for
belongs_to :attribute
# belongs_to :role
has_many :attribute_values,:autosave => true, :dependent => :destroy
# validates_uniqueness_of :key
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
def markup_value
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 {})
elsif self[:markup_value].nil? || (self[:markup_value].empty?)
return {}
else
return self[:markup_value]
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 role
self.attribute.role
end
def title_translations
if locale
return locale_title_translations
else
return 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 title=(var)
# binding.pry
if locale
self.locale_title = var
else
self.neutral_title = var
end
end
# # Convert the string list_options into an array
# def select_list_options=(var)
# self.list_options = var.gsub(' ', '').split(',')
# end
# # Convert the array list_options into a string
# def select_list_options
# self.list_options.to_a.join(', ')
# end
def is_built_in?
self.built_in
end
def is_disabled?
self.disabled
end
end