Merge pull request #4270 from BrokenEagle/universal-index-redirect

Add universal index redirect
This commit is contained in:
evazion
2020-01-25 14:52:52 -06:00
committed by GitHub
10 changed files with 64 additions and 26 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] ||= {}

View File

@@ -179,11 +179,11 @@ module ApplicationHelper
tag.input value: "Preview", type: "button", class: "dtext-preview-button", "data-input-id": input_id, "data-preview-id": preview_id
end
def quick_search_form_for(attribute, url, name, autocomplete: nil, &block)
def quick_search_form_for(attribute, url, name, autocomplete: nil, redirect: false, &block)
tag.li do
search_form_for(url, classes: "quick-search-form one-line-form") do |f|
out = f.input attribute, label: false, placeholder: "Search #{name}", input_html: { id: nil, "data-autocomplete": autocomplete }
out += tag.input type: :hidden, name: :redirect, value: 1
out += tag.input type: :hidden, name: :redirect, value: redirect
out += capture { yield f } if block_given?
out
end

View File

@@ -1,5 +1,5 @@
<% content_for(:secondary_links) do %>
<%= quick_search_form_for(:any_name_or_url_matches, artists_path, "artists", autocomplete: "artist") %>
<%= quick_search_form_for(:any_name_or_url_matches, artists_path, "artists", autocomplete: "artist", redirect: true) %>
<%= subnav_link_to "Listing", artists_path %>
<%= subnav_link_to "Banned", artists_path(search: { is_banned: "true", order: "updated_at" }) %>
<% if CurrentUser.is_member? %>

View File

@@ -1,5 +1,5 @@
<% content_for(:secondary_links) do %>
<%= quick_search_form_for(:name_matches, pools_path, "pools", autocomplete: "pool") %>
<%= quick_search_form_for(:name_matches, pools_path, "pools", autocomplete: "pool", redirect: true) %>
<%= subnav_link_to "Gallery", gallery_pools_path %>
<%= subnav_link_to "Listing", pools_path %>
<%= subnav_link_to "New", new_pool_path %>

View File

@@ -1,5 +1,5 @@
<% content_for(:secondary_links) do %>
<%= quick_search_form_for(:name_matches, users_path, "users", autocomplete: "user") %>
<%= quick_search_form_for(:name_matches, users_path, "users", autocomplete: "user", redirect: true) %>
<%= subnav_link_to "Listing", users_path %>
<%= subnav_link_to "Search", search_users_path %>

View File

@@ -1,5 +1,5 @@
<% content_for(:secondary_links) do %>
<%= quick_search_form_for(:title, wiki_pages_path, "wiki pages", autocomplete: "wiki-page") %>
<%= quick_search_form_for(:title_normalize, wiki_pages_path, "wiki pages", autocomplete: "wiki-page", redirect: true) %>
<%= subnav_link_to "Listing", wiki_pages_path %>
<%= subnav_link_to "Search", search_wiki_pages_path %>
<% if CurrentUser.is_member? %>