From 21c5e8a6270d7a00a9d4accc9d09969cb6f31b55 Mon Sep 17 00:00:00 2001 From: Didier Lafforgue Date: Thu, 29 Mar 2012 16:17:09 +0200 Subject: [PATCH] small optimization when retrieving a page from a path --- app/models/locomotive/extensions/page/render.rb | 5 +++-- features/step_definitions/pagination_steps.rb | 2 +- features/step_definitions/relationships_steps.rb | 2 +- spec/lib/locomotive/render_spec.rb | 16 ++++++++-------- 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/app/models/locomotive/extensions/page/render.rb b/app/models/locomotive/extensions/page/render.rb index 0a5dc114..91373df0 100644 --- a/app/models/locomotive/extensions/page/render.rb +++ b/app/models/locomotive/extensions/page/render.rb @@ -24,11 +24,12 @@ module Locomotive # @return [ Page ] The page matching the criteria OR the 404 one if none # 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) - 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 next else diff --git a/features/step_definitions/pagination_steps.rb b/features/step_definitions/pagination_steps.rb index a16cf3d5..d5b52c98 100644 --- a/features/step_definitions/pagination_steps.rb +++ b/features/step_definitions/pagination_steps.rb @@ -18,7 +18,7 @@ Then /^I should be able to display paginated models$/ do {{ paginate | default_pagination }} {% 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 visit '/hello' diff --git a/features/step_definitions/relationships_steps.rb b/features/step_definitions/relationships_steps.rb index 48e41700..22361b57 100644 --- a/features/step_definitions/relationships_steps.rb +++ b/features/step_definitions/relationships_steps.rb @@ -90,7 +90,7 @@ Then /^I should be able to view a paginated list of a has many association$/ do } # 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 visit '/hello' diff --git a/spec/lib/locomotive/render_spec.rb b/spec/lib/locomotive/render_spec.rb index 1bcf2702..47665725 100644 --- a/spec/lib/locomotive/render_spec.rb +++ b/spec/lib/locomotive/render_spec.rb @@ -83,25 +83,25 @@ describe 'Locomotive rendering system' do it 'should retrieve the index page /' do @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 end it 'should also retrieve the index page (index.html)' do @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 end it 'should retrieve it based on the full path' do @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 end it 'does not include the query string' do @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 end @@ -118,7 +118,7 @@ describe 'Locomotive rendering system' do @page.redirect = true @page.redirect_url = 'http://www.example.com/' @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 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(:fullpath).returns('/projects/content_type_template') @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 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 @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]) @controller.current_site.pages.expects(:not_found).returns(klass) @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 @controller.current_locomotive_account = true @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 end