aboutsummaryrefslogtreecommitdiffstats
path: root/spec/models/public_body_spec.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/models/public_body_spec.rb')
-rw-r--r--spec/models/public_body_spec.rb259
1 files changed, 71 insertions, 188 deletions
diff --git a/spec/models/public_body_spec.rb b/spec/models/public_body_spec.rb
index b90696c25..7b55efda1 100644
--- a/spec/models/public_body_spec.rb
+++ b/spec/models/public_body_spec.rb
@@ -30,212 +30,81 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe PublicBody do
- describe :type_of_authority do
+ describe :translations_attributes= 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
+ context 'translation_attrs is a Hash' 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')
+ it 'does not persist translations' do
+ body = FactoryGirl.create(:public_body)
+ body.translations_attributes = { :es => { :locale => 'es',
+ :name => 'El Body' } }
- expect(public_body.type_of_authority).to eq('Spec category')
- end
-
- end
+ expect(PublicBody.find(body.id).translations.size).to eq(1)
+ end
- context 'when associated with several categories' do
+ it 'creates a new translation' do
+ body = FactoryGirl.create(:public_body)
+ body.translations_attributes = { :es => { :locale => 'es',
+ :name => 'El Body' } }
+ body.save
+ body.reload
+ expect(body.name(:es)).to eq('El Body')
+ end
- 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')
+ it 'updates an existing translation' do
+ body = FactoryGirl.create(:public_body)
+ body.translations_attributes = { 'es' => { :locale => 'es',
+ :name => 'El Body' } }
+ body.save
+
+ body.translations_attributes = { 'es' => { :id => body.translation_for(:es).id,
+ :locale => 'es',
+ :name => 'Renamed' } }
+ body.save
+ expect(body.name(:es)).to eq('Renamed')
+ end
- description = 'Spec category 0, spec category 1 and spec category 2'
- expect(public_body.type_of_authority).to eq(description)
- end
+ it 'updates an existing translation and creates a new translation' do
+ body = FactoryGirl.create(:public_body)
+ body.translations.create(:locale => 'es',
+ :name => 'El Body')
- end
+ expect(body.translations.size).to eq(2)
- context 'when the html parameter is true' do
+ body.translations_attributes = {
+ 'es' => { :id => body.translation_for(:es).id,
+ :locale => 'es',
+ :name => 'Renamed' },
+ 'fr' => { :locale => 'fr',
+ :name => 'Le Body' }
+ }
- context 'when associated with one category' do
+ expect(body.translations.size).to eq(3)
+ I18n.with_locale(:es) { expect(body.name).to eq('Renamed') }
+ I18n.with_locale(:fr) { expect(body.name).to eq('Le Body') }
+ end
- 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')
+ it 'skips empty translations' do
+ body = FactoryGirl.create(:public_body)
+ body.translations.create(:locale => 'es',
+ :name => 'El Body')
- anchor = %Q(<a href="/body/list/spec">Spec category</a>)
- expect(public_body.type_of_authority(true)).to eq(anchor)
- end
+ expect(body.translations.size).to eq(2)
- end
+ body.translations_attributes = {
+ 'es' => { :id => body.translation_for(:es).id,
+ :locale => 'es',
+ :name => 'Renamed' },
+ 'fr' => { :locale => 'fr' }
+ }
- 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)
+ expect(body.translations.size).to eq(2)
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
-
- it 'takes the correct code path for a Hash' do
- attrs = {}
- attrs.should_receive(:each_value)
- PublicBody.new().translations_attributes = attrs
- end
-
- it 'updates an existing translation' do
- body = public_bodies(:geraldine_public_body)
- translation = body.translation_for(:es)
- params = { 'es' => { :locale => 'es',
- :name => 'Renamed' } }
-
- body.translations_attributes = params
- I18n.with_locale(:es) { expect(body.name).to eq('Renamed') }
- end
-
- it 'updates an existing translation and creates a new translation' do
- body = public_bodies(:geraldine_public_body)
- translation = body.translation_for(:es)
-
- expect(body.translations.size).to eq(2)
-
- body.translations_attributes = {
- 'es' => { :locale => 'es',
- :name => 'Renamed' },
- 'fr' => { :locale => 'fr',
- :name => 'Le Geraldine Quango' }
- }
-
- expect(body.translations.size).to eq(3)
- I18n.with_locale(:es) { expect(body.name).to eq('Renamed') }
- I18n.with_locale(:fr) { expect(body.name).to eq('Le Geraldine Quango') }
- end
-
- it 'skips empty translations' do
- body = public_bodies(:geraldine_public_body)
- translation = body.translation_for(:es)
-
- expect(body.translations.size).to eq(2)
-
- body.translations_attributes = {
- 'es' => { :locale => 'es',
- :name => 'Renamed' },
- 'fr' => { :locale => 'fr' }
- }
-
- expect(body.translations.size).to eq(2)
- end
-
- end
-
- context 'translation_attrs is an Array' do
-
- it 'takes the correct code path for an Array' do
- attrs = []
- attrs.should_receive(:each)
- PublicBody.new().translations_attributes = attrs
- end
-
- it 'creates a new translation' do
- body = public_bodies(:geraldine_public_body)
- body.translation_for(:es).destroy
- body.reload
-
- expect(body.translations.size).to eq(1)
-
- body.translations_attributes = [ {
- :locale => 'es',
- :name => 'Renamed'
- }
- ]
-
- expect(body.translations.size).to eq(2)
- I18n.with_locale(:es) { expect(body.name).to eq('Renamed') }
- end
-
- it 'skips empty translations' do
- body = public_bodies(:geraldine_public_body)
- body.translation_for(:es).destroy
- body.reload
-
- expect(body.translations.size).to eq(1)
-
- body.translations_attributes = [
- { :locale => 'empty' }
- ]
-
- expect(body.translations.size).to eq(1)
- end
-
- end
-
- end
-
end
+
describe PublicBody, " using tags" do
before do
@public_body = PublicBody.new(:name => 'Aardvark Monitoring Service',
@@ -1370,3 +1239,17 @@ describe PublicBody do
end
+describe PublicBody::Translation do
+
+ it 'requires a locale' do
+ translation = PublicBody::Translation.new
+ translation.valid?
+ expect(translation.errors[:locale]).to eq(["can't be blank"])
+ end
+
+ it 'is valid if all required attributes are assigned' do
+ translation = PublicBody::Translation.new(:locale => I18n.default_locale)
+ expect(translation).to be_valid
+ end
+
+end