diff options
author | Gareth Rees <gareth@mysociety.org> | 2015-02-04 15:06:08 +0000 |
---|---|---|
committer | Gareth Rees <gareth@mysociety.org> | 2015-02-04 15:06:08 +0000 |
commit | 37bb96707f5e17cd59d9b5be950e4304f779ec81 (patch) | |
tree | 0ea2e8851736bd04ab1a8b015944e575b56d3b80 | |
parent | b1b985873a6c24d5341a63941ac85034e8b132ae (diff) | |
parent | 752b3d4fa07c195c2b88e224248392213290600a (diff) |
Merge branch '2112-type-of-authority-locale' into rails-3-develop
-rw-r--r-- | app/models/public_body.rb | 30 | ||||
-rw-r--r-- | spec/models/public_body_spec.rb | 110 |
2 files changed, 123 insertions, 17 deletions
diff --git a/app/models/public_body.rb b/app/models/public_body.rb index a9cdfeab2..d2d50049e 100644 --- a/app/models/public_body.rb +++ b/app/models/public_body.rb @@ -336,33 +336,29 @@ class PublicBody < ActiveRecord::Base # Use tags to describe what type of thing this is def type_of_authority(html = false) - types = [] - first = true - for tag in self.tags + types = tags.each_with_index.map do |tag, index| 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>' end - types.push(desc) + + desc end end - if types.size > 0 - ret = types[0, types.size - 1].join(", ") - if types.size > 1 - ret = ret + " and " - end - ret = ret + types[-1] - return ret.html_safe + + types.compact! + + if types.any? + types.to_sentence(:last_word_connector => ' and ').html_safe else - return _("A public authority") + _("A public authority") end end diff --git a/spec/models/public_body_spec.rb b/spec/models/public_body_spec.rb index 225958cac..510ad59fe 100644 --- a/spec/models/public_body_spec.rb +++ b/spec/models/public_body_spec.rb @@ -28,6 +28,116 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') +describe PublicBody do + + describe :type_of_authority do + + it 'falls back to "A public authority"' do + public_body = FactoryGirl.build(:public_body) + 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 + + it 'constructs the correct string if there are tags which are not categories' do + heading = FactoryGirl.create(:public_body_heading) + 3.times do |i| + category = FactoryGirl.create(:public_body_category, :category_tag => "spec_#{i}", + :description => "spec category #{i}") + heading.add_category(category) + end + public_body = FactoryGirl.create(:public_body, :tag_string => 'spec_0 spec_2 unknown') + + expect(public_body.type_of_authority).to eq('Spec category 0 and spec category 2') + end + + context 'when associated with one category' do + + it 'returns the capitalised category description' do + category = FactoryGirl.create(:public_body_category, :category_tag => 'spec', + :description => 'spec 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('Spec category') + end + + end + + context 'when associated with several categories' do + + it 'joins the category descriptions and capitalizes the first letter' do + heading = FactoryGirl.create(:public_body_heading) + 3.times do |i| + category = FactoryGirl.create(:public_body_category, :category_tag => "spec_#{i}", + :description => "spec category #{i}") + heading.add_category(category) + end + public_body = FactoryGirl.create(:public_body, :tag_string => 'spec_0 spec_1 spec_2') + + description = 'Spec category 0, spec category 1 and spec category 2' + expect(public_body.type_of_authority).to eq(description) + end + + end + + context 'when the html parameter is true' do + + context 'when associated with one category' do + + it 'returns the description wrapped in an anchor tag' do + category = FactoryGirl.create(:public_body_category, :category_tag => 'spec', + :description => 'spec category') + heading = FactoryGirl.create(:public_body_heading) + heading.add_category(category) + public_body = FactoryGirl.create(:public_body, :tag_string => 'spec') + + anchor = %Q(<a href="/body/list/spec">Spec category</a>) + expect(public_body.type_of_authority(true)).to eq(anchor) + end + + end + + context 'when associated with several categories' do + + it 'joins the category descriptions and capitalizes the first letter' do + heading = FactoryGirl.create(:public_body_heading) + 3.times do |i| + category = FactoryGirl.create(:public_body_category, :category_tag => "spec_#{i}", + :description => "spec category #{i}") + heading.add_category(category) + end + public_body = FactoryGirl.create(:public_body, :tag_string => 'spec_0 spec_1 spec_2') + + description = [ + %Q(<a href="/body/list/spec_0">Spec category 0</a>), + ', ', + %Q(<a href="/body/list/spec_1">spec category 1</a>), + ' and ', + %Q(<a href="/body/list/spec_2">spec category 2</a>) + ].join('') + + expect(public_body.type_of_authority(true)).to eq(description) + end + + end + + end + + end + +end + describe PublicBody, " using tags" do before do @public_body = PublicBody.new(:name => 'Aardvark Monitoring Service', |