diff options
-rw-r--r-- | app/controllers/public_body_controller.rb | 103 | ||||
-rw-r--r-- | spec/controllers/admin_public_body_controller_spec.rb | 54 | ||||
-rw-r--r-- | spec/controllers/public_body_controller_spec.rb | 22 | ||||
-rw-r--r-- | spec/fixtures/public_body_translations.yml | 12 |
4 files changed, 137 insertions, 54 deletions
diff --git a/app/controllers/public_body_controller.rb b/app/controllers/public_body_controller.rb index 17eba911f..439e9d579 100644 --- a/app/controllers/public_body_controller.rb +++ b/app/controllers/public_body_controller.rb @@ -16,87 +16,96 @@ class PublicBodyController < ApplicationController return end - @public_body = PublicBody.find_by_url_name_with_historic(params[:url_name]) - raise "None found" if @public_body.nil? # XXX proper 404 + @locale = self.locale_from_params() + PublicBody.with_locale(@locale) do + @public_body = PublicBody.find_by_url_name_with_historic(params[:url_name]) + raise "None found" if @public_body.nil? # XXX proper 404 - # If found by historic name, redirect to new name - redirect_to show_public_body_url(:url_name => @public_body.url_name) if - @public_body.url_name != params[:url_name] + # If found by historic name, redirect to new name + redirect_to show_public_body_url(:url_name => @public_body.url_name) if + @public_body.url_name != params[:url_name] - set_last_body(@public_body) + set_last_body(@public_body) - top_url = main_url("/") - @searched_to_send_request = false - referrer = request.env['HTTP_REFERER'] - if !referrer.nil? && referrer.match(%r{^#{top_url}search/.*/bodies$}) - @searched_to_send_request = true - end + top_url = main_url("/") + @searched_to_send_request = false + referrer = request.env['HTTP_REFERER'] + if !referrer.nil? && referrer.match(%r{^#{top_url}search/.*/bodies$}) + @searched_to_send_request = true + end - # Use search query for this so can collapse and paginate easily - # XXX really should just use SQL query here rather than Xapian. - begin - @xapian_requests = perform_search([InfoRequestEvent], 'requested_from:' + @public_body.url_name, 'newest', 'request_collapse') - if (@page > 1) - @page_desc = " (page " + @page.to_s + ")" - else - @page_desc = "" + # Use search query for this so can collapse and paginate easily + # XXX really should just use SQL query here rather than Xapian. + begin + @xapian_requests = perform_search([InfoRequestEvent], 'requested_from:' + @public_body.url_name, 'newest', 'request_collapse') + if (@page > 1) + @page_desc = " (page " + @page.to_s + ")" + else + @page_desc = "" + end + rescue + @xapian_requests = nil end - rescue - @xapian_requests = nil - end - @track_thing = TrackThing.create_track_for_public_body(@public_body) - @feed_autodetect = [ { :url => do_track_url(@track_thing, 'feed'), :title => @track_thing.params[:title_in_rss], :has_json => true } ] + @track_thing = TrackThing.create_track_for_public_body(@public_body) + @feed_autodetect = [ { :url => do_track_url(@track_thing, 'feed'), :title => @track_thing.params[:title_in_rss], :has_json => true } ] - respond_to do |format| - format.html { @has_json = true } - format.json { render :json => @public_body.json_for_api } + respond_to do |format| + format.html { @has_json = true } + format.json { render :json => @public_body.json_for_api } + end end end def view_email @public_bodies = PublicBody.find(:all, :conditions => [ "url_name = ?", params[:url_name] ]) @public_body = @public_bodies[0] - - if params[:submitted_view_email] - if verify_recaptcha - flash.discard(:error) - render :template => "public_body/view_email" - return + PublicBody.with_locale(self.locale_from_params()) do + if params[:submitted_view_email] + if verify_recaptcha + flash.discard(:error) + render :template => "public_body/view_email" + return + end + flash.now[:error] = "There was an error with the words you entered, please try again." end - flash.now[:error] = "There was an error with the words you entered, please try again." + render :template => "public_body/view_email_captcha" end - render :template => "public_body/view_email_captcha" end def list # XXX move some of these tag SQL queries into has_tag_string.rb @tag = params[:tag] + @locale = self.locale_from_params() + locale_condition = 'public_body_translations.locale = ?' if @tag.nil? @tag = "all" - conditions = [] + conditions = [locale_condition, @locale] elsif @tag == 'other' category_list = PublicBodyCategories::CATEGORIES.map{|c| "'"+c+"'"}.join(",") - conditions = ['(select count(*) from has_tag_string_tags where has_tag_string_tags.model_id = public_bodies.id + conditions = [locale_condition + ' AND (select count(*) from has_tag_string_tags where has_tag_string_tags.model_id = public_bodies.id and has_tag_string_tags.model = \'PublicBody\' - and has_tag_string_tags.name in (' + category_list + ')) = 0'] + and has_tag_string_tags.name in (' + category_list + ')) = 0', @locale] elsif @tag.size == 1 @tag.upcase! - conditions = ['first_letter = ?', @tag] + conditions = [locale_condition + ' AND first_letter = ?', @locale, @tag] elsif @tag.include?(":") name, value = HasTagString::HasTagStringTag.split_tag_into_name_value(@tag) - conditions = ['(select count(*) from has_tag_string_tags where has_tag_string_tags.model_id = public_bodies.id + conditions = [locale_condition + ' AND (select count(*) from has_tag_string_tags where has_tag_string_tags.model_id = public_bodies.id and has_tag_string_tags.model = \'PublicBody\' - and has_tag_string_tags.name = ? and has_tag_string_tags.value = ?) > 0', name, value] + and has_tag_string_tags.name = ? and has_tag_string_tags.value = ?) > 0', @locale, name, value] else - conditions = ['(select count(*) from has_tag_string_tags where has_tag_string_tags.model_id = public_bodies.id + conditions = [locale_condition + ' AND (select count(*) from has_tag_string_tags where has_tag_string_tags.model_id = public_bodies.id and has_tag_string_tags.model = \'PublicBody\' - and has_tag_string_tags.name = ?) > 0', @tag] + and has_tag_string_tags.name = ?) > 0', @locale, @tag] end - @public_bodies = PublicBody.paginate( - :order => "public_bodies.name", :page => params[:page], :per_page => 1000, # fit all councils on one page - :conditions => conditions + PublicBody.with_locale(@locale) do + @public_bodies = PublicBody.paginate( + :order => "public_body_translations.name", :page => params[:page], :per_page => 1000, # fit all councils on one page + :conditions => conditions, + :joins => :translations ) + end if @tag.size == 1 @description = "beginning with '" + @tag + "'" else diff --git a/spec/controllers/admin_public_body_controller_spec.rb b/spec/controllers/admin_public_body_controller_spec.rb index db2e449f8..a48367ae1 100644 --- a/spec/controllers/admin_public_body_controller_spec.rb +++ b/spec/controllers/admin_public_body_controller_spec.rb @@ -43,3 +43,57 @@ describe AdminPublicBodyController, "when administering public bodies" do end + +describe AdminPublicBodyController, "when administering public bodies with i18n" do + integrate_views + fixtures :public_bodies, :public_body_translations + + it "shows the index page" do + get :index + end + + it "searches for 'humpa'" do + get :index, {:query => "humpa", :locale => "es"} + assigns[:public_bodies].should == [ public_bodies(:humpadink_public_body) ] + end + + it "shows a public body" do + get :show, {:id => 2, :locale => "es" } + end + + it "creates a new public body" do + PublicBody.count.should == 2 + post :create, { :public_body => { :name => "New Quango", :short_name => "", :tag_string => "blah", :request_email => 'newquango@localhost', :last_edit_comment => 'From test code' } } + PublicBody.count.should == 3 + end + + it "edits a public body" do + get :edit, {:id => 3, :locale => 'es'} + response.body.should include('Baguette') + end + + it "saves edits to a public body" do + PublicBody.with_locale(:es) do + pb = PublicBody.find(id=3) + pb.name.should == "El Department for Humpadinking" + end + + post :update, { :id => 3, :public_body => { :name => "Renamed", :short_name => "", :tag_string => "some tags", :request_email => 'edited@localhost', :last_edit_comment => 'From test code' }, :locale => "es" } + response.flash[:notice].should include('successful') + pb = PublicBody.find(public_bodies(:humpadink_public_body).id) + PublicBody.with_locale(:es) do + pb.name.should == "Renamed" + end + PublicBody.with_locale(:en) do + pb.name.should == "Department for Humpadinking" + end + end + + it "destroy a public body" do + PublicBody.count.should == 2 + post :destroy, { :id => 3 } + PublicBody.count.should == 1 + end + + +end diff --git a/spec/controllers/public_body_controller_spec.rb b/spec/controllers/public_body_controller_spec.rb index fbab832f6..6b55bc09a 100644 --- a/spec/controllers/public_body_controller_spec.rb +++ b/spec/controllers/public_body_controller_spec.rb @@ -4,7 +4,7 @@ require 'json' describe PublicBodyController, "when showing a body" do integrate_views - fixtures :public_bodies, :public_body_versions + fixtures :public_bodies, :public_body_versions, :public_body_translations it "should be successful" do get :show, :url_name => "dfh" @@ -21,6 +21,16 @@ describe PublicBodyController, "when showing a body" do assigns[:public_body].should == public_bodies(:humpadink_public_body) end + it "should assign the body using different locale from that used for url_name" do + get :show, {:url_name => "dfh", :locale => 'es'} + assigns[:public_body].notes.should == "Baguette" + end + + it "should assign the body using same locale as that used in url_name" do + get :show, {:url_name => "edfh", :locale => 'es'} + assigns[:public_body].notes.should == "Baguette" + end + it "should redirect to newest name if you use historic name of public body in URL" do get :show, :url_name => "hdink" response.should redirect_to(:controller => 'public_body', :action => 'show', :url_name => "dfh") @@ -51,6 +61,16 @@ describe PublicBodyController, "when listing bodies" do assigns[:description].should == "all" end + it "should list bodies in alphabetical order with different locale" do + get :list, :locale => "es" + + response.should render_template('list') + + assigns[:public_bodies].should == [ public_bodies(:geraldine_public_body), public_bodies(:humpadink_public_body) ] + assigns[:tag].should == "all" + assigns[:description].should == "all" + end + it "should list a tagged thing on the appropriate list page, and others on the other page, and all still on the all page" do public_bodies(:humpadink_public_body).tag_string = "foo local_council" diff --git a/spec/fixtures/public_body_translations.yml b/spec/fixtures/public_body_translations.yml index 225994041..b5e947044 100644 --- a/spec/fixtures/public_body_translations.yml +++ b/spec/fixtures/public_body_translations.yml @@ -1,5 +1,5 @@ -geraldine_public_body_translation: - name: El Geraldine Quango +geraldine_es_public_body_translation: + name: El A Geraldine Quango first_letter: E request_email: geraldine-requests@localhost id: "1" @@ -9,7 +9,7 @@ geraldine_public_body_translation: locale: es notes: "" -geraldine_public_body_translation: +geraldine_en_public_body_translation: name: Geraldine Quango first_letter: G request_email: geraldine-requests@localhost @@ -20,7 +20,7 @@ geraldine_public_body_translation: locale: en notes: "" -humpadink_public_body_translation: +humpadink_es_public_body_translation: name: "El Department for Humpadinking" first_letter: E request_email: humpadink-requests@localhost @@ -29,9 +29,9 @@ humpadink_public_body_translation: short_name: eDfH url_name: edfh locale: es - notes: An albatross told me!!! + notes: Baguette -humpadink_public_body_translation: +humpadink_en_public_body_translation: name: "Department for Humpadinking" first_letter: D request_email: humpadink-requests@localhost |