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:
@@ -22,6 +22,28 @@ class ApplicationController < ActionController::Base
|
|||||||
end
|
end
|
||||||
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
|
protected
|
||||||
|
|
||||||
def enable_cors
|
def enable_cors
|
||||||
|
|||||||
@@ -35,11 +35,7 @@ class ArtistsController < ApplicationController
|
|||||||
@artists = @artists.includes(:tag) if request.format.html?
|
@artists = @artists.includes(:tag) if request.format.html?
|
||||||
@artists = @artists.includes(:urls) 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])
|
respond_with(@artists)
|
||||||
redirect_to @artists.first
|
|
||||||
else
|
|
||||||
respond_with @artists
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def show
|
def show
|
||||||
@@ -85,6 +81,14 @@ class ArtistsController < ApplicationController
|
|||||||
|
|
||||||
private
|
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
|
def load_artist
|
||||||
@artist = Artist.find(params[:id])
|
@artist = Artist.find(params[:id])
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -19,11 +19,7 @@ class PoolsController < ApplicationController
|
|||||||
def index
|
def index
|
||||||
@pools = Pool.includes(:creator).paginated_search(params, count_pages: true)
|
@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])
|
respond_with(@pools)
|
||||||
redirect_to @pools.first
|
|
||||||
else
|
|
||||||
respond_with @pools
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def gallery
|
def gallery
|
||||||
@@ -94,6 +90,14 @@ class PoolsController < ApplicationController
|
|||||||
|
|
||||||
private
|
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
|
def pool_params
|
||||||
permitted_params = %i[name description category post_ids post_ids_string]
|
permitted_params = %i[name description category post_ids post_ids_string]
|
||||||
params.require(:pool).permit(*permitted_params, post_ids: [])
|
params.require(:pool).permit(*permitted_params, post_ids: [])
|
||||||
|
|||||||
@@ -32,11 +32,7 @@ class UsersController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
@users = User.paginated_search(params)
|
@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])
|
respond_with(@users)
|
||||||
redirect_to @users.first
|
|
||||||
else
|
|
||||||
respond_with @users
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def search
|
def search
|
||||||
@@ -98,6 +94,14 @@ class UsersController < ApplicationController
|
|||||||
|
|
||||||
private
|
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)
|
def check_privilege(user)
|
||||||
raise User::PrivilegeError unless user.id == CurrentUser.id || CurrentUser.is_admin?
|
raise User::PrivilegeError unless user.id == CurrentUser.id || CurrentUser.is_admin?
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -17,11 +17,7 @@ class WikiPagesController < ApplicationController
|
|||||||
def index
|
def index
|
||||||
@wiki_pages = WikiPage.paginated_search(params)
|
@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])
|
respond_with(@wiki_pages)
|
||||||
redirect_to @wiki_pages.first
|
|
||||||
else
|
|
||||||
respond_with(@wiki_pages)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def search
|
def search
|
||||||
@@ -80,6 +76,14 @@ class WikiPagesController < ApplicationController
|
|||||||
|
|
||||||
private
|
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
|
def normalize_search_params
|
||||||
if params[:title]
|
if params[:title]
|
||||||
params[:search] ||= {}
|
params[:search] ||= {}
|
||||||
|
|||||||
Reference in New Issue
Block a user