Skip to content

Commit

Permalink
Allow finders to be defined for each action.
Browse files Browse the repository at this point in the history
Per #3119 this will allow one to define a method such as:

    def find_page_for_about_us
      # logic for finding a page
    end

The advantage of this is that it allows any number of custom actions
and somewhat removes the hardcoded nature of home and show.
  • Loading branch information
parndt committed Jun 1, 2016
1 parent 8ebff51 commit b58d31e
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions pages/app/controllers/refinery/pages_controller.rb
Expand Up @@ -3,7 +3,8 @@ class PagesController < ::ApplicationController
include Pages::RenderOptions

before_action :find_page, :set_canonical
before_action :error_404, :unless => :current_user_can_view_page?
before_action :error_404, if: :action_has_page_finder?,
unless: :current_user_can_view_page?

# Save whole Page after delivery
after_action :write_cache?
Expand Down Expand Up @@ -63,22 +64,29 @@ def current_user_can_view_page?
page.live? || authorisation_manager.allow?(:plugin, "refinery_pages")
end

def action_has_page_finder?
self.class.instance_methods(false).include? action_page_finder
end

def first_live_child
page.children.order('lft ASC').live.first
end

def find_page(fallback_to_404 = true)
@page ||= case action_name
when "home"
Refinery::Page.find_by(link_url: '/')
when "show"
Refinery::Page.friendly.find_by_path_or_id(params[:path], params[:id])
end
@page ||= send(action_page_finder) if action_has_page_finder?
@page || (error_404 if fallback_to_404)
end

alias_method :page, :find_page

def find_page_for_home
Refinery::Page.find_by link_url: '/'
end

def find_page_for_show
Refinery::Page.friendly.find_by_path_or_id params[:path], params[:id]
end

def set_canonical
@canonical = refinery.url_for @page.canonical if @page.present?
end
Expand All @@ -89,5 +97,10 @@ def write_cache?
cache_page(response.body, File.join('', 'refinery', 'cache', 'pages', request.path).to_s)
end
end

private
def action_page_finder
:"find_page_for_#{action_name}"
end
end
end

0 comments on commit b58d31e

Please sign in to comment.