1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
# == Schema Information
#
# Table name: public_body_categories
#
# id :integer not null, primary key
# category_tag :text not null
#
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe PublicBodyCategory do
context 'when validating' do
it 'should require a title' do
category = PublicBodyCategory.new
category.should_not be_valid
category.errors[:title].should == ["Title can't be blank"]
end
it 'should require a category tag' do
category = PublicBodyCategory.new
category.should_not be_valid
category.errors[:category_tag].should == ["Tag can't be blank"]
end
it 'should require a unique tag' do
existing = FactoryGirl.create(:public_body_category)
PublicBodyCategory.new(:email => existing.category_tag).should_not be_valid
end
it 'should require a description' do
category = PublicBodyCategory.new
category.should_not be_valid
category.errors[:description].should == ["Description can't be blank"]
end
it 'validates the translations' do
category = FactoryGirl.build(:public_body_category)
translation = category.translations.build
expect(category).to_not be_valid
end
it 'uses the base model validation for the default locale' do
category = PublicBodyCategory.new
translation = category.translations.build(:locale => 'en',
:description => 'No title')
category.valid?
translation.valid?
expect(category).to have(1).error_on(:title)
expect(translation).to have(0).errors_on(:title)
end
end
describe :save do
it 'saves translations' do
category = FactoryGirl.build(:public_body_category)
category.translations_attributes = { :es => { :locale => 'es',
:title => 'El Category',
:description => 'Spanish description' } }
category.save
expect(PublicBodyCategory.find(category.id).translations.size).to eq(2)
end
end
describe :translations_attributes= do
context 'translation_attrs is a Hash' do
it 'does not persist translations' do
category = FactoryGirl.create(:public_body_category)
category.translations_attributes = { :es => { :locale => 'es',
:title => 'El Category',
:description => 'Spanish description' } }
expect(PublicBodyCategory.find(category.id).translations.size).to eq(1)
end
it 'creates a new translation' do
category = FactoryGirl.create(:public_body_category)
category.translations_attributes = { :es => { :locale => 'es',
:title => 'El Category',
:description => 'Spanish description' } }
category.save
category.reload
expect(category.title(:es)).to eq('El Category')
end
it 'updates an existing translation' do
category = FactoryGirl.create(:public_body_category)
category.translations_attributes = { 'es' => { :locale => 'es',
:title => 'Name',
:description => 'Desc' } }
category.save
category.translations_attributes = { 'es' => { :id => category.translation_for(:es).id,
:locale => 'es',
:title => 'Renamed',
:description => 'Desc' } }
category.save
expect(category.title(:es)).to eq('Renamed')
end
it 'updates an existing translation and creates a new translation' do
category = FactoryGirl.create(:public_body_category)
category.translations.create(:locale => 'es',
:title => 'Los Category',
:description => 'ES Description')
expect(category.translations.size).to eq(2)
category.translations_attributes = {
'es' => { :id => category.translation_for(:es).id,
:locale => 'es',
:title => 'Renamed' },
'fr' => { :locale => 'fr',
:title => 'Le Category' }
}
expect(category.translations.size).to eq(3)
I18n.with_locale(:es) { expect(category.title).to eq('Renamed') }
I18n.with_locale(:fr) { expect(category.title).to eq('Le Category') }
end
it 'skips empty translations' do
category = FactoryGirl.create(:public_body_category)
category.translations.create(:locale => 'es',
:title => 'Los Category',
:description => 'ES Description')
expect(category.translations.size).to eq(2)
category.translations_attributes = {
'es' => { :id => category.translation_for(:es).id,
:locale => 'es',
:title => 'Renamed' },
'fr' => { :locale => 'fr' }
}
expect(category.translations.size).to eq(2)
end
end
end
end
describe PublicBodyCategory::Translation do
it 'requires a locale' do
translation = PublicBodyCategory::Translation.new
translation.valid?
expect(translation.errors[:locale]).to eq(["can't be blank"])
end
it 'is valid if no required attributes are assigned' do
translation = PublicBodyCategory::Translation.new(:locale => I18n.default_locale)
expect(translation).to be_valid
end
it 'requires a title if another required attribute is assigned' do
translation = PublicBodyCategory::Translation.new(:description => 'spec')
translation.valid?
expect(translation.errors[:title]).to eq(["Title can't be blank"])
end
it 'requires a description if another required attribute is assigned' do
translation = PublicBodyCategory::Translation.new(:title => 'spec')
translation.valid?
expect(translation.errors[:description]).to eq(["Description can't be blank"])
end
end
|