Added universal redirect on the index action

- Only controllers with show actions will redirect on the index action
- Parameter checking is individualized per controller for the redirect check
This commit is contained in:
BrokenEagle
2020-01-25 20:28:08 +00:00
parent c1f2cd8d9d
commit 75ac11166c
5 changed files with 58 additions and 20 deletions

View File

@@ -22,6 +22,28 @@ class ApplicationController < ActionController::Base
end
end
private
def respond_with(*options, &block)
if params[:action] == "index" && is_redirect?(options[0])
redirect_to_show(options[0])
else
super(*options, &block)
end
end
def redirect_to_show(items)
redirect_to send("#{controller_path.singularize}_path", items.first, format: request.format.symbol)
end
def is_redirect?(items)
action_methods.include?("show") && params[:redirect].to_s.truthy? && items.one? && item_matches_params(items.first)
end
def item_matches_params(*)
true
end
protected
def enable_cors