module Comatose # Page attributes # - parent_id # - title # - full_path # - slug # - keywords # - layout # - body # - author # - filter_type # - position # - version # - updated_at # - created_at class Page < ActiveRecord::Base set_table_name 'comatose_pages' attr_accessor :new_root_page # Only versions the content... Not all of the meta data or position acts_as_versioned :table_name=>'comatose_page_versions', :if_changed => [:title, :slug, :keywords, :body] define_option :active_mount_info, {:root=>'', :index=>''} acts_as_tree :order => "position, title" acts_as_list :scope => :parent_id #before_create :create_full_path before_save :cache_full_path, :create_full_path after_save :update_children_full_path # Using before_validation so we can default the slug from the title before_validation do |record| # Create slug from title if record.slug.blank? and !record.title.blank? record.slug = record.title.downcase.lstrip.rstrip.gsub( /[^-a-z0-9~\s\.:;+=_]/, '').gsub(/[\s\.:;=_+]+/, '-').gsub(/[\-]{2,}/, '-').to_s end end validates_presence_of :title, :on => :save, :message => "must be present" validates_uniqueness_of :slug, :on => :save, :scope=>'parent_id', :message => "is already in use" validates :parent_id, :presence => true, :on => :create, :unless => :new_root_page # Tests ERB/Liquid content... validates_each :body, :allow_nil=>true, :allow_blank=>true do |record, attr, value| begin body_html = record.to_html rescue SyntaxError => e Comatose.logger.debug e.message record.errors.add :body, "syntax error: #{$!.to_s.gsub('<', '<')}" rescue => e Comatose.logger.debug e.message record.errors.add :body, "content error: #{$!.to_s.gsub('<', '<')}" end end class < 1 path = path[1..-1] if path.starts_with? "/" end end find( :first, :conditions=>[ 'full_path = ?', path ] ) end protected # Creates a URI path based on the Page tree def create_full_path if parent_node = self.parent # Build URI Path path = "#{parent_node.full_path}/#{self.slug}" # strip leading space, if there is one... path = path[1..-1] if path.starts_with? "/" self.full_path = path || "" else # I'm the root -- My path is blank self.full_path = "" end end def create_full_path! create_full_path save end # Caches old path (before save) for comparison later def cache_full_path @old_full_path = self.full_path end # Updates all this content's child URI paths def update_children_full_path(should_save=true) # OPTIMIZE: Only update all the children if the :slug/:fullpath is different for child in self.children child.create_full_path! unless child.frozen? child.update_children_full_path(should_save) end end end end