This repository has been archived on 2024-03-16. You can view files and clone it, but cannot push or open issues or pull requests.
orbit-4-1/app/models/attribute_model.rb

52 lines
1.3 KiB
Ruby
Raw Normal View History

2011-02-01 02:47:25 +00:00
class AttributeModel
include Mongoid::Document
2011-03-08 09:25:46 +00:00
include Mongoid::Timestamps
2011-02-01 02:47:25 +00:00
field :key
field :i18n_variable_id, :type => BSON::ObjectId, :index => true
field :markup
field :locale, :type => Boolean
field :list_options, :type => Array
2011-02-01 07:12:23 +00:00
embedded_in :user_attribute_model, :inverse_of => :attribute_models
2011-02-01 02:47:25 +00:00
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
2011-02-01 07:12:23 +00:00
var = I18nVariable.new(attr.merge({:key => self.key, :document_class => self.class, :parent_id => self.user_attribute_model.i18n_variable_id}))
2011-02-01 02:47:25 +00:00
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