diff options
Diffstat (limited to 'app')
-rw-r--r-- | app/controllers/general_controller.rb | 45 | ||||
-rw-r--r-- | app/controllers/request_controller.rb | 16 | ||||
-rw-r--r-- | app/helpers/application_helper.rb | 7 | ||||
-rw-r--r-- | app/mailers/request_mailer.rb | 6 | ||||
-rw-r--r-- | app/models/info_request.rb | 63 | ||||
-rw-r--r-- | app/models/public_body.rb | 24 | ||||
-rw-r--r-- | app/views/general/_frontpage_bodies_list.html.erb | 5 | ||||
-rw-r--r-- | app/views/general/_frontpage_requests_list.html.erb | 1 | ||||
-rw-r--r-- | app/views/general/frontpage.html.erb | 3 | ||||
-rw-r--r-- | app/views/request/_sidebar.html.erb | 10 |
10 files changed, 116 insertions, 64 deletions
diff --git a/app/controllers/general_controller.rb b/app/controllers/general_controller.rb index beefef4e6..aac078829 100644 --- a/app/controllers/general_controller.rb +++ b/app/controllers/general_controller.rb @@ -12,52 +12,7 @@ class GeneralController < ApplicationController # New, improved front page! def frontpage medium_cache - # get some example searches and public bodies to display - # either from config, or based on a (slow!) query if not set - body_short_names = AlaveteliConfiguration::frontpage_publicbody_examples.split(/\s*;\s*/).map{|s| "'%s'" % s.gsub(/'/, "''") }.join(", ") @locale = self.locale_from_params() - locale_condition = 'public_body_translations.locale = ?' - conditions = [locale_condition, @locale] - I18n.with_locale(@locale) do - if body_short_names.empty? - # This is too slow - @popular_bodies = PublicBody.visible.find(:all, - :order => "info_requests_count desc", - :limit => 32, - :conditions => conditions, - :joins => :translations - ) - else - conditions[0] += " and public_bodies.url_name in (" + body_short_names + ")" - @popular_bodies = PublicBody.find(:all, - :conditions => conditions, - :joins => :translations) - end - end - # Get some successful requests - begin - query = 'variety:response (status:successful OR status:partially_successful)' - sortby = "newest" - max_count = 5 - xapian_object = perform_search([InfoRequestEvent], query, sortby, 'request_title_collapse', max_count) - @request_events = xapian_object.results.map { |r| r[:model] } - - # If there are not yet enough successful requests, fill out the list with - # other requests - if @request_events.count < max_count - @request_events_all_successful = false - query = 'variety:sent' - xapian_object = perform_search([InfoRequestEvent], query, sortby, 'request_title_collapse', max_count-@request_events.count) - more_events = xapian_object.results.map { |r| r[:model] } - @request_events += more_events - # Overall we still want the list sorted with the newest first - @request_events.sort!{|e1,e2| e2.created_at <=> e1.created_at} - else - @request_events_all_successful = true - end - rescue - @request_events = [] - end end # Display blog entries diff --git a/app/controllers/request_controller.rb b/app/controllers/request_controller.rb index 44f3a5b9b..d982bd391 100644 --- a/app/controllers/request_controller.rb +++ b/app/controllers/request_controller.rb @@ -92,15 +92,8 @@ class RequestController < ApplicationController # Sidebar stuff @sidebar = true - # ... requests that have similar imporant terms - begin - limit = 10 - @xapian_similar = ActsAsXapian::Similar.new([InfoRequestEvent], @info_request.info_request_events, - :limit => limit, :collapse_by_prefix => 'request_collapse') - @xapian_similar_more = (@xapian_similar.matches_estimated > limit) - rescue - @xapian_similar = nil - end + @similar_cache_key = cache_key_for_similar_requests(@info_request, @locale) + # Track corresponding to this page @track_thing = TrackThing.create_track_for_request(@info_request) @feed_autodetect = [ { :url => do_track_url(@track_thing, 'feed'), :title => @track_thing.params[:title_in_rss], :has_json => true } ] @@ -972,5 +965,10 @@ class RequestController < ApplicationController file_info end + def cache_key_for_similar_requests(info_request, locale) + "request/similar/#{info_request.id}/#{locale}" + end + + end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 6d0a111e9..4b603b064 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -116,5 +116,12 @@ module ApplicationHelper return !session[:using_admin].nil? || (!@user.nil? && @user.super?) end + def cache_if_caching_fragments(*args, &block) + if AlaveteliConfiguration::cache_fragments + cache(*args) { yield } + else + yield + end + end end diff --git a/app/mailers/request_mailer.rb b/app/mailers/request_mailer.rb index 13b3bc4a1..c8a19afa8 100644 --- a/app/mailers/request_mailer.rb +++ b/app/mailers/request_mailer.rb @@ -63,7 +63,7 @@ class RequestMailer < ApplicationMailer mail(:from => user.name_and_email, :to => contact_from_name_and_email, - :subject => _("FOI response requires admin ({{reason}}) - {{title}}", :reason => info_request.described_state, :title => info_request.title)) + :subject => _("FOI response requires admin ({{reason}}) - {{title}}", :reason => info_request.described_state, :title => info_request.title).html_safe) end # Tell the requester that a new response has arrived @@ -79,7 +79,7 @@ class RequestMailer < ApplicationMailer mail(:from => contact_from_name_and_email, :to => info_request.user.name_and_email, - :subject => _("New response to your FOI request - ") + info_request.title, + :subject => (_("New response to your FOI request - ") + info_request.title).html_safe, :charset => "UTF-8", # not much we can do if the user's email is broken :reply_to => contact_from_name_and_email) @@ -182,7 +182,7 @@ class RequestMailer < ApplicationMailer mail(:from => contact_from_name_and_email, :to => info_request.user.name_and_email, - :subject => _("Clarify your FOI request - ") + info_request.title) + :subject => (_("Clarify your FOI request - ") + info_request.title).html_safe) end # Tell requester that somebody add an annotation to their request diff --git a/app/models/info_request.rb b/app/models/info_request.rb index 9463a236e..0a073dc79 100644 --- a/app/models/info_request.rb +++ b/app/models/info_request.rb @@ -1212,6 +1212,69 @@ public end end + + # Get requests that have similar important terms + def similar_requests(limit=10) + xapian_similar = nil + xapian_similar_more = false + begin + xapian_similar = ActsAsXapian::Similar.new([InfoRequestEvent], + info_request_events, + :limit => limit, + :collapse_by_prefix => 'request_collapse') + xapian_similar_more = (xapian_similar.matches_estimated > limit) + rescue + end + return [xapian_similar, xapian_similar_more] + end + + def InfoRequest.recent_requests + request_events = [] + request_events_all_successful = false + # Get some successful requests + begin + query = 'variety:response (status:successful OR status:partially_successful)' + sortby = "newest" + max_count = 5 + + xapian_object = ActsAsXapian::Search.new([InfoRequestEvent], + query, + :offset => 0, + :limit => 5, + :sort_by_prefix => 'created_at', + :sort_by_ascending => true, + :collapse_by_prefix => 'request_title_collapse' + ) + xapian_object.results + request_events = xapian_object.results.map { |r| r[:model] } + + # If there are not yet enough successful requests, fill out the list with + # other requests + if request_events.count < max_count + query = 'variety:sent' + xapian_object = ActsAsXapian::Search.new([InfoRequestEvent], + query, + :offset => 0, + :limit => max_count-request_events.count, + :sort_by_prefix => 'created_at', + :sort_by_ascending => true, + :collapse_by_prefix => 'request_title_collapse' + ) + xapian_object.results + more_events = xapian_object.results.map { |r| r[:model] } + request_events += more_events + # Overall we still want the list sorted with the newest first + request_events.sort!{|e1,e2| e2.created_at <=> e1.created_at} + else + request_events_all_successful = true + end + rescue + request_events = [] + end + + return [request_events, request_events_all_successful] + end + private def set_defaults diff --git a/app/models/public_body.rb b/app/models/public_body.rb index 883afc3af..8e474c797 100644 --- a/app/models/public_body.rb +++ b/app/models/public_body.rb @@ -722,6 +722,30 @@ class PublicBody < ActiveRecord::Base 'y_max' => 100, 'totals' => original_totals} end + def self.popular_bodies(locale) + # get some example searches and public bodies to display + # either from config, or based on a (slow!) query if not set + body_short_names = AlaveteliConfiguration::frontpage_publicbody_examples.split(/\s*;\s*/) + locale_condition = 'public_body_translations.locale = ?' + conditions = [locale_condition, locale] + bodies = [] + I18n.with_locale(locale) do + if body_short_names.empty? + # This is too slow + bodies = visible.find(:all, + :order => "info_requests_count desc", + :limit => 32, + :conditions => conditions, + :joins => :translations + ) + else + conditions[0] += " and public_bodies.url_name in (?)" + conditions << body_short_names + bodies = find(:all, :conditions => conditions, :joins => :translations) + end + end + return bodies + end private diff --git a/app/views/general/_frontpage_bodies_list.html.erb b/app/views/general/_frontpage_bodies_list.html.erb index 75daea41d..44321f14a 100644 --- a/app/views/general/_frontpage_bodies_list.html.erb +++ b/app/views/general/_frontpage_bodies_list.html.erb @@ -1,10 +1,11 @@ -<% if @popular_bodies.size > 0 %> +<%- popular_bodies = PublicBody.popular_bodies(@locale) %> +<% if popular_bodies.size > 0 %> <div id="examples_0"> <h3><%= _("Who can I request information from?") %></h3> <%= _("{{site_name}} covers requests to {{number_of_authorities}} authorities, including:", :site_name => site_name, :number_of_authorities => PublicBody.visible.count) %> <ul> - <% for popular_body in @popular_bodies %> + <% for popular_body in popular_bodies %> <li><%=public_body_link(popular_body)%> <%= n_('{{count}} request', '{{count}} requests', popular_body.info_requests_count, :count => popular_body.info_requests_count) %> </li> diff --git a/app/views/general/_frontpage_requests_list.html.erb b/app/views/general/_frontpage_requests_list.html.erb index a628c14ef..d7d9184c4 100644 --- a/app/views/general/_frontpage_requests_list.html.erb +++ b/app/views/general/_frontpage_requests_list.html.erb @@ -1,3 +1,4 @@ +<%- @request_events, @request_events_all_successful = InfoRequest.recent_requests %> <div id="examples_1"> <h3> <% if @request_events_all_successful %> diff --git a/app/views/general/frontpage.html.erb b/app/views/general/frontpage.html.erb index bf5261d15..8bb32bdf2 100644 --- a/app/views/general/frontpage.html.erb +++ b/app/views/general/frontpage.html.erb @@ -1,4 +1,4 @@ -<% # TODO: Cache for 5 minutes %> +<% cache_if_caching_fragments("frontpage-#{@locale}", :expires_in => 5.minutes) do %> <div id="frontpage_splash"> <div id="left_column"> <%= render :partial => "frontpage_new_request" %> @@ -17,3 +17,4 @@ <%= render :partial => "frontpage_bodies_list" %> <%= render :partial => "frontpage_requests_list" %> </div> +<% end %> diff --git a/app/views/request/_sidebar.html.erb b/app/views/request/_sidebar.html.erb index 8d4a4a2d8..8400cd6ac 100644 --- a/app/views/request/_sidebar.html.erb +++ b/app/views/request/_sidebar.html.erb @@ -51,16 +51,18 @@ <%= render :partial => 'request/next_actions' %> - <% # TODO: Cache for 1 day %> - <% if !@xapian_similar.nil? && @xapian_similar.results.size > 0 %> + <% cache_if_caching_fragments(@similar_cache_key, :expires_in => 1.day) do %> + <% xapian_similar, xapian_similar_more = @info_request.similar_requests %> + <% if !xapian_similar.nil? && xapian_similar.results.size > 0 %> <h2><%= _('Similar requests')%></h2> - <% for result in @xapian_similar.results %> + <% for result in xapian_similar.results %> <%= render :partial => 'request/request_listing_short_via_event', :locals => { :event => result[:model], :info_request => result[:model].info_request } %> <% end %> - <% if @xapian_similar_more %> + <% if xapian_similar_more %> <p><%= link_to _("More similar requests"), similar_request_path(@info_request.url_title) %></p> <% end %> <% end %> + <% end %> <p><%= link_to _('Event history details'), request_details_path(@info_request) %></p> |