diff options
author | Mark Longair <mhl@pobox.com> | 2013-08-29 10:24:02 +0100 |
---|---|---|
committer | Mark Longair <mhl@pobox.com> | 2013-09-10 14:59:58 +0100 |
commit | 3fb80a12652792df9da06c22b4bb944ab80bf376 (patch) | |
tree | 7d513312a0df678fae06fff3ab88d85c161bec90 | |
parent | 17199cf44fd4ad8dcf700a13126607c42bb3be34 (diff) |
Make falling back to default locale in public body listings optional
As the code stood, the list method in PublicBodyController would only
return results that had translations of the public body in the default
locale. This has a variety of problems if you're viewing pages in the
non-default locale - for example, the "first letter" links wouldn't
bring up the public bodies that began with that letter in the current
locale, only those that began with it in the default locale.
Ideally, every public body would be translated into every available
locale for the site, but there are cases where deployers wish to have
public body listings also include those from the default locale, in
case there are untralsated public bodies:
https://groups.google.com/d/msg/alaveteli-dev/zUY_USaAMAM/M7KTQ9RC5YUJ
This commit makes the default behaviour to look for public body
listings only in the current locale, but if the new configuration
option PUBLIC_BODY_LIST_FALLBACK_TO_DEFAULT_LOCALE is set, then public
body listings will be looked for in both the current locale and the
default locale.
Fixes #1000
-rw-r--r-- | app/controllers/public_body_controller.rb | 37 | ||||
-rw-r--r-- | config/general.yml-example | 5 | ||||
-rw-r--r-- | lib/configuration.rb | 1 | ||||
-rw-r--r-- | spec/controllers/public_body_controller_spec.rb | 9 |
4 files changed, 38 insertions, 14 deletions
diff --git a/app/controllers/public_body_controller.rb b/app/controllers/public_body_controller.rb index e51edc7bd..335dddf65 100644 --- a/app/controllers/public_body_controller.rb +++ b/app/controllers/public_body_controller.rb @@ -87,32 +87,42 @@ class PublicBodyController < ApplicationController # XXX move some of these tag SQL queries into has_tag_string.rb @query = "%#{params[:public_body_query].nil? ? "" : params[:public_body_query]}%" @tag = params[:tag] - @locale = self.locale_from_params() - default_locale = I18n.default_locale.to_s + @locale = self.locale_from_params + underscore_locale = @locale.gsub '-', '_' + underscore_default_locale = I18n.default_locale.to_s.gsub '-', '_' locale_condition = "(upper(public_body_translations.name) LIKE upper(?)" \ - " OR upper(public_body_translations.notes) LIKE upper (?))" \ - " AND public_body_translations.locale = ?" \ - " AND public_bodies.id <> #{PublicBody.internal_admin_body.id}" + " OR upper(public_body_translations.notes) LIKE upper (?))" \ + " AND public_bodies.id <> #{PublicBody.internal_admin_body.id}" + condition_parameters = [@query, @query] + if AlaveteliConfiguration::public_body_list_fallback_to_default_locale + locale_condition += " AND (public_body_translations.locale = ? OR public_body_translations.locale = ?)" + condition_parameters.concat [underscore_locale, underscore_default_locale] + else + locale_condition += " AND public_body_translations.locale = ?" + condition_parameters.concat [underscore_locale] + end if @tag.nil? or @tag == "all" @tag = "all" - conditions = [locale_condition, @query, @query, default_locale] elsif @tag == 'other' category_list = PublicBodyCategories::get().tags().map{|c| "'"+c+"'"}.join(",") - conditions = [locale_condition + " AND (SELECT count(*) FROM has_tag_string_tags WHERE has_tag_string_tags.model_id = public_bodies.id" \ + 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", @query, @query, default_locale] + " AND has_tag_string_tags.name in (#{category_list})) = 0" elsif @tag.size == 1 @tag.upcase! - conditions = [locale_condition + " AND public_body_translations.first_letter = ?", @query, @query, default_locale, @tag] + locale_condition += " AND public_body_translations.first_letter = ?" + condition_parameters.concat [@tag] elsif @tag.include?(":") name, value = HasTagString::HasTagStringTag.split_tag_into_name_value(@tag) - conditions = [locale_condition + " AND (SELECT count(*) FROM has_tag_string_tags WHERE has_tag_string_tags.model_id = public_bodies.id" \ + 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", @query, @query, default_locale, name, value] + " AND has_tag_string_tags.name = ? AND has_tag_string_tags.value = ?) > 0" + condition_parameters.concat [name, value] else - conditions = [locale_condition + " AND (SELECT count(*) FROM has_tag_string_tags WHERE has_tag_string_tags.model_id = public_bodies.id" \ + 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", @query, @query, default_locale, @tag] + " AND has_tag_string_tags.name = ?) > 0" + condition_parameters.concat [@tag] end if @tag == "all" @@ -127,6 +137,7 @@ class PublicBodyController < ApplicationController @description = _("in the category ‘{{category_name}}’", :category_name=>category_name) end end + conditions = [locale_condition] + condition_parameters I18n.with_locale(@locale) do @public_bodies = PublicBody.where(conditions).joins(:translations).order("public_body_translations.name").paginate( :page => params[:page], :per_page => 100 diff --git a/config/general.yml-example b/config/general.yml-example index 0753af46b..a47c5dad7 100644 --- a/config/general.yml-example +++ b/config/general.yml-example @@ -192,3 +192,8 @@ MTA_LOG_TYPE: "exim" # this will be included in the message people see when their request is # successful. DONATION_URL: "http://www.mysociety.org/donate/" + +# If only some of the public bodies have been translated into every +# available locale, you can allow a fallback to the default locale for +# listing of public bodies. +PUBLIC_BODY_LIST_FALLBACK_TO_DEFAULT_LOCALE: false diff --git a/lib/configuration.rb b/lib/configuration.rb index 03c4ac616..c949c1051 100644 --- a/lib/configuration.rb +++ b/lib/configuration.rb @@ -47,6 +47,7 @@ module AlaveteliConfiguration :MTA_LOG_TYPE => 'exim', :NEW_RESPONSE_REMINDER_AFTER_DAYS => [3, 10, 24], :OVERRIDE_ALL_PUBLIC_BODY_REQUEST_EMAILS => '', + :PUBLIC_BODY_LIST_FALLBACK_TO_DEFAULT_LOCALE => false, :RAW_EMAILS_LOCATION => 'files/raw_emails', :READ_ONLY => '', :RECAPTCHA_PRIVATE_KEY => 'x', diff --git a/spec/controllers/public_body_controller_spec.rb b/spec/controllers/public_body_controller_spec.rb index 4e1841164..2b921b488 100644 --- a/spec/controllers/public_body_controller_spec.rb +++ b/spec/controllers/public_body_controller_spec.rb @@ -78,7 +78,8 @@ describe PublicBodyController, "when listing bodies" do response.should be_success end - it "should list all bodies from default locale, even when there are no translations for selected locale" do + it "if fallback is requested, should list all bodies from default locale, even when there are no translations for selected locale" do + AlaveteliConfiguration.stub!(:public_body_list_fallback_to_default_locale).and_return(true) I18n.with_locale(:en) do @english_only = PublicBody.new(:name => 'English only', :short_name => 'EO', @@ -96,6 +97,12 @@ describe PublicBodyController, "when listing bodies" do response.should contain('El Department for Humpadinking') end + it 'should not show the internal admin authority' do + PublicBody.internal_admin_body + get :list, {:locale => 'en'} + response.should_not contain('Internal admin authority') + end + it 'should show public body names in the selected locale language if present for a locale with underscores' do AlaveteliLocalization.set_locales('he_IL en', 'en') get :list, {:locale => 'he_IL'} |