app/models/page.rb in puffer_pages-0.0.2 vs app/models/page.rb in puffer_pages-0.0.3

- old
+ new

@@ -1,8 +1,5 @@ -class LayoutError < StandardError -end - class Page < ActiveRecord::Base acts_as_nested_set scope :with_parts, includes(:page_parts) @@ -18,26 +15,28 @@ end def self.find_page location page = Page.find_by_location location raise ActiveRecord::RecordNotFound if page.nil? || page.draft? + page end - has_many :page_parts, :order => "name = '#{PufferPages::MAIN_PART}' desc, name", :dependent => :destroy + has_many :page_parts, :order => "name = '#{PufferPages.primary_page_part_name}' desc, name", :dependent => :destroy + accepts_nested_attributes_for :page_parts, :allow_destroy => true belongs_to :layout, :primary_key => :name, :foreign_key => :layout_name validates_presence_of :name validate do |page| page.errors.add(:layout_name, :blank) unless page.inherited_layout_name.present? end validates_uniqueness_of :slug, :scope => :parent_id validates_format_of :slug, - :with => /\A([a-zA-Z0-9]+[\w-]*(\.[a-zA-Z0-9]+)?|\*)\Z/, + :with => /\A([\w]+[\w-]*(\.[\w]+)?|\*)\Z/, :message => :slug_format, :unless => :root? validates_format_of :slug, - :with => /\A\/\Z/, + :with => /\A\Z/, :message => :root_slug_format, :if => :root? validates_inclusion_of :status, :in => Page.statuses attr_protected :location @@ -47,21 +46,21 @@ self.status ||= 'draft' end before_save :update_location def update_location - self.location = [(swallow_nil{parent.location} || '/'), slug].compact.join('/').gsub(/\/+/, '/') + self.location = [swallow_nil{parent.location}, slug].compact.join('/').presence end before_update :update_locations def update_locations Page.update_all "location = replace(location, '#{location_was}', '#{location}')", ["location like ?", location_was + '%'] if location_changed? end after_save :create_main_part def create_main_part - page_parts.create(:name => PufferPages::MAIN_PART) if root? + page_parts.create(:name => PufferPages.primary_page_part_name) if root? end statuses.each do |status_name| define_method "#{status_name}?" do status == status_name end end @@ -94,10 +93,10 @@ def render_layout inherited_layout ? false : inherited_layout_name end def inherited_page_parts - PagePart.where(:page_id => self_and_ancestors.map(&:id).reverse).group('name').order("name = '#{PufferPages::MAIN_PART}' desc, name") + PagePart.where(:page_id => self_and_ancestors.map(&:id).reverse).group('name').order("name = '#{PufferPages.primary_page_part_name}' desc, name") end def part name inherited_page_parts.detect {|part| part.name = name} end