Skip to content

Commit

Permalink
Trying to do the same job with less overhead.
Browse files Browse the repository at this point in the history
  • Loading branch information
parndt committed May 25, 2011
1 parent fe62d9a commit 7f6aae6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 36 deletions.
46 changes: 26 additions & 20 deletions pages/app/models/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ def translation
:approximate_ascii => RefinerySetting.find_or_set(:approximate_ascii, false, :scoping => "pages"),
:strip_non_ascii => RefinerySetting.find_or_set(:strip_non_ascii, false, :scoping => "pages")

def to_param_cache
slug.try(:name)
end

def to_param
to_param_cache || super
end

has_many :parts,
:class_name => "PagePart",
:order => "position ASC",
Expand Down Expand Up @@ -129,19 +137,17 @@ def reposition_parts!
# Before destroying a page we check to see if it's a deletable page or not
# Refinery system pages are not deletable.
def destroy
if deletable?
super
else
unless Rails.env.test?
# give useful feedback when trying to delete from console
puts "This page is not deletable. Please use .destroy! if you really want it deleted "
puts "unset .link_url," if link_url.present?
puts "unset .menu_match," if menu_match.present?
puts "set .deletable to true" unless deletable
end

return false
return super if deletable?

unless Rails.env.test?
# give useful feedback when trying to delete from console
puts "This page is not deletable. Please use .destroy! if you really want it deleted "
puts "unset .link_url," if link_url.present?
puts "unset .menu_match," if menu_match.present?
puts "set .deletable to true" unless deletable
end

return false
end

# If you want to destroy a page that is set to be not deletable this is the way to do it.
Expand Down Expand Up @@ -224,7 +230,7 @@ def nested_url
end

def uncached_nested_url
[(parent.nested_url unless parent_id.nil?), to_param].compact.flatten
[parent.try(:nested_url), to_param].compact.flatten
end

# Returns the string version of nested_url, i.e., the path that should be generated
Expand Down Expand Up @@ -272,14 +278,14 @@ def shown_siblings

def to_refinery_menu_item
{
:id => self.id,
:lft => self.lft,
:menu_match => self.menu_match,
:parent_id => self.parent_id,
:rgt => self.rgt,
:title => self.page_title || self.title,
:id => id,
:lft => lft,
:menu_match => menu_match,
:parent_id => parent_id,
:rgt => rgt,
:title => (page_title if respond_to?(:page_title)) || title,
:type => self.class.name,
:url => self.url
:url => url
}
end

Expand Down
20 changes: 4 additions & 16 deletions pages/lib/refinery/pages/instance_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ def error_404(exception=nil)
protected
def find_pages_for_menu
# First, apply a filter to determine which pages to show.
pages = ::Page.live.in_menu
# We need to join to the page's slug to avoid multiple queries.
pages = ::Page.live.in_menu.includes(:slug)

# Now we only want to select particular columns to avoid any further queries.
# Title is retrieved in the next block below so it's not here.
Expand All @@ -31,21 +32,8 @@ def find_pages_for_menu
pages = pages.select("title as page_title")
end

# Cache the slug's name for the menu url.
pages = pages.joins(:slug).select("`#{Slug.table_name}`.`name` AS to_param_cache")

# Set the slug to use to_param_cache
current_cache_column = ::Page.friendly_id_config.cache_column
::Page.friendly_id_config.cache_column = 'to_param_cache'

# Compile the menu.
@menu_pages = ::Refinery::Menu.new(pages.map(&:to_refinery_menu_item))

# Now we can set it back
::Page.friendly_id_config.cache_column = current_cache_column

# Return @menu_pages.
@menu_pages
# Compile the menu
@menu_pages = ::Refinery::Menu.new(pages)
end

def render(*args)
Expand Down

0 comments on commit 7f6aae6

Please sign in to comment.