aboutsummaryrefslogtreecommitdiffstats
path: root/app/models
diff options
context:
space:
mode:
authorLouise Crow <louise.crow@gmail.com>2014-05-01 18:09:49 +0100
committerLouise Crow <louise.crow@gmail.com>2014-05-01 18:09:49 +0100
commitfd85e6a1d697be8652eb6fdb278969b4afc4923c (patch)
tree690f191d1536937d3f9939a574d8f54597dc8ec4 /app/models
parent7956b0398457f12d645dac9c1be65b0bd29a84dd (diff)
parent4d04209d29b76c997e5238d8546d7eb17ee2bd53 (diff)
Merge branch 'rails-3-develop' of ssh://git.mysociety.org/data/git/public/alaveteli into rails-3-develop
Diffstat (limited to 'app/models')
-rw-r--r--app/models/public_body.rb28
1 files changed, 28 insertions, 0 deletions
diff --git a/app/models/public_body.rb b/app/models/public_body.rb
index 9cb344f14..03ec270ee 100644
--- a/app/models/public_body.rb
+++ b/app/models/public_body.rb
@@ -60,6 +60,34 @@ class PublicBody < ActiveRecord::Base
translates :name, :short_name, :request_email, :url_name, :notes, :first_letter, :publication_scheme
+ # Public: Search for Public Bodies whose name, short_name, request_email or
+ # tags contain the given query
+ #
+ # query - String to query the searchable fields
+ # locale - String to specify the language of the seach query
+ # (default: I18n.locale)
+ #
+ # Returns an ActiveRecord::Relation
+ def self.search(query, locale = I18n.locale)
+ locale = locale.to_s.gsub('-', '_') # Clean the locale string
+
+ sql = <<-SQL
+ (
+ lower(public_body_translations.name) like lower('%'||?||'%')
+ OR lower(public_body_translations.short_name) like lower('%'||?||'%')
+ OR lower(public_body_translations.request_email) like lower('%'||?||'%' )
+ OR lower(has_tag_string_tags.name) like lower('%'||?||'%' )
+ )
+ AND has_tag_string_tags.model_id = public_bodies.id
+ AND has_tag_string_tags.model = 'PublicBody'
+ AND (public_body_translations.locale = ?)
+ SQL
+
+ PublicBody.joins(:translations, :tags).
+ where([sql, query, query, query, query, locale]).
+ uniq
+ end
+
# Convenience methods for creating/editing translations via forms
def find_translation_by_locale(locale)
self.translations.find_by_locale(locale)