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 class such as:

    module Finders
      class Show
        def call(params)
          # logic for finding a page
        end
      end
    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 Jul 15, 2016
1 parent a242d12 commit aa8c4e6
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions pages/app/controllers/refinery/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,7 @@ def first_live_child
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 ||= action_page_finder.call(params) if action_has_page_finder?
@page || (error_404 if fallback_to_404)
end

Expand All @@ -89,5 +84,28 @@ def write_cache?
cache_page(response.body, File.join('', 'refinery', 'cache', 'pages', request.path).to_s)
end
end

private
def action_has_page_finder?
Finders.const_defined? action_name.classify
end

def action_page_finder
Finders.const_get action_name.classify
end

module Finders
class Home
def self.call(_params)
Refinery::Page.find_by link_url: "/"
end
end

class Show
def self.call(params)
Refinery::Page.friendly.find_by_path_or_id params[:path], params[:id]
end
end
end
end
end

0 comments on commit aa8c4e6

Please sign in to comment.