diff options
-rw-r--r-- | Gemfile | 1 | ||||
-rw-r--r-- | Gemfile.lock | 2 | ||||
-rw-r--r-- | app/controllers/public_body_controller.rb | 15 | ||||
-rw-r--r-- | app/models/info_request.rb | 2 | ||||
-rw-r--r-- | app/models/public_body.rb | 11 | ||||
-rw-r--r-- | db/migrate/20120912170035_add_info_requests_count_to_public_bodies.rb | 17 | ||||
-rw-r--r-- | spec/fixtures/public_bodies.yml | 14 | ||||
-rw-r--r-- | spec/fixtures/public_body_translations.yml | 14 | ||||
-rw-r--r-- | spec/models/public_body_spec.rb | 14 |
9 files changed, 67 insertions, 23 deletions
@@ -11,6 +11,7 @@ gem 'rails', '2.3.14' gem 'pg' gem 'fast_gettext', '>= 0.6.0' +gem 'fastercsv', '>=1.5.5' gem 'gettext_i18n_rails', '>= 0.6.0', :git => "git://github.com/sebbacon/gettext_i18n_rails.git" gem 'gettext', '>= 1.9.3' gem 'json', '~> 1.5.1' diff --git a/Gemfile.lock b/Gemfile.lock index 2a15e1bae..841e92112 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -28,6 +28,7 @@ GEM columnize (0.3.6) fakeweb (1.3.0) fast_gettext (0.6.8) + fastercsv (1.5.5) gettext (2.2.1) locale highline (1.6.13) @@ -89,6 +90,7 @@ DEPENDENCIES capistrano fakeweb fast_gettext (>= 0.6.0) + fastercsv (>= 1.5.5) gettext (>= 1.9.3) gettext_i18n_rails (>= 0.6.0)! json (~> 1.5.1) diff --git a/app/controllers/public_body_controller.rb b/app/controllers/public_body_controller.rb index 95d936e54..b8ea82a66 100644 --- a/app/controllers/public_body_controller.rb +++ b/app/controllers/public_body_controller.rb @@ -7,7 +7,7 @@ # # $Id: public_body_controller.rb,v 1.8 2009-09-14 13:27:00 francis Exp $ -require 'csv' +require 'fastercsv' class PublicBodyController < ApplicationController # XXX tidy this up with better error messages, and a more standard infrastructure for the redirect to canonical URL @@ -148,10 +148,10 @@ class PublicBodyController < ApplicationController end def list_all_csv - public_bodies = PublicBody.find(:all, :order => 'url_name') - report = StringIO.new - CSV::Writer.generate(report, ',') do |title| - title << [ + public_bodies = PublicBody.find(:all, :order => 'url_name', + :include => [:translations, :tags]) + report = FasterCSV.generate() do |csv| + csv << [ 'Name', 'Short name', # deliberately not including 'Request email' @@ -164,7 +164,7 @@ class PublicBodyController < ApplicationController 'Version', ] public_bodies.each do |public_body| - title << [ + csv << [ public_body.name, public_body.short_name, # DO NOT include request_email (we don't want to make it @@ -179,8 +179,7 @@ class PublicBodyController < ApplicationController ] end end - report.rewind - send_data(report.read, :type=> 'text/csv; charset=utf-8; header=present', + send_data(report, :type=> 'text/csv; charset=utf-8; header=present', :filename => 'all-authorities.csv', :disposition =>'attachment', :encoding => 'utf8') end diff --git a/app/models/info_request.rb b/app/models/info_request.rb index f2d8929bc..ed54da840 100644 --- a/app/models/info_request.rb +++ b/app/models/info_request.rb @@ -35,7 +35,7 @@ class InfoRequest < ActiveRecord::Base belongs_to :user validate :must_be_internal_or_external - belongs_to :public_body + belongs_to :public_body, :counter_cache => true validates_presence_of :public_body_id has_many :outgoing_messages, :order => 'created_at' diff --git a/app/models/public_body.rb b/app/models/public_body.rb index fb30da234..77da81d4c 100644 --- a/app/models/public_body.rb +++ b/app/models/public_body.rb @@ -45,6 +45,8 @@ class PublicBody < ActiveRecord::Base has_many :censor_rules, :order => 'created_at desc' has_tag_string + before_save :set_api_key, :set_default_publication_scheme + translates :name, :short_name, :request_email, :url_name, :notes, :first_letter, :publication_scheme @@ -89,13 +91,13 @@ class PublicBody < ActiveRecord::Base end end - def after_initialize + def set_default_publication_scheme # Make sure publication_scheme gets the correct default value. # (This would work automatically, were publication_scheme not a translated attribute) self.publication_scheme = "" if self.publication_scheme.nil? end - def before_save + def set_api_key self.api_key = SecureRandom.base64(33) if self.api_key.nil? end @@ -184,7 +186,7 @@ class PublicBody < ActiveRecord::Base end acts_as_versioned - self.non_versioned_columns << 'created_at' << 'updated_at' << 'first_letter' << 'api_key' + self.non_versioned_columns << 'created_at' << 'updated_at' << 'first_letter' << 'api_key' << 'info_requests_count' class Version attr_accessor :created_at @@ -549,9 +551,10 @@ class PublicBody < ActiveRecord::Base def notes_as_html self.notes end + def notes_without_html # assume notes are reasonably behaved HTML, so just use simple regexp on this - self.notes.nil? ? '' : self.notes.gsub(/<\/?[^>]*>/, "") + @notes_without_html ||= (self.notes.nil? ? '' : self.notes.gsub(/<\/?[^>]*>/, "")) end def json_for_api diff --git a/db/migrate/20120912170035_add_info_requests_count_to_public_bodies.rb b/db/migrate/20120912170035_add_info_requests_count_to_public_bodies.rb new file mode 100644 index 000000000..d77dbaa64 --- /dev/null +++ b/db/migrate/20120912170035_add_info_requests_count_to_public_bodies.rb @@ -0,0 +1,17 @@ +class AddInfoRequestsCountToPublicBodies < ActiveRecord::Migration + def self.up + add_column :public_bodies, :info_requests_count, :integer, :null => false, :default => 0 + + PublicBody.reset_column_information + + PublicBody.find_each do |public_body| + public_body.update_attribute :info_requests_count, public_body.info_requests.length + end + + end + + def self.down + remove_column :public_bodies, :info_requests_count + end + +end diff --git a/spec/fixtures/public_bodies.yml b/spec/fixtures/public_bodies.yml index 367e0fc50..615c4bcb6 100644 --- a/spec/fixtures/public_bodies.yml +++ b/spec/fixtures/public_bodies.yml @@ -1,4 +1,4 @@ -geraldine_public_body: +geraldine_public_body: name: The Geraldine Quango first_letter: T updated_at: 2007-10-24 10:51:01.161639 @@ -11,7 +11,8 @@ geraldine_public_body: url_name: tgq created_at: 2007-10-24 10:51:01.161639 api_key: 1 -humpadink_public_body: + info_requests_count: 4 +humpadink_public_body: name: "Department for Humpadinking" first_letter: D updated_at: 2007-10-25 10:51:01.161639 @@ -25,6 +26,7 @@ humpadink_public_body: created_at: 2007-10-25 10:51:01.161639 notes: An albatross told me!!! api_key: 2 + info_requests_count: 2 forlorn_public_body: name: "Department of Loneliness" first_letter: D @@ -39,7 +41,8 @@ forlorn_public_body: created_at: 2011-01-26 14:11:02.12345 notes: A very lonely public body that no one has corresponded with api_key: 3 -silly_walks_public_body: + info_requests_count: 0 +silly_walks_public_body: id: 5 version: 1 name: "Ministry of Silly Walks" @@ -53,7 +56,8 @@ silly_walks_public_body: created_at: 2007-10-25 10:51:01.161639 notes: You know the one. api_key: 4 -sensible_walks_public_body: + info_requests_count: 2 +sensible_walks_public_body: id: 6 version: 1 name: "Ministry of Sensible Walks" @@ -67,4 +71,4 @@ sensible_walks_public_body: last_edit_editor: robin created_at: 2008-10-25 10:51:01.161639 api_key: 5 - + info_requests_count: 1 diff --git a/spec/fixtures/public_body_translations.yml b/spec/fixtures/public_body_translations.yml index cbb55bb0c..d705358c5 100644 --- a/spec/fixtures/public_body_translations.yml +++ b/spec/fixtures/public_body_translations.yml @@ -1,4 +1,4 @@ -geraldine_es_public_body_translation: +geraldine_es_public_body_translation: name: El A Geraldine Quango first_letter: E request_email: geraldine-requests@localhost @@ -8,8 +8,9 @@ geraldine_es_public_body_translation: url_name: etgq locale: es notes: "" + publication_scheme: "" -geraldine_en_public_body_translation: +geraldine_en_public_body_translation: name: Geraldine Quango first_letter: G request_email: geraldine-requests@localhost @@ -19,8 +20,9 @@ geraldine_en_public_body_translation: url_name: tgq locale: en notes: "" + publication_scheme: "" -humpadink_es_public_body_translation: +humpadink_es_public_body_translation: name: "El Department for Humpadinking" first_letter: E request_email: humpadink-requests@localhost @@ -30,8 +32,9 @@ humpadink_es_public_body_translation: url_name: edfh locale: es notes: Baguette + publication_scheme: "" -humpadink_en_public_body_translation: +humpadink_en_public_body_translation: name: "Department for Humpadinking" first_letter: D request_email: humpadink-requests@localhost @@ -41,8 +44,9 @@ humpadink_en_public_body_translation: url_name: dfh locale: en notes: An albatross told me!!! + publication_scheme: "" -forlorn_en_public_body_translation: +forlorn_en_public_body_translation: name: "Department of Loneliness" first_letter: D request_email: forlorn-requests@localhost diff --git a/spec/models/public_body_spec.rb b/spec/models/public_body_spec.rb index 9e22a9c43..011824190 100644 --- a/spec/models/public_body_spec.rb +++ b/spec/models/public_body_spec.rb @@ -407,6 +407,7 @@ describe PublicBody, " when loading CSV files" do end describe PublicBody do + describe "calculated home page" do it "should return the home page verbatim if it's present" do public_body = PublicBody.new @@ -438,4 +439,17 @@ describe PublicBody do public_body.calculated_home_page.should == "https://example.com" end end + + describe 'when asked for notes without html' do + + before do + @public_body = PublicBody.new(:notes => 'some <a href="/notes">notes</a>') + end + + it 'should remove simple tags from notes' do + @public_body.notes_without_html.should == 'some notes' + end + + end + end |