Skip to content

Commit

Permalink
Added support for @canonical in pages_controller.
Browse files Browse the repository at this point in the history
Fixes refinery#1472

Conflicts:

	pages/app/models/refinery/page.rb
  • Loading branch information
parndt committed May 25, 2012
1 parent 33878c5 commit bcec0bb
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 18 deletions.
6 changes: 3 additions & 3 deletions core/app/views/refinery/_head.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
<meta charset='<%= Rails.application.config.encoding %>' />
<!--[if IE]><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" /><![endif]-->
<title><%= browser_title(yield(:title)) %></title>
<%= raw(%(<meta name="description" content="#{@meta.meta_description}" />)) if @meta.meta_description.present? -%>
<%= raw(%(<meta name="keywords" content="#{@meta.meta_keywords}">)) if @meta.meta_keywords.present? -%>
<%= raw(%(<link rel="canonical" content="#{@canonical}" />)) if @canonical.present? -%>
<%= raw %(<meta name="description" content="#{@meta.meta_description}" />) if @meta.meta_description.present? -%>
<%= raw %(<meta name="keywords" content="#{@meta.meta_keywords}">) if @meta.meta_keywords.present? -%>
<%= raw %(<link rel="canonical" content="#{@canonical}" />) if @canonical.present? -%>
<%= csrf_meta_tags if Refinery::Core.authenticity_token_on_frontend -%>
<%= yield :meta %>

Expand Down
6 changes: 5 additions & 1 deletion pages/app/controllers/refinery/pages_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Refinery
class PagesController < ::ApplicationController
before_filter :find_page, :except => [:preview]
before_filter :find_page, :set_canonical, :except => [:preview]

# Save whole Page after delivery
after_filter { |c| c.write_cache? }
Expand Down Expand Up @@ -94,6 +94,10 @@ def render_with_templates?(render_options = {})
render render_options if render_options.any?
end

def set_canonical
@canonical = refinery.url_for @page.canonical if @page.present?
end

def write_cache?
if Refinery::Pages.cache_pages_full && !refinery_user?
cache_page(response.body, File.join('', 'refinery', 'cache', 'pages', request.path.sub("//", "/")).to_s)
Expand Down
32 changes: 18 additions & 14 deletions pages/app/models/refinery/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def different_frontend_locale?

# Returns how many pages per page should there be when paginating pages
def per_page(dialog = false)
dialog ? Pages.pages_per_dialog : Pages.config.pages_per_admin_index
dialog ? Pages.pages_per_dialog : Pages.pages_per_admin_index
end

def expire_page_caching
Expand All @@ -159,10 +159,17 @@ def expire_page_caching
end
end

# The canonical page for this particular page.
# Consists of:
# * The default locale's translated slug
def canonical
with_locale_param url_marketable, ::Refinery::I18n.default_frontend_locale
end

# Returns in cascading order: custom_slug or menu_title or title depending on
# which attribute is first found to be present for this page.
def custom_slug_or_title
custom_slug.presence || menu_title.presence || title.presence
custom_slug.presence || menu_title.presence || title
end

# Am I allowed to delete this page?
Expand Down Expand Up @@ -256,7 +263,7 @@ def link_url_localised?
# For example, this might evaluate to /about for the "About" page.
def url_marketable
# :id => nil is important to prevent any other params[:id] from interfering with this route.
url_normal.merge(:path => nested_url, :id => nil)
url_normal.merge :path => nested_url, :id => nil
end

# Returns a url suitable to be used in url_for in Rails (such as link_to).
Expand All @@ -267,26 +274,23 @@ def url_normal

# If the current locale is set to something other than the default locale
# then the :locale attribute will be set on the url hash, otherwise it won't be.
def with_locale_param(url_hash)
if self.class.different_frontend_locale?
url_hash.update(:locale => (::Refinery::I18n.current_frontend_locale if Refinery.i18n_enabled?))
end
def with_locale_param(url_hash, locale = nil)
locale ||= ::Refinery::I18n.current_frontend_locale if self.class.different_frontend_locale?
url_hash.update :locale => locale if locale
url_hash
end

def uncached_nested_url
[parent.try(:uncached_nested_url), to_param.to_s].compact.flatten
end

# Returns an array with all ancestors to_param, allow with its own
# Ex: with an About page and a Mission underneath,
# ::Refinery::Page.find('mission').nested_url would return:
#
# ['about', 'mission']
#
def nested_url
Rails.cache.fetch(url_cache_key) { uncached_nested_url }
end

def uncached_nested_url
[parent.try(:nested_url), to_param.to_s].compact.flatten
end
alias_method :nested_url, :uncached_nested_url

# Returns the string version of nested_url, i.e., the path that should be generated
# by the router
Expand Down
9 changes: 9 additions & 0 deletions pages/spec/models/refinery/page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,15 @@ def turn_on_marketable_urls
end
end

describe '#canonical' do
let(:page_title) { 'team' }
let(:child_title) { 'about' }

specify '' do

end
end

context 'custom slugs' do
let(:custom_page_slug) { 'custom-page-slug' }
let(:custom_child_slug) { 'custom-child-slug' }
Expand Down

0 comments on commit bcec0bb

Please sign in to comment.