DEV: Change tag routing for categories on the server-side

These are the changes to tags routing that correspond to the category
routing changes of d84c34ad. The new scheme is:

/tags/c/*slug_path/:id/none/:tag_id/ENDPOINT
/tags/c/*slug_path/:id/none/:tag_id
/tags/c/*slug_path/:id/:tag_id/ENDPOINT
/tags/c/*slug_path/:id/:tag_id
This commit is contained in:
Daniel Waterworth 2019-11-03 16:32:31 +00:00
parent 671f303b53
commit fa2c06da93
2 changed files with 66 additions and 23 deletions

View File

@ -293,29 +293,52 @@ class TagsController < ::ApplicationController
end end
def set_category_from_params def set_category_from_params
slug_or_id = params[:category] if request.path_parameters.include?(:category_slug_path_with_id)
return true if slug_or_id.nil? parts = params[:category_slug_path_with_id].split('/')
parent_slug_or_id = params[:parent_category] if !parts.empty? && parts.last =~ /\A\d+\Z/
id = parts.pop.to_i
end
slug_path = parts unless parts.empty?
parent_category_id = nil if id.present?
if parent_slug_or_id.present? @filter_on_category = Category.find_by_id(id)
parent_category_id = Category.query_parent_category(parent_slug_or_id) elsif slug_path.present?
category_redirect_or_not_found && (return) if parent_category_id.blank? if (1..2).include?(slug_path.size)
@filter_on_category = Category.find_by_slug(*slug_path.reverse)
end
# Legacy paths
if @filter_on_category.nil? && parts.last =~ /\A\d+-/
@filter_on_category = Category.find_by_id(parts.last.to_i)
end
end
else
slug_or_id = params[:category]
return true if slug_or_id.nil?
@filter_on_category = Category.query_category(slug_or_id, nil)
end end
@filter_on_category = Category.query_category(slug_or_id, parent_category_id)
category_redirect_or_not_found && (return) if !@filter_on_category category_redirect_or_not_found && (return) if !@filter_on_category
guardian.ensure_can_see!(@filter_on_category) guardian.ensure_can_see!(@filter_on_category)
end end
# TODO: this is duplication of ListController
def page_params def page_params
route_params = { format: 'json' } route_params = { format: 'json' }
route_params[:category] = @filter_on_category.slug_for_url if @filter_on_category
route_params[:parent_category] = @filter_on_category.parent_category.slug_for_url if @filter_on_category && @filter_on_category.parent_category if @filter_on_category
if request.path_parameters.include?(:category_slug_path_with_id)
slug_path = @filter_on_category.slug_path
route_params[:category_slug_path_with_id] =
(slug_path + [@filter_on_category.id.to_s]).join("/")
else
route_params[:category] = @filter_on_category.slug_for_url
end
end
route_params route_params
end end
@ -345,15 +368,27 @@ class TagsController < ::ApplicationController
def construct_url_with(action, opts) def construct_url_with(action, opts)
method = url_method(opts) method = url_method(opts)
begin page_params =
url = if action == :prev case action
public_send(method, opts.merge(prev_page_params)) when :prev
else # :next prev_page_params
public_send(method, opts.merge(next_page_params)) when :next
next_page_params
else
raise "unreachable"
end end
if page_params.include?(:category_slug_path_with_id)
opts = opts.dup
opts.delete(:category)
end
begin
url = public_send(method, opts.merge(page_params))
rescue ActionController::UrlGenerationError rescue ActionController::UrlGenerationError
raise Discourse::NotFound raise Discourse::NotFound
end end
url.sub('.json?', '?') url.sub('.json?', '?')
end end

View File

@ -836,11 +836,22 @@ Discourse::Application.routes.draw do
get '/unused' => 'tags#list_unused' get '/unused' => 'tags#list_unused'
delete '/unused' => 'tags#destroy_unused' delete '/unused' => 'tags#destroy_unused'
constraints(tag_id: /[^\/]+?/, format: /json|rss/) do constraints(tag_id: /[^\/]+?/, format: /json|rss/) do
scope path: '/c/*category_slug_path_with_id' do
Discourse.filters.each do |filter|
get "/none/:tag_id/l/#{filter}" => "tags#show_#{filter}", as: "tag_category_none_show_#{filter}", defaults: { no_subcategories: true }
end
get '/none/:tag_id' => 'tags#show', as: 'tag_category_none_show', defaults: { no_subcategories: true }
Discourse.filters.each do |filter|
get "/:tag_id/l/#{filter}" => "tags#show_#{filter}", as: "tag_category_show_#{filter}"
end
get '/:tag_id' => 'tags#show', as: 'tag_category_show'
end
get '/:tag_id.rss' => 'tags#tag_feed' get '/:tag_id.rss' => 'tags#tag_feed'
get '/:tag_id' => 'tags#show', as: 'tag_show' get '/:tag_id' => 'tags#show', as: 'tag_show'
get '/c/:category/:tag_id' => 'tags#show', as: 'tag_category_show'
get '/c/:category/none/:tag_id' => 'tags#show', as: 'tag_category_none_show', defaults: { no_subcategories: true }
get '/c/:parent_category/:category/:tag_id' => 'tags#show', as: 'tag_parent_category_category_show'
get '/intersection/:tag_id/*additional_tag_ids' => 'tags#show', as: 'tag_intersection' get '/intersection/:tag_id/*additional_tag_ids' => 'tags#show', as: 'tag_intersection'
get '/:tag_id/notifications' => 'tags#notifications' get '/:tag_id/notifications' => 'tags#notifications'
put '/:tag_id/notifications' => 'tags#update_notifications' put '/:tag_id/notifications' => 'tags#update_notifications'
@ -849,9 +860,6 @@ Discourse::Application.routes.draw do
Discourse.filters.each do |filter| Discourse.filters.each do |filter|
get "/:tag_id/l/#{filter}" => "tags#show_#{filter}", as: "tag_show_#{filter}" get "/:tag_id/l/#{filter}" => "tags#show_#{filter}", as: "tag_show_#{filter}"
get "/c/:category/:tag_id/l/#{filter}" => "tags#show_#{filter}", as: "tag_category_show_#{filter}"
get "/c/:category/none/:tag_id/l/#{filter}" => "tags#show_#{filter}", as: "tag_category_none_show_#{filter}", defaults: { no_subcategories: true }
get "/c/:parent_category/:category/:tag_id/l/#{filter}" => "tags#show_#{filter}", as: "tag_parent_category_category_show_#{filter}"
end end
end end
end end