diff options
-rw-r--r-- | app/models/public_body.rb | 20 | ||||
-rw-r--r-- | spec/models/public_body_spec.rb | 21 |
2 files changed, 40 insertions, 1 deletions
diff --git a/app/models/public_body.rb b/app/models/public_body.rb index 5fff4a77f..3da901e83 100644 --- a/app/models/public_body.rb +++ b/app/models/public_body.rb @@ -25,7 +25,7 @@ # Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved. # Email: francis@mysociety.org; WWW: http://www.mysociety.org/ # -# $Id: public_body.rb,v 1.135 2009-03-18 05:04:47 francis Exp $ +# $Id: public_body.rb,v 1.136 2009-03-22 09:03:18 tony Exp $ require 'csv' require 'set' @@ -131,6 +131,24 @@ class PublicBody < ActiveRecord::Base Hash[*self.categories_with_description.map() { |a| [a[0],a[2]] }.flatten] end + # like find_by_url_name but also search historic url_name if none found + def self.find_by_urlname(name) + found = PublicBody.find_all_by_url_name(name) + 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 none found, then search the history of short names + old = PublicBody::Version.find_all_by_url_name(name) + # :conditions => [ "id in (select public_body_id from public_body_versions where url_name = ?)", name ]) + # Maybe return the first one, so we show something relevant, + # rather than throwing an error? + raise "Two bodies with the same historical URL name: #{name}" if old.size > 1 + return unless old.size == 1 + # does acts_as_versioned provide a method that returns the current version? + return PublicBody.find(old.first.public_body_id) + end + + # Set the first letter, which is used for faster queries before_save(:set_first_letter) def set_first_letter diff --git a/spec/models/public_body_spec.rb b/spec/models/public_body_spec.rb index b2a62cd8d..bcce59471 100644 --- a/spec/models/public_body_spec.rb +++ b/spec/models/public_body_spec.rb @@ -28,6 +28,27 @@ describe PublicBody, " when saving" do end end +describe PublicBody, "when searching" do + fixtures :public_bodies + + it "should find by existing url name" do + body = PublicBody.find_by_urlname('dfh') + body.id.should == 3 + end + + it "should find by historic url name" do + body = PublicBody.find_by_urlname('hdink') + body.id.should == 3 + body.class.to_s.should == 'PublicBody' + end + + it "should cope with not finding any" do + body = PublicBody.find_by_urlname('idontexist') + body.should be_nil + end + +end + describe PublicBody, " when indexing with Xapian" do fixtures :public_bodies |