aboutsummaryrefslogtreecommitdiffstats
path: root/app/controllers/public_body_controller.rb
diff options
context:
space:
mode:
authorMark Longair <mhl@pobox.com>2013-08-29 10:24:02 +0100
committerMark Longair <mhl@pobox.com>2013-09-10 14:59:58 +0100
commit3fb80a12652792df9da06c22b4bb944ab80bf376 (patch)
tree7d513312a0df678fae06fff3ab88d85c161bec90 /app/controllers/public_body_controller.rb
parent17199cf44fd4ad8dcf700a13126607c42bb3be34 (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
Diffstat (limited to 'app/controllers/public_body_controller.rb')
-rw-r--r--app/controllers/public_body_controller.rb37
1 files changed, 24 insertions, 13 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