| 
									
										
										
										
											2011-04-13 10:19:51 +00:00
										 |  |  | class UserAttributeModel | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   include Mongoid::Document | 
					
						
							| 
									
										
										
										
											2011-03-08 09:25:46 +00:00
										 |  |  |   include Mongoid::Timestamps | 
					
						
							| 
									
										
										
										
											2011-04-13 10:19:51 +00:00
										 |  |  |    | 
					
						
							|  |  |  |   field :key | 
					
						
							|  |  |  |   field :i18n_variable_id, :type => BSON::ObjectId, :index => true | 
					
						
							| 
									
										
										
										
											2011-05-02 04:07:31 +00:00
										 |  |  |   field :built_in, :type => Boolean, :default => false | 
					
						
							|  |  |  |   field :disabled, :type => Boolean, :default => false | 
					
						
							| 
									
										
										
										
											2011-04-13 10:19:51 +00:00
										 |  |  |    | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											2011-05-02 04:07:31 +00:00
										 |  |  |   def is_built_in? | 
					
						
							|  |  |  |     self.built_in | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   def is_disabled? | 
					
						
							| 
									
										
										
										
											2011-11-19 13:47:18 +00:00
										 |  |  |     self.disabled.blank? ? false : self.disabled | 
					
						
							| 
									
										
										
										
											2011-05-02 04:07:31 +00:00
										 |  |  |   end | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   def get_enabled_attribute_models | 
					
						
							|  |  |  |     self.attribute_models.excludes('disabled' => true) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											2011-04-13 10:19:51 +00:00
										 |  |  |   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 |