diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index ba3d08ffc..c7b68906d 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -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 diff --git a/app/controllers/artists_controller.rb b/app/controllers/artists_controller.rb index 030913614..a57187cc2 100644 --- a/app/controllers/artists_controller.rb +++ b/app/controllers/artists_controller.rb @@ -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 diff --git a/app/controllers/pools_controller.rb b/app/controllers/pools_controller.rb index b6eb0c2ae..8d3877c26 100644 --- a/app/controllers/pools_controller.rb +++ b/app/controllers/pools_controller.rb @@ -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: []) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index cbe809266..ad0ad5053 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -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 diff --git a/app/controllers/wiki_pages_controller.rb b/app/controllers/wiki_pages_controller.rb index 34bc094e2..57bb6eda1 100644 --- a/app/controllers/wiki_pages_controller.rb +++ b/app/controllers/wiki_pages_controller.rb @@ -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] ||= {}