orbit-basic/app/models/user_attribute_model.rb

61 lines
1.5 KiB
Ruby
Raw Normal View History

class UserAttributeModel
include Mongoid::Document
2011-03-08 09:25:46 +00:00
include Mongoid::Timestamps
field :key
field :i18n_variable_id, :type => BSON::ObjectId, :index => true
2011-02-01 02:46:26 +00:00
embeds_many :attribute_models
after_update :destroy_attrs
2011-02-01 02:46:26 +00:00
# Update or create the attribute_model records
def attribute_models=(*attrs)
2011-01-28 06:44:08 +00:00
attrs[0].each do |attributes|
if attributes[:id].blank?
2011-02-01 02:46:26 +00:00
attribute_models.build(attributes)
else
2011-02-01 02:46:26 +00:00
attribute_model = attribute_models.detect {|a| a.id.to_s == attributes[:id].to_s }
attribute_model.update_attributes(attributes)
end
end
end
2011-02-01 07:12:23 +00:00
# Destroy the i18n_variables
def destroy_i18n_variables
self.i18n_variable.destroy rescue nil
self.attribute_models.each do |attr|
attr.destroy_i18n_variable
end
end
# Update or create the i18n_variable record
def i18n_variable=(attr)
if self.i18n_variable_id
self.i18n_variable.update_attributes(attr) rescue nil
else
var = I18nVariable.new(attr.merge({:key => self.key, :document_class => self.class}))
var.save
self.i18n_variable_id = var.id
end
end
# Get the i18n_variable
def i18n_variable
@i18n_variable ||= I18nVariable.find(self.i18n_variable_id) rescue nil
end
protected
2011-02-01 02:46:26 +00:00
# Destroy the i18n_variable for each attribute_models if marked to destroy
def destroy_attrs
2011-02-01 02:46:26 +00:00
attribute_models.each do |a|
if a.should_destroy?
2011-01-28 06:44:08 +00:00
a.destroy_i18n_variable
end
end
end
end