diff options
author | Mark Longair <mhl@pobox.com> | 2013-10-04 17:29:21 +0100 |
---|---|---|
committer | Mark Longair <mhl@pobox.com> | 2013-10-04 17:29:21 +0100 |
commit | 6a857d9a42629da7c430ddf6c865cb7eb73a8901 (patch) | |
tree | 7cefba76990385b8acb51594cbff1baccd2e739a | |
parent | abfbb63875850922052da6e0626bdba4e137af65 (diff) |
Fix upcasing of a non-US-ASCII first letter under Ruby 1.8
In the rare circumstance that someone created a public body
whose name started with a lower case letter outside [a-z]
with Alaveteli running under Ruby 1.8, the letter would not be
upcased correctly before saving to the first_letter column.
This commit fixes that by using a Unicode-aware upcase function.
-rw-r--r-- | app/models/public_body.rb | 2 | ||||
-rw-r--r-- | spec/models/public_body_spec.rb | 11 |
2 files changed, 12 insertions, 1 deletions
diff --git a/app/models/public_body.rb b/app/models/public_body.rb index 828e8c94a..485a794b0 100644 --- a/app/models/public_body.rb +++ b/app/models/public_body.rb @@ -71,7 +71,7 @@ class PublicBody < ActiveRecord::Base def PublicBody.set_first_letter(instance) unless instance.name.nil? or instance.name.empty? # we use a regex to ensure it works with utf-8/multi-byte - first_letter = instance.name.scan(/^./mu)[0].upcase + first_letter = Unicode.upcase instance.name.scan(/^./mu)[0] if first_letter != instance.first_letter instance.first_letter = first_letter end diff --git a/spec/models/public_body_spec.rb b/spec/models/public_body_spec.rb index b0cb15f27..0324e3f5a 100644 --- a/spec/models/public_body_spec.rb +++ b/spec/models/public_body_spec.rb @@ -190,6 +190,17 @@ describe PublicBody, " when saving" do @public_body.first_letter.should == 'T' end + it "should update first letter, even if it's a multibyte character" do + pb = PublicBody.new(:name => 'åccents, lower-case', + :short_name => 'ALC', + :request_email => 'foo@localhost', + :last_edit_editor => 'test', + :last_edit_comment => '') + pb.first_letter.should be_nil + pb.save! + pb.first_letter.should == 'Å' + end + it "should save the name when renaming an existing public body" do public_body = public_bodies(:geraldine_public_body) public_body.name = "Mark's Public Body" |