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

View File

@@ -35,11 +35,7 @@ class ArtistsController < ApplicationController
@artists = @artists.includes(:tag) if request.format.html?
@artists = @artists.includes(:urls) if !request.format.html?
if params[:redirect].to_s.truthy? && @artists.one? && @artists.first.name == Artist.normalize_name(params[:search][:any_name_or_url_matches])
redirect_to @artists.first
else
respond_with @artists
end
respond_with(@artists)
end
def show
@@ -85,6 +81,14 @@ class ArtistsController < ApplicationController
private
def item_matches_params(artist)
if params[:search][:any_name_or_url_matches]
artist.name == Artist.normalize_name(params[:search][:any_name_or_url_matches])
else
true
end
end
def load_artist
@artist = Artist.find(params[:id])
end

View File

@@ -19,11 +19,7 @@ class PoolsController < ApplicationController
def index
@pools = Pool.includes(:creator).paginated_search(params, count_pages: true)
if params[:redirect].to_s.truthy? && @pools.one? && Pool.normalize_name_for_search(@pools.first.name) == Pool.normalize_name_for_search(params[:search][:name_matches])
redirect_to @pools.first
else
respond_with @pools
end
respond_with(@pools)
end
def gallery
@@ -94,6 +90,14 @@ class PoolsController < ApplicationController
private
def item_matches_params(pool)
if params[:search][:name_matches]
Pool.normalize_name_for_search(pool.name) == Pool.normalize_name_for_search(params[:search][:name_matches])
else
true
end
end
def pool_params
permitted_params = %i[name description category post_ids post_ids_string]
params.require(:pool).permit(*permitted_params, post_ids: [])

View File

@@ -32,11 +32,7 @@ class UsersController < ApplicationController
end
@users = User.paginated_search(params)
if params[:redirect].to_s.truthy? && @users.one? && User.normalize_name(@users.first.name) == User.normalize_name(params[:search][:name_matches])
redirect_to @users.first
else
respond_with @users
end
respond_with(@users)
end
def search
@@ -98,6 +94,14 @@ class UsersController < ApplicationController
private
def item_matches_params(user)
if params[:search][:name_matches]
User.normalize_name(user.name) == User.normalize_name(params[:search][:name_matches])
else
true
end
end
def check_privilege(user)
raise User::PrivilegeError unless user.id == CurrentUser.id || CurrentUser.is_admin?
end

View File

@@ -17,11 +17,7 @@ class WikiPagesController < ApplicationController
def index
@wiki_pages = WikiPage.paginated_search(params)
if params[:redirect].to_s.truthy? && @wiki_pages.one? && @wiki_pages.first.title == WikiPage.normalize_title(params[:search][:title])
redirect_to @wiki_pages.first
else
respond_with(@wiki_pages)
end
respond_with(@wiki_pages)
end
def search
@@ -80,6 +76,14 @@ class WikiPagesController < ApplicationController
private
def item_matches_params(wiki_page)
if params[:search][:title]
wiki_page.title == WikiPage.normalize_title(params[:search][:title])
else
true
end
end
def normalize_search_params
if params[:title]
params[:search] ||= {}