diff options
-rw-r--r-- | app/models/public_body.rb | 7 | ||||
-rw-r--r-- | spec/models/public_body_spec.rb | 12 |
2 files changed, 16 insertions, 3 deletions
diff --git a/app/models/public_body.rb b/app/models/public_body.rb index ee38a7269..76fa3555a 100644 --- a/app/models/public_body.rb +++ b/app/models/public_body.rb @@ -336,9 +336,8 @@ class PublicBody < ActiveRecord::Base # Use tags to describe what type of thing this is def type_of_authority(html = false) - types = [] first = true - tags.each do |tag| + types = tags.map do |tag| if PublicBodyCategory.get().by_tag().include?(tag.name) desc = PublicBodyCategory.get().singular_by_tag()[tag.name] @@ -350,10 +349,12 @@ class PublicBody < ActiveRecord::Base # 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 + types.compact! + if types.any? types.to_sentence(:last_word_connector => ' and ').html_safe else diff --git a/spec/models/public_body_spec.rb b/spec/models/public_body_spec.rb index 3415dc28b..510ad59fe 100644 --- a/spec/models/public_body_spec.rb +++ b/spec/models/public_body_spec.rb @@ -48,6 +48,18 @@ describe PublicBody do 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 |