Skip to content

Commit

Permalink
Improved performance in menu rendering when there are very many pages…
Browse files Browse the repository at this point in the history
… by first querying the translations table and then using those results in the pages table rather than the other way around. There are still too many queries in the logfile for my liking so this has a way to come.
  • Loading branch information
parndt committed Mar 14, 2011
1 parent 2b28909 commit 75cdc74
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 10 deletions.
3 changes: 1 addition & 2 deletions core/app/views/shared/_menu.html.erb
Expand Up @@ -11,15 +11,14 @@
selected_item = collection.detect{|page| selected_page?(page)}
selected_item = @page if selected_item.nil?
end
sibling_count = roots.length - 1
-%>
<nav id='<%= dom_id %>' class='<%= %W(#{css} clearfix).join(' ') %>'>
<ul>
<%= render :partial => "/shared/menu_branch",
:collection => roots,
:locals => {
:hide_children => hide_children,
:sibling_count => sibling_count,
:sibling_count => (roots.length - 1),
:collection => collection,
:selected_item => selected_item,
:apply_css => true #if you don't care about class='first' class='last' or class='selected' set apply_css to false for speed.
Expand Down
6 changes: 5 additions & 1 deletion core/app/views/shared/_menu_branch.html.erb
Expand Up @@ -5,7 +5,11 @@
dom_id = ("id='item_#{menu_branch_counter}'" if menu_branch.parent_id.nil? and menu_branch.title.present?)

hide_children = (defined?(hide_children) && hide_children)
children = (hide_children || !menu_branch.has_descendants?) ? [] : collection.select { |p| p.parent_id == menu_branch.id }
children = if (hide_children || !menu_branch.has_descendants?)
[]
else
collection.select { |p| p.parent_id == menu_branch.id }
end
-%>
<li<%= ['', css, dom_id].compact.join(' ').gsub(/\ *$/, '') %>>
<%= link_to menu_branch.title, menu_branch.url -%>
Expand Down
2 changes: 1 addition & 1 deletion core/lib/refinery/application_controller.rb
Expand Up @@ -87,7 +87,7 @@ def login?

# get all the pages to be displayed in the site menu.
def find_pages_for_menu
@menu_pages = Page.live.in_menu.order('lft ASC')
@menu_pages = Page.in_menu.live.order('lft ASC')
end

# use a different model for the meta information.
Expand Down
12 changes: 6 additions & 6 deletions pages/app/models/page.rb
Expand Up @@ -32,14 +32,14 @@ class Page < ActiveRecord::Base

scope :live, where(:draft => false)

# shows all pages with :show_in_menu set to true, but it also
# Shows all pages with :show_in_menu set to true, but it also
# rejects any page that has not been translated to the current locale.
# This works using a query against the translated content first and then
# using all of the page_ids we further filter against this model's table.
scope :in_menu, lambda {
pages = Arel::Table.new(Page.table_name)
translations = Arel::Table.new(Page.translations_table_name)

includes(:translations).where(:show_in_menu => true).where(
translations[:locale].eq(Globalize.locale)).where(pages[:id].eq(translations[:page_id]))
includes(:translations).where(Page.arel_table[:id].in(
Page::Translation.where(:locale => Globalize.locale).map(&:page_id)
)).where(:show_in_menu => true)
}

# when a dialog pops up to link to a page, how many pages per page should there be
Expand Down

0 comments on commit 75cdc74

Please sign in to comment.