diff options
-rw-r--r-- | app/models/public_body.rb | 13 | ||||
-rw-r--r-- | spec/models/public_body_spec.rb | 14 |
2 files changed, 21 insertions, 6 deletions
diff --git a/app/models/public_body.rb b/app/models/public_body.rb index ef1d60e26..b75da4331 100644 --- a/app/models/public_body.rb +++ b/app/models/public_body.rb @@ -54,15 +54,16 @@ class PublicBody < ActiveRecord::Base # like find_by_url_name but also search historic url_name if none found def self.find_by_url_name_with_historic(name) - @locale = I18n.locale.to_s - PublicBody.with_locale(@locale) do + locale = self.locale || I18n.locale + PublicBody.with_locale(locale) do found = PublicBody.find(:all, :conditions => ["public_body_translations.url_name='#{name}'"], :joins => :translations, :readonly => false) - return found.first if found.size == 1 - # Shouldn't we just make url_name unique? - raise "Two bodies with the same URL name: #{name}" if found.size > 1 + # If many bodies are found (usually because the url_name is the same across + # locales) return any of them + return found.first if found.size >= 1 + # If none found, then search the history of short names old = PublicBody::Version.find_all_by_url_name(name) # Find unique public bodies in it @@ -138,7 +139,7 @@ class PublicBody < ActiveRecord::Base return 'defunct' elsif self.not_apply? return 'not_apply' - elsif self.request_email.empty? or self.request_email == 'blank' + elsif self.request_email.nil? or self.request_email.empty? or self.request_email == 'blank' return 'bad_contact' else raise "requestable_failure_reason called with type that has no reason" diff --git a/spec/models/public_body_spec.rb b/spec/models/public_body_spec.rb index ec84cbe65..3d00d37fb 100644 --- a/spec/models/public_body_spec.rb +++ b/spec/models/public_body_spec.rb @@ -210,6 +210,20 @@ describe PublicBody, "when searching" do body.id.should == 3 body.class.to_s.should == 'PublicBody' end + + it "should cope with same url_name across multiple locales" do + PublicBody.with_locale(:es) do + # use the unique spanish name to retrieve and edit + body = PublicBody.find_by_url_name_with_historic('etgq') + body.short_name = 'tgq' # Same as english version + body.save! + + # now try to retrieve it + body = PublicBody.find_by_url_name_with_historic('tgq') + body.id.should == public_bodies(:geraldine_public_body).id + body.name.should == "El A Geraldine Quango" + end + end end describe PublicBody, " when loading CSV files" do |