aboutsummaryrefslogtreecommitdiffstats
path: root/app
diff options
context:
space:
mode:
Diffstat (limited to 'app')
-rw-r--r--app/controllers/request_controller.rb12
-rw-r--r--app/helpers/application_helper.rb13
-rw-r--r--app/models/info_request.rb17
-rw-r--r--app/views/request/_list_results.html.erb12
-rw-r--r--app/views/request/list.html.erb22
5 files changed, 53 insertions, 23 deletions
diff --git a/app/controllers/request_controller.rb b/app/controllers/request_controller.rb
index 04d8f06b0..649075568 100644
--- a/app/controllers/request_controller.rb
+++ b/app/controllers/request_controller.rb
@@ -141,7 +141,10 @@ class RequestController < ApplicationController
def list
medium_cache
@view = params[:view]
+ @locale = self.locale_from_params()
@page = get_search_page_from_params if !@page # used in cache case, as perform_search sets @page as side effect
+ @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
@@ -151,16 +154,11 @@ class RequestController < ApplicationController
raise ActiveRecord::RecordNotFound.new("Sorry. No pages after #{MAX_RESULTS / PER_PAGE}.")
end
- query = InfoRequestEvent.make_query_from_params(params.merge(:latest_status => @view))
+ @filters = params.merge(:latest_status => @view)
@title = _("View and search requests")
- sortby = "newest"
- xapian_object = perform_search([InfoRequestEvent], query, sortby, 'request_collapse')
- @list_results = xapian_object.results.map { |r| r[:model] }
- @matches_estimated = xapian_object.matches_estimated
- @show_no_more_than = (@matches_estimated > MAX_RESULTS) ? MAX_RESULTS : @matches_estimated
@title = @title + " (page " + @page.to_s + ")" if (@page > 1)
- @track_thing = TrackThing.create_track_for_search_query(query)
+ @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 } ]
# Don't let robots go more than 20 pages in
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
index 4b603b064..154697377 100644
--- a/app/helpers/application_helper.rb
+++ b/app/helpers/application_helper.rb
@@ -123,5 +123,18 @@ module ApplicationHelper
yield
end
end
+
+ # We only want to cache request lists that have a reasonable chance of not expiring
+ # before they're requested again. Don't cache lists returned from specific searches
+ # or anything except the first page of results, just the first page of the default
+ # views
+ def request_list_cache_key
+ cacheable_param_list = ['controller', 'action', 'locale', 'view']
+ if params.keys.all?{ |key| cacheable_param_list.include?(key) }
+ "request-list-#{@view}-#{@locale}"
+ else
+ nil
+ end
+ end
end
diff --git a/app/models/info_request.rb b/app/models/info_request.rb
index 0a073dc79..4624cefaf 100644
--- a/app/models/info_request.rb
+++ b/app/models/info_request.rb
@@ -1228,6 +1228,23 @@ public
return [xapian_similar, xapian_similar_more]
end
+ def InfoRequest.request_list(filters, page, per_page, max_results)
+ xapian_object = ActsAsXapian::Search.new([InfoRequestEvent],
+ InfoRequestEvent.make_query_from_params(filters),
+ :offset => (page - 1) * per_page,
+ :limit => 25,
+ :sort_by_prefix => 'created_at',
+ :sort_by_ascending => true,
+ :collapse_by_prefix => 'request_collapse'
+ )
+ list_results = xapian_object.results.map { |r| r[:model] }
+ matches_estimated = xapian_object.matches_estimated
+ show_no_more_than = [matches_estimated, max_results].min
+ return { :results => list_results,
+ :matches_estimated => matches_estimated,
+ :show_no_more_than => show_no_more_than }
+ end
+
def InfoRequest.recent_requests
request_events = []
request_events_all_successful = false
diff --git a/app/views/request/_list_results.html.erb b/app/views/request/_list_results.html.erb
new file mode 100644
index 000000000..4da042816
--- /dev/null
+++ b/app/views/request/_list_results.html.erb
@@ -0,0 +1,12 @@
+ <% @results = InfoRequest.request_list(@filters, @page, @per_page, @max_results) %>
+ <% if @results[:results].empty? %>
+ <p> <%= _('No requests of this sort yet.')%></p>
+ <% else %>
+ <h2 class="foi_results"><%= _('{{count}} FOI requests found', :count => @results[:matches_estimated]) %></h2>
+ <div class="results_block">
+ <% @results[:results].each do |result| %>
+ <%= render :partial => 'request/request_listing_via_event', :locals => { :event => result, :info_request => result.info_request } %>
+ <% end %>
+ </div>
+ <% end %>
+ <%= will_paginate WillPaginate::Collection.new(@page, @per_page, @results[:show_no_more_than]) %>
diff --git a/app/views/request/list.html.erb b/app/views/request/list.html.erb
index 062b77c3e..a465f03ba 100644
--- a/app/views/request/list.html.erb
+++ b/app/views/request/list.html.erb
@@ -14,21 +14,11 @@
<div style="clear:both"></div>
<div class="results_section">
- <% # TODO: Cache for 5 minutes %>
- <% if @list_results.empty? %>
- <p> <%= _('No requests of this sort yet.')%></p>
- <% else %>
- <h2 class="foi_results"><%= _('{{count}} FOI requests found', :count => @matches_estimated) %></h2>
- <div class="results_block">
- <% for result in @list_results%>
- <% if result.class.to_s == 'InfoRequestEvent' %>
- <%= render :partial => 'request/request_listing_via_event', :locals => { :event => result, :info_request => result.info_request } %>
- <% else %>
- <p><strong><%= _('Unexpected search result type') %> <%=result.class.to_s%></strong></p>
- <% end %>
- <% end %>
- </div>
+ <% if key = request_list_cache_key %>
+ <% cache_if_caching_fragments(key, :expires_in => 5.minutes) do %>
+ <%= render :partial => 'list_results' %>
<% end %>
-
- <%= will_paginate WillPaginate::Collection.new(@page, @per_page, @show_no_more_than) %>
+ <% else %>
+ <%= render :partial => 'list_results' %>
+ <% end %>
</div>