Orbit/app/models/attribute_model.rb

52 lines
1.3 KiB
Ruby

class AttributeModel
include Mongoid::Document
include Mongoid::Timestamps
field :key
field :i18n_variable_id, :type => BSON::ObjectId, :index => true
field :markup
field :locale, :type => Boolean
field :list_options, :type => Array
embedded_in :user_attribute_model, :inverse_of => :attribute_models
validates_uniqueness_of :key
# Destroy the i18n_variable
def destroy_i18n_variable
self.i18n_variable.destroy rescue nil
end
# Get the i18n_variable
def i18n_variable
@i18n_variable ||= I18nVariable.find(self.i18n_variable_id) rescue nil
end
# Update or create the i18n_variable record
def i18n_variable=(attr)
if self.i18n_variable_id
self.i18n_variable.update_attributes(attr)
else
var = I18nVariable.new(attr.merge({:key => self.key, :document_class => self.class, :parent_id => self.user_attribute_model.i18n_variable_id}))
var.save
self.i18n_variable_id = var.id
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
# Check if the attribute is set to be destroyed
def should_destroy?
should_destroy.to_i == 1 rescue nil
end
end