diff options
author | Louise Crow <louise.crow@gmail.com> | 2015-03-05 13:41:50 +0000 |
---|---|---|
committer | Louise Crow <louise.crow@gmail.com> | 2015-03-05 13:41:50 +0000 |
commit | 7bc05735fd6b138d09e92be08979c86525b1b0bd (patch) | |
tree | 3317d7ae235673b322eacd3d75642f5daa62791f | |
parent | 13408b4569f8c164e275cb594920813ec8210aec (diff) | |
parent | 0947b1445ce4e6ac73e2a7a1536ab4aac775f6a6 (diff) |
Merge branch '59-localised-authority-links' into rails-3-develop
Conflicts:
app/views/public_body/show.html.erb
-rw-r--r-- | app/helpers/public_body_helper.rb | 26 | ||||
-rw-r--r-- | app/models/public_body.rb | 29 | ||||
-rw-r--r-- | app/views/public_body/show.html.erb | 2 | ||||
-rw-r--r-- | spec/helpers/public_body_helper_spec.rb | 93 | ||||
-rw-r--r-- | spec/models/public_body_spec.rb | 106 | ||||
-rw-r--r-- | spec/views/public_body/show.html.erb_spec.rb | 2 |
6 files changed, 120 insertions, 138 deletions
diff --git a/app/helpers/public_body_helper.rb b/app/helpers/public_body_helper.rb index 4a71f89ab..332e93284 100644 --- a/app/helpers/public_body_helper.rb +++ b/app/helpers/public_body_helper.rb @@ -32,4 +32,30 @@ module PublicBodyHelper reasons.compact end + # Use tags to describe what type of authority a PublicBody is. + # + # public_body - Instance of a PublicBody + # + # Returns a string + def type_of_authority(public_body) + types = public_body.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 index.zero? + desc = desc.sub(/\S/) { |m| Unicode.upcase(m) } + end + link_to(desc, list_public_bodies_path(tag.name)) + end + end + + types.compact! + + if types.any? + types.to_sentence(:last_word_connector => ' and ').html_safe + else + _("A public authority") + end + end + end diff --git a/app/models/public_body.rb b/app/models/public_body.rb index c023b436c..0e90a3c16 100644 --- a/app/models/public_body.rb +++ b/app/models/public_body.rb @@ -345,35 +345,6 @@ class PublicBody < ActiveRecord::Base end end - - # Use tags to describe what type of thing this is - def type_of_authority(html = false) - 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 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 - - desc - end - end - - types.compact! - - if types.any? - types.to_sentence(:last_word_connector => ' and ').html_safe - else - _("A public authority") - end - end - # Guess home page from the request email, or use explicit override, or nil # if not known. def calculated_home_page diff --git a/app/views/public_body/show.html.erb b/app/views/public_body/show.html.erb index 5c11c9428..016c2460d 100644 --- a/app/views/public_body/show.html.erb +++ b/app/views/public_body/show.html.erb @@ -13,7 +13,7 @@ <div class="authority__header"> <h1><%=h(@public_body.name)%></h1> <p class="authority__header__subtitle"> - <%=@public_body.type_of_authority(true)%><% if not @public_body.short_name.empty? %>, + <%= type_of_authority(@public_body) %><% if not @public_body.short_name.empty? %>, <%= _('also called {{public_body_short_name}}', :public_body_short_name => h(@public_body.short_name))%><% end %> <% if !@user.nil? && @user.admin_page_links? %> (<%= link_to _("admin"), admin_body_path(@public_body) %>) diff --git a/spec/helpers/public_body_helper_spec.rb b/spec/helpers/public_body_helper_spec.rb index 89a4d0641..0bf55abb4 100644 --- a/spec/helpers/public_body_helper_spec.rb +++ b/spec/helpers/public_body_helper_spec.rb @@ -1,3 +1,4 @@ +# encoding: UTF-8 require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') describe PublicBodyHelper do @@ -23,7 +24,7 @@ describe PublicBodyHelper do @body.tag_string = 'defunct' msg = 'This authority no longer exists, so you cannot make a request to it.' expect(public_body_not_requestable_reasons(@body)).to include(msg) - end + end it 'links to the request page if the body has no contact email' do @body.request_email = '' @@ -47,4 +48,94 @@ describe PublicBodyHelper do end + + describe :type_of_authority do + + it 'falls back to "A public authority"' do + public_body = FactoryGirl.build(:public_body) + expect(type_of_authority(public_body)).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(type_of_authority(public_body)).to eq('<a href="/body/list/spec">Ünicode category</a>') + 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') + expected = '<a href="/body/list/spec_0">Spec category 0</a> and <a href="/body/list/spec_2">spec category 2</a>' + expect(type_of_authority(public_body)).to eq(expected) + end + + + 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(type_of_authority(public_body)).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(type_of_authority(public_body)).to eq(description) + end + + end + + context 'when in a non-default locale' do + + it 'creates the anchor href in the correct locale' do + # Activate the routing filter, normally turned off for helper tests + RoutingFilter.active = true + 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="/es/body/list/spec">Spec category</a>) + I18n.with_locale(:es) { expect(type_of_authority(public_body) +).to eq(anchor) } + end + + end + + end + end diff --git a/spec/models/public_body_spec.rb b/spec/models/public_body_spec.rb index b90696c25..d91021315 100644 --- a/spec/models/public_body_spec.rb +++ b/spec/models/public_body_spec.rb @@ -30,112 +30,6 @@ 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 - describe :translations_attributes= do context 'translation_attrs is a Hash' do diff --git a/spec/views/public_body/show.html.erb_spec.rb b/spec/views/public_body/show.html.erb_spec.rb index e4217ca4d..6ebc39caa 100644 --- a/spec/views/public_body/show.html.erb_spec.rb +++ b/spec/views/public_body/show.html.erb_spec.rb @@ -7,7 +7,7 @@ describe "public_body/show" do :short_name => 'tq', :url_name => 'testquango', :notes => '', - :type_of_authority => 'A public body', + :tags => [], :eir_only? => nil, :info_requests => [1, 2, 3, 4], # out of sync with Xapian :publication_scheme => '', |