2011-04-13 10:19:51 +00:00
|
|
|
class UserAttributeModel
|
|
|
|
|
|
|
|
include Mongoid::Document
|
|
|
|
|
|
|
|
field :key
|
|
|
|
field :i18n_variable_id, :type => BSON::ObjectId, :index => true
|
|
|
|
|
2011-02-01 02:46:26 +00:00
|
|
|
embeds_many :attribute_models
|
2011-04-13 10:19:51 +00:00
|
|
|
|
|
|
|
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|
|
2011-04-13 10:19:51 +00:00
|
|
|
if attributes[:id].blank?
|
2011-02-01 02:46:26 +00:00
|
|
|
attribute_models.build(attributes)
|
2011-04-13 10:19:51 +00:00
|
|
|
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)
|
2011-04-13 10:19:51 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
2011-02-01 02:46:26 +00:00
|
|
|
# Destroy the i18n_variable for each attribute_models if marked to destroy
|
2011-04-13 10:19:51 +00:00
|
|
|
def destroy_attrs
|
2011-02-01 02:46:26 +00:00
|
|
|
attribute_models.each do |a|
|
2011-04-13 10:19:51 +00:00
|
|
|
if a.should_destroy?
|
2011-01-28 06:44:08 +00:00
|
|
|
a.destroy_i18n_variable
|
2011-04-13 10:19:51 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|