aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSeb Bacon <seb.bacon@gmail.com>2012-04-17 11:02:50 +0100
committerSeb Bacon <seb.bacon@gmail.com>2012-04-17 11:02:50 +0100
commit46a84b14bbee61ae7699f9dddd420b978222ff2b (patch)
tree69f0f423a99892c175c9e6c87b86cefbc7a8f184
parent48995a8d786418900cb6552eae009bc5b91f6fd5 (diff)
Work around race condition when setting short_name and name. Fixes #474
-rw-r--r--app/models/public_body.rb7
-rw-r--r--spec/models/public_body_spec.rb5
2 files changed, 11 insertions, 1 deletions
diff --git a/app/models/public_body.rb b/app/models/public_body.rb
index a18af8c69..bc83f88f9 100644
--- a/app/models/public_body.rb
+++ b/app/models/public_body.rb
@@ -234,7 +234,12 @@ class PublicBody < ActiveRecord::Base
end
def update_url_name
- self.url_name = MySociety::Format.simplify_url_part(self.short_or_long_name, 'body')
+ preferred_name = self.short_or_long_name
+ if !preferred_name.nil?
+ # we test it's not nil, because sometimes a race condition
+ # means no name has been set yet
+ self.url_name = MySociety::Format.simplify_url_part(preferred_name, 'body')
+ end
end
# Return the short name if present, or else long name
diff --git a/spec/models/public_body_spec.rb b/spec/models/public_body_spec.rb
index e30916dff..baa1e6186 100644
--- a/spec/models/public_body_spec.rb
+++ b/spec/models/public_body_spec.rb
@@ -169,6 +169,11 @@ describe PublicBody, " when saving" do
@public_body.save!
@public_body.first_letter.should == 'T'
end
+
+ it "should not throw an error if the name is unset at the time the short_name is set" do
+ @public_body.name = nil
+ @public_body.short_name = nil
+ end
end
describe PublicBody, "when searching" do