aboutsummaryrefslogtreecommitdiffstats
path: root/app/controllers
diff options
context:
space:
mode:
Diffstat (limited to 'app/controllers')
-rw-r--r--app/controllers/general_controller.rb11
-rw-r--r--app/controllers/public_body_controller.rb94
-rw-r--r--app/controllers/track_controller.rb3
3 files changed, 85 insertions, 23 deletions
diff --git a/app/controllers/general_controller.rb b/app/controllers/general_controller.rb
index 540f67ec9..beefef4e6 100644
--- a/app/controllers/general_controller.rb
+++ b/app/controllers/general_controller.rb
@@ -178,6 +178,7 @@ class GeneralController < ApplicationController
@xapian_requests_hits = @xapian_requests.results.size
@xapian_requests_total_hits = @xapian_requests.matches_estimated
@total_hits += @xapian_requests.matches_estimated
+ @request_for_spelling = @xapian_requests
end
if @bodies
@xapian_bodies = perform_search([PublicBody], @query, @sortby, nil, 5)
@@ -186,6 +187,7 @@ class GeneralController < ApplicationController
@xapian_bodies_hits = @xapian_bodies.results.size
@xapian_bodies_total_hits = @xapian_bodies.matches_estimated
@total_hits += @xapian_bodies.matches_estimated
+ @request_for_spelling = @xapian_bodies
end
if @users
@xapian_users = perform_search([User], @query, @sortby, nil, 5)
@@ -194,14 +196,13 @@ class GeneralController < ApplicationController
@xapian_users_hits = @xapian_users.results.size
@xapian_users_total_hits = @xapian_users.matches_estimated
@total_hits += @xapian_users.matches_estimated
+ @request_for_spelling = @xapian_users
end
# Spelling and highight words are same for all three queries
- if !@xapian_requests.nil?
- @highlight_words = @xapian_requests.words_to_highlight
- if !(@xapian_requests.spelling_correction =~ /[a-z]+:/)
- @spelling_correction = @xapian_requests.spelling_correction
- end
+ @highlight_words = @request_for_spelling.words_to_highlight
+ if !(@request_for_spelling.spelling_correction =~ /[a-z]+:/)
+ @spelling_correction = @request_for_spelling.spelling_correction
end
@track_thing = TrackThing.create_track_for_search_query(@query, @variety_postfix)
diff --git a/app/controllers/public_body_controller.rb b/app/controllers/public_body_controller.rb
index 02f0ceb19..308d38e4c 100644
--- a/app/controllers/public_body_controller.rb
+++ b/app/controllers/public_body_controller.rb
@@ -7,6 +7,7 @@
require 'fastercsv'
require 'confidence_intervals'
+require 'tempfile'
class PublicBodyController < ApplicationController
# XXX tidy this up with better error messages, and a more standard infrastructure for the redirect to canonical URL
@@ -113,8 +114,8 @@ class PublicBodyController < ApplicationController
elsif @tag == 'other'
category_list = PublicBodyCategories::get().tags().map{|c| "'"+c+"'"}.join(",")
where_condition += base_tag_condition + " AND has_tag_string_tags.name in (#{category_list})) = 0"
- elsif @tag.size == 1
- @tag.upcase!
+ elsif @tag.scan(/./mu).size == 1
+ @tag = Unicode.upcase @tag
# The first letter queries have to be done on
# translations, so just indicate to add that later:
first_letter = true
@@ -191,9 +192,76 @@ class PublicBodyController < ApplicationController
end
def list_all_csv
- send_data(PublicBody.export_csv, :type=> 'text/csv; charset=utf-8; header=present',
+ # FIXME: this is just using the download directory for zip
+ # archives, since we know that is allowed for X-Sendfile and
+ # the filename can't clash with the numeric subdirectory names
+ # used for the zips. However, really there should be a
+ # generically named downloads directory that contains all
+ # kinds of downloadable assets.
+ download_directory = File.join(InfoRequest.download_zip_dir(),
+ 'download')
+ FileUtils.mkdir_p download_directory
+ output_leafname = 'all-authorities.csv'
+ output_filename = File.join download_directory, output_leafname
+ # Create a temporary file in the same directory, so we can
+ # rename it atomically to the intended filename:
+ tmp = Tempfile.new output_leafname, download_directory
+ tmp.close
+ # Export all the public bodies to that temporary path and make
+ # it readable:
+ PublicBody.export_csv tmp.path
+ FileUtils.chmod 0644, tmp.path
+ # Rename into place and send the file:
+ File.rename tmp.path, output_filename
+ send_file(output_filename,
+ :type => 'text/csv; charset=utf-8; header=present',
:filename => 'all-authorities.csv',
- :disposition =>'attachment', :encoding => 'utf8')
+ :disposition =>'attachment',
+ :encoding => 'utf8')
+ end
+
+
+ # This is a helper method to take data returned by the PublicBody
+ # model's statistics-generating methods, and converting them to
+ # simpler data structure that can be rendered by a Javascript
+ # graph library. (This could be a class method except that we need
+ # access to the URL helper public_body_path.)
+ def simplify_stats_for_graphs(data,
+ column,
+ percentages,
+ graph_properties)
+ # Copy the data, only taking known-to-be-safe keys:
+ result = Hash.new { |h, k| h[k] = [] }
+ result.update Hash[data.select do |key, value|
+ ['y_values',
+ 'y_max',
+ 'totals',
+ 'cis_below',
+ 'cis_above'].include? key
+ end]
+
+ # Extract data about the public bodies for the x-axis,
+ # tooltips, and so on:
+ data['public_bodies'].each_with_index do |pb, i|
+ result['x_values'] << i
+ result['x_ticks'] << [i, pb.name]
+ result['tooltips'] << "#{pb.name} (#{result['totals'][i]})"
+ result['public_bodies'] << {
+ 'name' => pb.name,
+ 'url' => public_body_path(pb)
+ }
+ end
+
+ # Set graph metadata properties, like the title, axis labels, etc.
+ graph_id = "#{column}-"
+ graph_id += graph_properties[:highest] ? 'highest' : 'lowest'
+ result.update({
+ 'id' => graph_id,
+ 'x_axis' => _('Public Bodies'),
+ 'y_axis' => graph_properties[:y_axis],
+ 'errorbars' => percentages,
+ 'title' => graph_properties[:title]
+ })
end
def statistics
@@ -201,7 +269,7 @@ class PublicBodyController < ApplicationController
raise ActiveRecord::RecordNotFound.new("Page not enabled")
end
- per_graph = 8
+ per_graph = 10
minimum_requests = AlaveteliConfiguration::minimum_requests_for_statistics
# Make sure minimum_requests is > 0 to avoid division-by-zero
minimum_requests = [minimum_requests, 1].max
@@ -251,20 +319,12 @@ class PublicBodyController < ApplicationController
minimum_requests)
end
- data_to_draw = {
- 'id' => "#{column}-#{highest ? 'highest' : 'lowest'}",
- 'x_axis' => _('Public Bodies'),
- 'y_axis' => graph_properties[:y_axis],
- 'errorbars' => percentages,
- 'title' => graph_properties[:title]}
-
if data
- data_to_draw.update(data)
- data_to_draw['x_values'] = data['public_bodies'].each_with_index.map { |pb, i| i }
- data_to_draw['x_ticks'] = data['public_bodies'].each_with_index.map { |pb, i| [i, pb.name] }
+ @graph_list.push simplify_stats_for_graphs(data,
+ column,
+ percentages,
+ graph_properties)
end
-
- @graph_list.push data_to_draw
end
end
diff --git a/app/controllers/track_controller.rb b/app/controllers/track_controller.rb
index 9e41e35f9..1123903f9 100644
--- a/app/controllers/track_controller.rb
+++ b/app/controllers/track_controller.rb
@@ -184,7 +184,8 @@ class TrackController < ApplicationController
if new_medium == 'delete'
track_thing.destroy
flash[:notice] = _("You are no longer following {{track_description}}.", :track_description => track_thing.params[:list_description])
- redirect_to params[:r]
+ redirect_to URI.parse(params[:r]).path
+
# Reuse code like this if we let medium change again.
#elsif new_medium == 'email_daily'
# track_thing.track_medium = new_medium