Skip to content

Commit

Permalink
Merge f370a7b into 9c22e2d
Browse files Browse the repository at this point in the history
  • Loading branch information
gbp committed Apr 11, 2024
2 parents 9c22e2d + f370a7b commit 9135c9a
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 30 deletions.
72 changes: 53 additions & 19 deletions app/controllers/request_controller.rb
Expand Up @@ -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?

Expand Down Expand Up @@ -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])

Expand All @@ -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
Expand All @@ -158,9 +142,44 @@ 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)
if @category.nil?
@title = n_('Found {{count}} request tagged ‘{{tag_name}}’',
'Found {{count}} requests tagged ‘{{tag_name}}’',
@results[:matches_estimated],
count: @results[:matches_estimated],
tag_name: @tag)
else
@title = n_('Found {{count}} request in the category ‘{{category}}’',
'Found {{count}} requests in the category ‘{{category}}’',
@results[:matches_estimated],
count: @results[:matches_estimated],
category: @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
Expand Down Expand Up @@ -474,6 +493,21 @@ 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
return if @page <= MAX_RESULTS / PER_PAGE

raise ActiveRecord::RecordNotFound,
"Sorry. No pages after #{MAX_RESULTS / PER_PAGE}."
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"
Expand Down
2 changes: 2 additions & 0 deletions app/views/request/_category.html.erb
@@ -0,0 +1,2 @@
<%= render_notes(@category.all_notes) %>
<%= @category.body %>
2 changes: 1 addition & 1 deletion 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? %>
<p> <%= _('No requests of this sort yet.')%></p>
<% else %>
Expand Down
2 changes: 2 additions & 0 deletions app/views/request/list.html.erb
Expand Up @@ -2,6 +2,8 @@
<h1><%= @title %></h1>
<%= render :partial => 'request/request_search_form',
:locals => { :after_form_fields => render(:partial => 'request/request_filter_form') } %>
<%= render partial: 'category' if @category %>
</div>

<div id="header_right" class="sidebar header_right">
Expand Down
3 changes: 3 additions & 0 deletions config/routes.rb
Expand Up @@ -98,6 +98,9 @@ def matches?(request)
match '/list' => 'request#list',
:as => :request_list,
:via => :get
match '/list/:tag' => 'request#list_by_tag',
:as => :request_list_by_tag,
:via => :get

match '/select_authority' => 'request#select_authority',
:as => :select_authority,
Expand Down
1 change: 1 addition & 0 deletions doc/CHANGES.md
Expand Up @@ -2,6 +2,7 @@

## Highlighted Features

* Add list by tag endpoint to view tagged requests (Graeme Porteous)
* Allow categories to have notes associated with them (Graeme Porteous)
* Add styling option and rich text editor to the notes admin (Graeme Porteous)
* Strengthen 2FA warning. Users *must* remember to keep this code safe (Gareth
Expand Down
67 changes: 57 additions & 10 deletions spec/controllers/request_controller_spec.rb
@@ -1,11 +1,6 @@
require 'spec_helper'

RSpec.describe RequestController, "when listing recent requests" do
before(:each) do
load_raw_emails_data
update_xapian_index
end

it "should be successful" do
get :list, params: { view: 'all' }
expect(response).to be_successful
Expand All @@ -17,11 +12,6 @@
end

it "should return 404 for pages we don't want to serve up" do
xap_results = double(
ActsAsXapian::Search,
results: (1..25).to_a.map { |m| { model: m } },
matches_estimated: 1_000_000
)
expect {
get :list, params: { view: 'all', page: 100 }
}.to raise_error(ActiveRecord::RecordNotFound)
Expand All @@ -39,6 +29,63 @@
end
end

RSpec.describe RequestController, "when listing recent requests by tag" do
it "should be successful" do
get :list_by_tag, params: { tag: 'climate' }
expect(response).to be_successful
end

it "should render with 'list' template" do
get :list_by_tag, params: { tag: 'climate' }
expect(response).to render_template('list')
end

it "should return 404 for pages we don't want to serve up" do
expect {
get :list_by_tag, params: { tag: 'climate', page: 100 }
}.to raise_error(ActiveRecord::RecordNotFound)
end

it "raise unknown format error" do
expect { get :list_by_tag, params: { tag: "climate", format: :json } }.to(
raise_error ActionController::UnknownFormat
)
end

it 'should not raise an error for a page param of less than zero, but should treat it as a param of 1' do
expect { get :list_by_tag, params: { tag: 'climate', page: "-1" } }.
not_to raise_error
expect(assigns[:page]).to eq(1)
end

it 'sets title based on if tag matches an request category' do
FactoryBot.create(:category, :info_request,
title: 'Climate requests', category_tag: 'climate')

update_xapian_index
get :list_by_tag, params: { tag: 'climate' }
expect(assigns[:title]).
to eq("Found 0 requests in the category ‘Climate requests’")

FactoryBot.create(:info_request, tag_string: 'climate')
update_xapian_index
get :list_by_tag, params: { tag: 'climate' }
expect(assigns[:title]).
to eq("Found 1 request in the category ‘Climate requests’")
end

it 'sets title based on if tag does not match an request category' do
update_xapian_index
get :list_by_tag, params: { tag: 'other' }
expect(assigns[:title]).to eq("Found 0 requests tagged ‘other’")

FactoryBot.create(:info_request, tag_string: 'other')
update_xapian_index
get :list_by_tag, params: { tag: 'other' }
expect(assigns[:title]).to eq("Found 1 request tagged ‘other’")
end
end

RSpec.describe RequestController, "when showing one request" do
render_views

Expand Down
4 changes: 4 additions & 0 deletions spec/factories/categories.rb
Expand Up @@ -20,5 +20,9 @@
trait :public_body do
parents { [PublicBody.category_root] }
end

trait :info_request do
parents { [InfoRequest.category_root] }
end
end
end

0 comments on commit 9135c9a

Please sign in to comment.