small optimization when retrieving a page from a path

This commit is contained in:
Didier Lafforgue 2012-03-29 16:17:09 +02:00
parent 7877f11669
commit 21c5e8a627
4 changed files with 13 additions and 12 deletions

View File

@ -24,11 +24,12 @@ module Locomotive
# @return [ Page ] The page matching the criteria OR the 404 one if none # @return [ Page ] The page matching the criteria OR the 404 one if none
# #
def fetch_page_from_path(site, path, logged_in) def fetch_page_from_path(site, path, logged_in)
page = nil page = nil
depth = path == 'index' ? 0 : path.split('/').size
matching_paths = path == 'index' ? %w(index) : path_combinations(path) matching_paths = path == 'index' ? %w(index) : path_combinations(path)
site.pages.any_in(:fullpath => matching_paths).each do |_page| site.pages.where(:depth => depth, :fullpath.in => matching_paths).each do |_page|
if !_page.published? && !logged_in if !_page.published? && !logged_in
next next
else else

View File

@ -18,7 +18,7 @@ Then /^I should be able to display paginated models$/ do
{{ paginate | default_pagination }} {{ paginate | default_pagination }}
{% endpaginate %} {% endpaginate %}
} }
FactoryGirl.create(:page, :site => @site, :slug => 'hello', :raw_template => raw_template) FactoryGirl.create(:page, :site => @site, :slug => 'hello', :parent => @site.pages.root.first, :raw_template => raw_template)
# The page should have the first two articles # The page should have the first two articles
visit '/hello' visit '/hello'

View File

@ -90,7 +90,7 @@ Then /^I should be able to view a paginated list of a has many association$/ do
} }
# Create a page # Create a page
FactoryGirl.create(:page, :site => @site, :slug => 'hello', :raw_template => raw_template) FactoryGirl.create(:page, :site => @site, :slug => 'hello', :parent => @site.pages.root.first, :raw_template => raw_template)
# The page should have the first two comments # The page should have the first two comments
visit '/hello' visit '/hello'

View File

@ -83,25 +83,25 @@ describe 'Locomotive rendering system' do
it 'should retrieve the index page /' do it 'should retrieve the index page /' do
@controller.request.fullpath = '/' @controller.request.fullpath = '/'
@controller.current_site.pages.expects(:any_in).with({ :fullpath => %w{index} }).returns([@page]) @controller.current_site.pages.expects(:where).with(:depth => 0, :fullpath.in => %w{index}).returns([@page])
@controller.send(:locomotive_page).should_not be_nil @controller.send(:locomotive_page).should_not be_nil
end end
it 'should also retrieve the index page (index.html)' do it 'should also retrieve the index page (index.html)' do
@controller.request.fullpath = '/index.html' @controller.request.fullpath = '/index.html'
@controller.current_site.pages.expects(:any_in).with({ :fullpath => %w{index} }).returns([@page]) @controller.current_site.pages.expects(:where).with(:depth => 0, :fullpath.in => %w{index}).returns([@page])
@controller.send(:locomotive_page).should_not be_nil @controller.send(:locomotive_page).should_not be_nil
end end
it 'should retrieve it based on the full path' do it 'should retrieve it based on the full path' do
@controller.request.fullpath = '/about_us/team.html' @controller.request.fullpath = '/about_us/team.html'
@controller.current_site.pages.expects(:any_in).with({ :fullpath => %w{about_us/team about_us/content_type_template content_type_template/team} }).returns([@page]) @controller.current_site.pages.expects(:where).with(:depth => 2, :fullpath.in => %w{about_us/team about_us/content_type_template content_type_template/team}).returns([@page])
@controller.send(:locomotive_page).should_not be_nil @controller.send(:locomotive_page).should_not be_nil
end end
it 'does not include the query string' do it 'does not include the query string' do
@controller.request.fullpath = '/about_us/team.html?some=params&we=use' @controller.request.fullpath = '/about_us/team.html?some=params&we=use'
@controller.current_site.pages.expects(:any_in).with({ :fullpath => %w{about_us/team about_us/content_type_template content_type_template/team} }).returns([@page]) @controller.current_site.pages.expects(:where).with(:depth => 2, :fullpath.in => %w{about_us/team about_us/content_type_template content_type_template/team}).returns([@page])
@controller.send(:locomotive_page).should_not be_nil @controller.send(:locomotive_page).should_not be_nil
end end
@ -118,7 +118,7 @@ describe 'Locomotive rendering system' do
@page.redirect = true @page.redirect = true
@page.redirect_url = 'http://www.example.com/' @page.redirect_url = 'http://www.example.com/'
@controller.request.fullpath = '/contact' @controller.request.fullpath = '/contact'
@controller.current_site.pages.expects(:any_in).with({ :fullpath => %w{contact content_type_template} }).returns([@page]) @controller.current_site.pages.expects(:where).with(:depth => 1, :fullpath.in => %w{contact content_type_template}).returns([@page])
end end
it 'redirects to the redirect_url' do it 'redirects to the redirect_url' do
@ -137,7 +137,7 @@ describe 'Locomotive rendering system' do
@page.stubs(:fetch_target_entry).returns(@content_entry) @page.stubs(:fetch_target_entry).returns(@content_entry)
@page.stubs(:fullpath).returns('/projects/content_type_template') @page.stubs(:fullpath).returns('/projects/content_type_template')
@controller.request.fullpath = '/projects/edeneo.html' @controller.request.fullpath = '/projects/edeneo.html'
@controller.current_site.pages.expects(:any_in).with({ :fullpath => %w{projects/edeneo projects/content_type_template content_type_template/edeneo} }).returns([@page]) @controller.current_site.pages.expects(:where).with(:depth => 2, :fullpath.in => %w{projects/edeneo projects/content_type_template content_type_template/edeneo}).returns([@page])
end end
it 'sets the content_entry variable' do it 'sets the content_entry variable' do
@ -172,7 +172,7 @@ describe 'Locomotive rendering system' do
it 'should return the 404 page if the page has not been published yet' do it 'should return the 404 page if the page has not been published yet' do
@controller.request.fullpath = '/contact' @controller.request.fullpath = '/contact'
@controller.current_site.pages.expects(:any_in).with({ :fullpath => %w{contact content_type_template} }).returns([@page]) @controller.current_site.pages.expects(:where).with(:depth => 1, :fullpath.in => %w{contact content_type_template}).returns([@page])
(klass = Locomotive::Page).expects(:published).returns([true]) (klass = Locomotive::Page).expects(:published).returns([true])
@controller.current_site.pages.expects(:not_found).returns(klass) @controller.current_site.pages.expects(:not_found).returns(klass)
@controller.send(:locomotive_page).should be_true @controller.send(:locomotive_page).should be_true
@ -181,7 +181,7 @@ describe 'Locomotive rendering system' do
it 'should not return the 404 page if the page has not been published yet and admin is logged in' do it 'should not return the 404 page if the page has not been published yet and admin is logged in' do
@controller.current_locomotive_account = true @controller.current_locomotive_account = true
@controller.request.fullpath = '/contact' @controller.request.fullpath = '/contact'
@controller.current_site.pages.expects(:any_in).with({ :fullpath => %w{contact content_type_template} }).returns([@page]) @controller.current_site.pages.expects(:where).with(:depth => 1, :fullpath.in => %w{contact content_type_template}).returns([@page])
@controller.send(:locomotive_page).should == @page @controller.send(:locomotive_page).should == @page
end end