Skip to content

Instantly share code, notes, and snippets.

@jess
Last active December 10, 2015 12:29
Show Gist options
  • Save jess/4434676 to your computer and use it in GitHub Desktop.
Save jess/4434676 to your computer and use it in GitHub Desktop.
# /app/decorators/controllers/refinery/pages/pages_controller_decorator.rb
Refinery::PagesController.class_eval do
def show
if current_user_can_view_page?
if should_skip_to_first_child?
redirect_to refinery.url_for(first_live_child.url)
elsif page.link_url.present? && redirect_url?
redirect_to page.link_url
else
if requested_friendly_id != page.friendly_id && redirect_url?
redirect_to refinery.url_for(page.url), :status => 301
else
render_with_templates?
end
end
else
error_404
end
end
def redirect_url?
page.link_url[0] == "/"
end
def find_page(fallback_to_404 = true)
@page ||= case action_name
when "home"
Refinery::Page.where(:link_url => '/').first
when "show"
Refinery::Page.find_by_path_or_id(params[:path], params[:id]) || Refinery::Page.where(:link_url => "#{params[:path]}").first
end
@page || (error_404 if fallback_to_404)
end
end
@parndt
Copy link

parndt commented Jan 6, 2013

If link_url is nil you'll get the following:

nil[0]
NoMethodError: undefined method `[]' on nil:NilClass.

Why not just put the page.link_url.present? check inside redirect_url?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment