| 
									
										
										
										
											2010-01-14 10:30:53 +00:00
										 |  |  | class Item | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-04-13 10:19:51 +00:00
										 |  |  |   include Mongoid::Document | 
					
						
							| 
									
										
										
										
											2011-03-08 09:25:46 +00:00
										 |  |  |   include Mongoid::Timestamps | 
					
						
							| 
									
										
										
										
											2010-01-14 10:30:53 +00:00
										 |  |  |    | 
					
						
							| 
									
										
										
										
											2011-04-13 10:19:51 +00:00
										 |  |  |   field :name, :index => true | 
					
						
							|  |  |  |   field :full_name, :index => true | 
					
						
							|  |  |  |   field :i18n_variable_id, :type => BSON::ObjectId | 
					
						
							|  |  |  |   field :parent_id, :index => true | 
					
						
							|  |  |  |   field :parent_name | 
					
						
							|  |  |  |   field :position, :type => Integer | 
					
						
							|  |  |  |   field :is_published, :type => Boolean, :default => true, :index => true | 
					
						
							| 
									
										
										
										
											2010-01-18 07:52:52 +00:00
										 |  |  |   | 
					
						
							| 
									
										
										
										
											2010-02-22 07:27:11 +00:00
										 |  |  |   validates_format_of :name, :with => /^[0-9a-zA-Z\-_]+$/ | 
					
						
							| 
									
										
										
										
											2011-04-13 10:19:51 +00:00
										 |  |  |   validates :name, :exclusion => { :in => LIST[:forbidden_item_names] } | 
					
						
							| 
									
										
										
										
											2010-02-05 09:18:57 +00:00
										 |  |  |   validates_uniqueness_of :name, :scope => :parent_id | 
					
						
							| 
									
										
										
										
											2011-04-13 10:19:51 +00:00
										 |  |  |   validates_presence_of :name, :full_name, :position, :is_published | 
					
						
							|  |  |  |   validates_presence_of :parent_id, :if => :validates_presence_of_parent_id? | 
					
						
							| 
									
										
										
										
											2010-02-05 09:04:07 +00:00
										 |  |  |    | 
					
						
							| 
									
										
										
										
											2011-04-13 10:19:51 +00:00
										 |  |  |   referenced_in :parent, :class_name => "Item" | 
					
						
							|  |  |  |   references_many :children, :class_name => "Item" | 
					
						
							| 
									
										
										
										
											2010-01-14 10:30:53 +00:00
										 |  |  |    | 
					
						
							|  |  |  |   before_validation :setup_default_value | 
					
						
							| 
									
										
										
										
											2010-02-22 07:27:11 +00:00
										 |  |  |    | 
					
						
							| 
									
										
										
										
											2011-01-28 06:44:08 +00:00
										 |  |  |   # Destroy the i18n_variable | 
					
						
							|  |  |  |   def destroy_i18n_variable | 
					
						
							|  |  |  |     self.i18n_variable.destroy rescue nil | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											2010-01-14 10:30:53 +00:00
										 |  |  |   def self.find_by_name(item_name) | 
					
						
							| 
									
										
										
										
											2010-03-01 08:18:28 +00:00
										 |  |  |     Item.first(:conditions => { :name => item_name, :is_published => true }) | 
					
						
							| 
									
										
										
										
											2010-01-14 10:30:53 +00:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2010-01-18 07:52:52 +00:00
										 |  |  |    | 
					
						
							| 
									
										
										
										
											2011-04-13 10:19:51 +00:00
										 |  |  |   # Get an array of ancestors | 
					
						
							| 
									
										
										
										
											2010-01-18 07:52:52 +00:00
										 |  |  |   def ancestors | 
					
						
							|  |  |  |     node, nodes = self, [] | 
					
						
							| 
									
										
										
										
											2011-04-13 10:19:51 +00:00
										 |  |  |     nodes << node = node.parent while !node.parent.blank? rescue nil | 
					
						
							| 
									
										
										
										
											2010-01-18 07:52:52 +00:00
										 |  |  |     nodes.reverse | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-04-13 10:19:51 +00:00
										 |  |  |    | 
					
						
							| 
									
										
										
										
											2011-01-28 06:44:08 +00:00
										 |  |  |   # Destroy the i18n_variable | 
					
						
							|  |  |  |   def destroy_i18n_variable | 
					
						
							|  |  |  |     self.i18n_variable.destroy rescue nil | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											2011-04-13 10:19:51 +00:00
										 |  |  |   # 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.name, :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 | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   # Check if the page is home | 
					
						
							|  |  |  |   def is_home? | 
					
						
							| 
									
										
										
										
											2011-01-31 02:57:41 +00:00
										 |  |  |     self.name.eql?('home') && (self.parent_id.nil? || self.parent_id.empty?) | 
					
						
							| 
									
										
										
										
											2011-04-13 10:19:51 +00:00
										 |  |  |   end | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   # Build the url from the array of ancestors | 
					
						
							| 
									
										
										
										
											2010-02-05 09:18:57 +00:00
										 |  |  |   def url | 
					
						
							|  |  |  |     urls = ancestors.map{ |a| a.name } << self.name | 
					
						
							| 
									
										
										
										
											2011-04-13 10:19:51 +00:00
										 |  |  |     urls.join("/") | 
					
						
							| 
									
										
										
										
											2010-02-05 09:18:57 +00:00
										 |  |  |   end | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											2010-01-14 10:30:53 +00:00
										 |  |  |   protected | 
					
						
							| 
									
										
										
										
											2010-02-05 08:53:52 +00:00
										 |  |  |    | 
					
						
							| 
									
										
										
										
											2010-01-14 10:30:53 +00:00
										 |  |  |   def setup_default_value | 
					
						
							| 
									
										
										
										
											2011-04-13 10:19:51 +00:00
										 |  |  |     # Set the position value within the parent scope | 
					
						
							| 
									
										
										
										
											2010-01-14 10:30:53 +00:00
										 |  |  |     if self.position.blank? | 
					
						
							| 
									
										
										
										
											2011-04-13 10:19:51 +00:00
										 |  |  |       max_page = Item.where(:parent_id => self.parent_id).count | 
					
						
							|  |  |  |       self.position = (max_page)? max_page + 1 : 1
 | 
					
						
							| 
									
										
										
										
											2010-01-14 10:30:53 +00:00
										 |  |  |     end | 
					
						
							|  |  |  |      | 
					
						
							| 
									
										
										
										
											2011-04-13 10:19:51 +00:00
										 |  |  |     # Set the parent value | 
					
						
							|  |  |  |     self.parent_name = Item.first(:conditions => {:id => self.parent_id} ).name rescue nil | 
					
						
							| 
									
										
										
										
											2010-02-11 08:46:20 +00:00
										 |  |  |      | 
					
						
							| 
									
										
										
										
											2011-04-13 10:19:51 +00:00
										 |  |  |     # Build the full_name from the ancestors array | 
					
						
							| 
									
										
										
										
											2010-02-11 08:46:20 +00:00
										 |  |  |     full_node = self.ancestors.map{ |a| a.name }.push( self.name ) | 
					
						
							| 
									
										
										
										
											2011-04-13 10:19:51 +00:00
										 |  |  |     # Remove root node if not root | 
					
						
							| 
									
										
										
										
											2010-02-11 08:46:20 +00:00
										 |  |  |     full_node.shift if full_node.size >= 2
 | 
					
						
							|  |  |  |     self.full_name = full_node.join("/") | 
					
						
							| 
									
										
										
										
											2010-01-14 10:30:53 +00:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-04-13 10:19:51 +00:00
										 |  |  |    | 
					
						
							|  |  |  |   # Enable the validation for parent_id | 
					
						
							|  |  |  |   def validates_presence_of_parent_id? | 
					
						
							|  |  |  |     true | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2010-01-14 10:30:53 +00:00
										 |  |  |      | 
					
						
							| 
									
										
										
										
											2011-04-13 10:19:51 +00:00
										 |  |  | end |