diff options
author | Gareth Rees <gareth@mysociety.org> | 2015-01-23 15:12:50 +0000 |
---|---|---|
committer | Gareth Rees <gareth@mysociety.org> | 2015-02-04 15:02:00 +0000 |
commit | 93c137a906715c194dc64044556c148d813c87a3 (patch) | |
tree | c7479f296aa16b9eb50a0bcea6edd08daa5104dc | |
parent | 3fe8f773df749988a1d88141330e65b13eb43ab6 (diff) |
Nicer capitalize first letter only
String#capitalize downcases remaining letters, so:
> 'heLLo WorLd'.capitalize
# => "Hello world"
Our version only works on the first character of the String, preserving
the case of the rest of the String:
> 'heLLo WorLd'.sub(/\S/) { |m| Unicode.upcase(m) }
# => 'HeLLo WorLd'
Also handle unicode.
-rw-r--r-- | app/models/public_body.rb | 9 | ||||
-rw-r--r-- | spec/models/public_body_spec.rb | 11 |
2 files changed, 15 insertions, 5 deletions
diff --git a/app/models/public_body.rb b/app/models/public_body.rb index f6d182878..ee38a7269 100644 --- a/app/models/public_body.rb +++ b/app/models/public_body.rb @@ -341,12 +341,11 @@ class PublicBody < ActiveRecord::Base tags.each do |tag| if PublicBodyCategory.get().by_tag().include?(tag.name) desc = PublicBodyCategory.get().singular_by_tag()[tag.name] - if first - # terrible that Ruby/Rails doesn't have an equivalent of ucfirst - # (capitalize shockingly converts later characters to lowercase) - desc = desc[0,1].capitalize + desc[1,desc.size] - first = false + + if index.zero? + desc = desc.sub(/\S/) { |m| Unicode.upcase(m) } end + if html # TODO: this should call proper route helpers, but is in model sigh desc = '<a href="/body/list/' + tag.name + '">' + desc + '</a>' diff --git a/spec/models/public_body_spec.rb b/spec/models/public_body_spec.rb index db38e3a15..3415dc28b 100644 --- a/spec/models/public_body_spec.rb +++ b/spec/models/public_body_spec.rb @@ -37,6 +37,17 @@ describe PublicBody do expect(public_body.type_of_authority).to eq('A public authority') end + it 'handles Unicode' do + category = FactoryGirl.create(:public_body_category, :category_tag => 'spec', + :description => 'ünicode category') + heading = FactoryGirl.create(:public_body_heading) + heading.add_category(category) + public_body = FactoryGirl.create(:public_body, :tag_string => 'spec') + + + expect(public_body.type_of_authority).to eq('Ünicode category') + end + context 'when associated with one category' do it 'returns the capitalised category description' do |