Skip to content

Commit

Permalink
Add fixes to page model.
Browse files Browse the repository at this point in the history
- Check if title exists before generates slug.
- Check if the page is persisted before rebuild its path.
  • Loading branch information
Luis Mendo committed Sep 10, 2015
1 parent b708fdf commit 03dfc6f
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions app/models/no_cms/pages/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,18 @@ def self.home

def set_slug_and_path
self.slug = title.parameterize if slug.nil? && !title.nil? # If there's no slug then we create it
self.slug = title.parameterize if slug.blank? && !parent.nil? # If slug is blank and this page has a parent then we recreate it
self.slug = title.parameterize if slug.blank? && Page.home && (Page.home != self) # If slug is blank and there's already a home (and it's another page) then we recreate it
self.slug = title.parameterize if slug.blank? && !title.nil? && !parent.nil? # If slug is blank and this page has a parent then we recreate it
self.slug = title.parameterize if slug.blank? && !title.nil? && Page.home && (Page.home != self) # If slug is blank and there's already a home (and it's another page) then we recreate it
self.rebuild_path if path.nil? || attribute_changed?('slug')
end

def rebuild_path
self.update_attribute :path, "#{parent.path unless parent.nil?}/#{slug}"
descendants.each(&:rebuild_path)
if self.persisted?
self.update_attribute :path, "#{parent.path unless parent.nil?}/#{slug}"
descendants.each(&:rebuild_path)
else
self.path = "#{parent.path unless parent.nil?}/#{slug}"
end
end

def self.templates
Expand Down

0 comments on commit 03dfc6f

Please sign in to comment.