Skip to content
Browse files

Merge pull request #1584 from Subsis/master

Fixes page ambiguity for different pages with the same slug in find_by_path.
  • Loading branch information...
2 parents 6bc0c98 + 970f9fc commit 1056d6c94795b8b7eeeabe577021b954faa8b513 @ugisozols ugisozols committed Apr 10, 2012
Showing with 23 additions and 4 deletions.
  1. +4 −4 pages/app/models/refinery/page.rb
  2. +19 −0 pages/spec/models/refinery/page_spec.rb
View
8 pages/app/models/refinery/page.rb
@@ -74,8 +74,8 @@ def live
# For example with about/example we would need to find 'about' and then its child
# called 'example' otherwise it may clash with another page called /example.
def find_by_path(path)
- split_path = path.to_s.split('/')
- page = ::Refinery::Page.by_slug(split_path.shift).first
+ split_path = path.to_s.split('/').reject(&:blank?)
+ page = ::Refinery::Page.by_slug(split_path.shift, :parent_id => nil).first
page = page.children.by_slug(split_path.shift).first until page.nil? || split_path.empty?
page
@@ -105,9 +105,9 @@ def by_title(title)
end
# Finds a page using its slug. See by_title
- def by_slug(slug)
+ def by_slug(slug, conditions={})
locales = Refinery.i18n_enabled? ? Refinery::I18n.frontend_locales : ::I18n.locale
- with_globalize(:locale => locales, :slug => slug)
+ with_globalize({ :locale => locales, :slug => slug }.merge(conditions))
end
# Shows all pages with :show_in_menu set to true, but it also
View
19 pages/spec/models/refinery/page_spec.rb
@@ -418,5 +418,24 @@ def turn_on_marketable_urls
end
end
+ describe '.find_by_path' do
+ let(:page_title) { 'team' }
+ let(:child_title) { 'about' }
+ let(:created_root_about) { subject.class.create!(:title => child_title, :deletable => true) }
+
+ before(:each) do
+ # Ensure pages are created.
+ created_child
+ created_root_about
+ end
+
+ it "should return (root) about page when looking for '/about'" do
+ Refinery::Page.find_by_path('/about').should == created_root_about
+ end
+
+ it "should return child about page when looking for '/team/about'" do
+ Refinery::Page.find_by_path('/team/about').should == created_child
+ end
+ end
end
end

0 comments on commit 1056d6c

Please sign in to comment.
Something went wrong with that request. Please try again.