diff --git a/app/controllers/request_controller.rb b/app/controllers/request_controller.rb index cb23b6ce70..b4e335c6eb 100644 --- a/app/controllers/request_controller.rb +++ b/app/controllers/request_controller.rb @@ -14,6 +14,7 @@ class RequestController < ApplicationController before_action :set_info_request, only: [:show] before_action :redirect_new_form_to_pro_version, only: [:select_authority, :new] before_action :set_in_pro_area, only: [:select_authority, :show] + before_action :setup_results_pagination, only: [:list, :list_by_tag, :similar] helper_method :state_transitions_empty? @@ -110,13 +111,6 @@ def details # Requests similar to this one def similar short_cache - @per_page = 25 - @page = (params[:page] || "1").to_i - - # Later pages are very expensive to load - if @page > MAX_RESULTS / PER_PAGE - raise ActiveRecord::RecordNotFound, "Sorry. No pages after #{MAX_RESULTS / PER_PAGE}." - end @info_request = InfoRequest.find_by_url_title!(params[:url_title]) @@ -134,20 +128,10 @@ def similar def list medium_cache @view = params[:view] - unless @page # used in cache case, as perform_search sets @page as side effect - @page = get_search_page_from_params - end - @per_page = PER_PAGE - @max_results = MAX_RESULTS if @view == "recent" return redirect_to request_list_all_url(action: "list", view: "all", page: @page), status: :moved_permanently end - # Later pages are very expensive to load - if @page > MAX_RESULTS / PER_PAGE - raise ActiveRecord::RecordNotFound, "Sorry. No pages after #{MAX_RESULTS / PER_PAGE}." - end - @filters = params.merge(latest_status: @view) if @page > 1 @@ -158,9 +142,34 @@ def list @track_thing = TrackThing.create_track_for_search_query(InfoRequestEvent.make_query_from_params(@filters)) @feed_autodetect = [ { url: do_track_url(@track_thing, 'feed'), title: @track_thing.params[:title_in_rss], has_json: true } ] + end - # Don't let robots go more than 20 pages in - @no_crawl = true if @page > 20 + def list_by_tag + medium_cache + + @tag = params[:tag] + @filters = { query: "tag:#{@tag}" } + @results = InfoRequest.request_list(@filters, @page, @per_page, @max_results) + + @category = InfoRequest.category_list.find_by(category_tag: @tag) + @title = if @category.nil? + n_('Found {{count}} request tagged ‘{{tag_name}}’', + 'Found {{count}} requests tagged ‘{{tag_name}}’', + @results[:matches_estimated], + count: @results[:matches_estimated], + tag_name: @tag) + else + n_('Found {{count}} request in the category ‘{{category_title}}’', + 'Found {{count}} requests in the category ‘{{category_title}}’', + @results[:matches_estimated], + count: @results[:matches_estimated], + category_title: @category.title) + end + + @track_thing = TrackThing.create_track_for_search_query(InfoRequestEvent.make_query_from_params(@filters)) + @feed_autodetect = [ { url: do_track_url(@track_thing, 'feed'), title: @track_thing.params[:title_in_rss], has_json: true } ] + + render :list end # Page new form posts to @@ -474,6 +483,20 @@ def outgoing_message_params params.require(:outgoing_message).permit(:body, :what_doing) end + def setup_results_pagination + @page ||= get_search_page_from_params + @per_page = PER_PAGE + @max_results = MAX_RESULTS + + # Don't let robots go more than 20 pages in + @no_crawl = true if @page > 20 + + # Later pages are very expensive to load + if @page > MAX_RESULTS / PER_PAGE + raise ActiveRecord::RecordNotFound, "Sorry. No pages after #{MAX_RESULTS / PER_PAGE}." + end + end + def can_update_status(info_request) # Don't allow status update on external requests, otherwise accept param info_request.is_external? ? false : params[:update_status] == "1" diff --git a/app/views/request/_category.html.erb b/app/views/request/_category.html.erb new file mode 100644 index 0000000000..2f96b70660 --- /dev/null +++ b/app/views/request/_category.html.erb @@ -0,0 +1,2 @@ +<%= render_notes(@category.all_notes) %> +<%= @category.body %> diff --git a/app/views/request/_list_results.html.erb b/app/views/request/_list_results.html.erb index 8824ff0245..251ceffeab 100644 --- a/app/views/request/_list_results.html.erb +++ b/app/views/request/_list_results.html.erb @@ -1,4 +1,4 @@ -<% @results = InfoRequest.request_list(@filters, @page, @per_page, @max_results) %> +<% @results ||= InfoRequest.request_list(@filters, @page, @per_page, @max_results) %> <% if @results[:results].empty? %>

<%= _('No requests of this sort yet.')%>

<% else %> diff --git a/app/views/request/list.html.erb b/app/views/request/list.html.erb index 498c45e7ee..5a89b11565 100644 --- a/app/views/request/list.html.erb +++ b/app/views/request/list.html.erb @@ -2,6 +2,8 @@

<%= @title %>

<%= render :partial => 'request/request_search_form', :locals => { :after_form_fields => render(:partial => 'request/request_filter_form') } %> + + <%= render partial: 'category' if @category %>