dm-is-localizable 0.10.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,72 @@
1
+ require 'spec_helper'
2
+
3
+ describe "all available_languages providers", :shared => true do
4
+
5
+ describe "with no translations" do
6
+
7
+ it "should return 0 languages" do
8
+ @provider.available_languages.size.should == 0
9
+ end
10
+
11
+ end
12
+
13
+ describe "with 1 translation in 1 language" do
14
+
15
+ before :each do
16
+ @l = Language.create :code => 'en-US', :name => 'English'
17
+ @t = ItemTranslation.create :item => @item, :language => @l, :name => "Book", :desc => "Literature"
18
+ end
19
+
20
+ it "should return 1 language" do
21
+ @provider.available_languages.size.should == 1
22
+ end
23
+
24
+ it "should return the right language" do
25
+ @provider.available_languages.first.should == @l
26
+ end
27
+
28
+ end
29
+
30
+ describe "with 2 translations in 1 language" do
31
+
32
+ before :each do
33
+ @l = Language.create :code => 'en-US', :name => 'English'
34
+ @t = ItemTranslation.create :item => @item, :language => @l, :name => "Book", :desc => "Literature"
35
+ @t = ItemTranslation.create :item => @item, :language => @l, :name => "Hook", :desc => "Tool"
36
+ @item.reload
37
+ end
38
+
39
+ it "should return 1 language" do
40
+ @provider.available_languages.size.should == 1
41
+ end
42
+
43
+ it "should return the right language" do
44
+ @provider.available_languages.first.should == @l
45
+ end
46
+
47
+ end
48
+
49
+ describe "with 3 translations in 2 languages" do
50
+
51
+ before :each do
52
+ @item2 = Item.create
53
+ @l1 = Language.create :code => 'en-US', :name => 'English'
54
+ @l2 = Language.create :code => 'de-AT', :name => 'Deutsch'
55
+ ItemTranslation.create :item => @item, :language => @l1, :name => "Book", :desc => "Literature"
56
+ ItemTranslation.create :item => @item, :language => @l2, :name => "Haken", :desc => "Werkzeug"
57
+ ItemTranslation.create :item => @item2, :language => @l1, :name => "Hook", :desc => "Tool"
58
+ end
59
+
60
+ it "should return 2 language" do
61
+ @provider.available_languages.size.should == 2
62
+ end
63
+
64
+ it "should return the right language" do
65
+ @provider.available_languages.size.should == 2
66
+ @provider.available_languages.should include(@l1)
67
+ @provider.available_languages.should include(@l2)
68
+ end
69
+
70
+ end
71
+
72
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1,2 @@
1
+ --format specdoc
2
+ --colour
@@ -0,0 +1,67 @@
1
+ require 'rubygems'
2
+
3
+ require 'dm-core'
4
+ require 'dm-is-remixable'
5
+ require 'dm-validations'
6
+ require 'dm-accepts_nested_attributes'
7
+
8
+ # Support running specs with 'rake spec' and 'spec'
9
+ $LOAD_PATH.unshift('lib') unless $LOAD_PATH.include?('lib')
10
+
11
+ require 'dm-is-localizable'
12
+
13
+ ENV["SQLITE3_SPEC_URI"] ||= 'sqlite3::memory:'
14
+ ENV["MYSQL_SPEC_URI"] ||= 'mysql://localhost/dm-is_localizable_test'
15
+ ENV["POSTGRES_SPEC_URI"] ||= 'postgres://postgres@localhost/dm-is_localizable_test'
16
+
17
+
18
+ def setup_adapter(name, default_uri = nil)
19
+ begin
20
+ DataMapper.setup(name, ENV["#{ENV['ADAPTER'].to_s.upcase}_SPEC_URI"] || default_uri)
21
+ Object.const_set('ADAPTER', ENV['ADAPTER'].to_sym) if name.to_s == ENV['ADAPTER']
22
+ true
23
+ rescue Exception => e
24
+ if name.to_s == ENV['ADAPTER']
25
+ Object.const_set('ADAPTER', nil)
26
+ warn "Could not load do_#{name}: #{e}"
27
+ end
28
+ false
29
+ end
30
+ end
31
+
32
+ # have the loggers handy
33
+ # DataObjects::Logger.new(STDOUT, :debug)
34
+ # DataObjects::Sqlite3.logger = DataObjects::Logger.new(STDOUT, :debug)
35
+
36
+ # -----------------------------------------------
37
+ # support for nice html output in rspec tmbundle
38
+ # -----------------------------------------------
39
+
40
+ USE_TEXTMATE_RSPEC_BUNDLE = true # set to false if not using textmate
41
+
42
+ if USE_TEXTMATE_RSPEC_BUNDLE
43
+
44
+ require Pathname(__FILE__).dirname.expand_path + 'lib/rspec_tmbundle_support'
45
+
46
+ # use the tmbundle logger
47
+ #RSpecTmBundleHelpers::TextmateRspecLogger.new(STDOUT, :off)
48
+
49
+
50
+ class Object
51
+ include RSpecTmBundleHelpers
52
+ end
53
+
54
+ end
55
+
56
+ ENV['ADAPTER'] ||= 'sqlite3'
57
+ setup_adapter(:default)
58
+ Dir[Pathname(__FILE__).dirname.to_s + "/fixtures/**/*.rb"].each { |rb| require(rb) }
59
+
60
+
61
+ Spec::Runner.configure do |config|
62
+
63
+ config.before(:each) do
64
+ DataMapper.auto_migrate!
65
+ end
66
+
67
+ end
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+
3
+ describe "DataMapper.auto_migrate!" do
4
+
5
+ it "should allow to automigrate the resource to be translated" do
6
+ lambda { Item.auto_migrate! }.should_not raise_error
7
+ end
8
+
9
+ it "should allow to automigrate the resource where translations will be stored" do
10
+ lambda { ItemTranslation.auto_migrate! }.should_not raise_error
11
+ end
12
+
13
+ end
@@ -0,0 +1,169 @@
1
+ require 'spec_helper'
2
+ require 'shared/shared_examples_spec'
3
+
4
+ describe "class level API:" do
5
+
6
+ describe "translation_model" do
7
+
8
+ it "should follow naming conventions" do
9
+ Item.translation_model.should == ItemTranslation
10
+ end
11
+
12
+ end
13
+
14
+ describe "available_languages" do
15
+
16
+ before :each do
17
+ @item = Item.create
18
+ @provider = Item
19
+ end
20
+
21
+ it_should_behave_like "all available_languages providers"
22
+
23
+ end
24
+
25
+ describe "nr_of_available_languages" do
26
+
27
+ describe "with 0 items" do
28
+
29
+ it "should return 0" do
30
+ Item.nr_of_available_languages.should == 0
31
+ end
32
+
33
+ end
34
+
35
+ describe "with 1 item" do
36
+
37
+ before :each do
38
+ @l1 = Language.create :code => 'en-US', :name => 'English'
39
+ @l2 = Language.create :code => 'de-AT', :name => 'Deutsch'
40
+ @i1 = Item.create
41
+ end
42
+
43
+ describe "and 0 translations" do
44
+
45
+ it "should return 0" do
46
+ Item.nr_of_available_languages == 0
47
+ end
48
+
49
+ end
50
+
51
+ describe "and 1 translation" do
52
+
53
+ it "should return 1" do
54
+ ItemTranslation.create :item => @i1, :language => @l1, :name => 'Book', :desc => 'Literature'
55
+ Item.nr_of_available_languages == 1
56
+ end
57
+
58
+ end
59
+
60
+ describe "and 2 translations" do
61
+
62
+ it "should return 2" do
63
+ ItemTranslation.create :item => @i1, :language => @l1, :name => 'Book', :desc => 'Literature'
64
+ ItemTranslation.create :item => @i1, :language => @l2, :name => 'Book', :desc => 'Literature'
65
+ Item.nr_of_available_languages == 2
66
+ end
67
+
68
+ end
69
+
70
+ end
71
+
72
+ end
73
+
74
+
75
+ describe "translations_complete?" do
76
+
77
+ describe "with 0 items" do
78
+
79
+ it "should return true" do
80
+ Item.translations_complete?.should be_true
81
+ end
82
+
83
+ end
84
+
85
+ describe "with 1 item" do
86
+
87
+ before :each do
88
+ @i1 = Item.create
89
+ @provider = Item
90
+ end
91
+
92
+ describe "and 0 translations" do
93
+
94
+ it "should return true" do
95
+ Item.translations_complete?.should be_true
96
+ end
97
+
98
+ end
99
+
100
+ describe "and 1 translation" do
101
+
102
+ it "should return true" do
103
+ l = Language.create :code => 'en-US', :name => 'English'
104
+ ItemTranslation.create :item => @i1, :language => l, :name => 'Book', :desc => 'Literature'
105
+ Item.translations_complete?.should be_true
106
+ end
107
+
108
+ end
109
+
110
+ describe "and more than 1 translation" do
111
+
112
+ it "should return true" do
113
+ l1 = Language.create :code => 'en-US', :name => 'English'
114
+ l2 = Language.create :code => 'de-AT', :name => 'Deutsch'
115
+ ItemTranslation.create :item => @i1, :language => l1, :name => 'Book', :desc => 'Literature'
116
+ ItemTranslation.create :item => @i1, :language => l2, :name => 'Buch', :desc => 'Literatur'
117
+ Item.translations_complete?.should be_true
118
+ end
119
+
120
+ end
121
+
122
+ end
123
+
124
+ describe "with 2 items" do
125
+
126
+ before :each do
127
+ @l1 = Language.create :code => 'en-US', :name => 'English'
128
+ @l2 = Language.create :code => 'de-AT', :name => 'Deutsch'
129
+ @i1 = Item.create
130
+ @i2 = Item.create
131
+ end
132
+
133
+ describe "and not all items are translated" do
134
+
135
+ it "should return false" do
136
+ ItemTranslation.create :item => @i1, :language => @l1, :name => 'Book', :desc => 'Literature'
137
+ ItemTranslation.create :item => @i1, :language => @l2, :name => 'Buch', :desc => 'Literatur'
138
+ Item.translations_complete?.should be_false
139
+ end
140
+
141
+ end
142
+
143
+ describe "and all items are translated" do
144
+
145
+ it "should return true" do
146
+ ItemTranslation.create :item => @i1, :language => @l1, :name => 'Book', :desc => 'Literature'
147
+ ItemTranslation.create :item => @i1, :language => @l2, :name => 'Buch', :desc => 'Literatur'
148
+ ItemTranslation.create :item => @i2, :language => @l1, :name => 'Hook', :desc => 'Tool'
149
+ ItemTranslation.create :item => @i2, :language => @l2, :name => 'Haken', :desc => 'Werkzeug'
150
+ Item.translations_complete?.should be_true
151
+ end
152
+
153
+ end
154
+
155
+ end
156
+
157
+ end
158
+
159
+ describe "localizable_properties" do
160
+
161
+ it "should return a list of symbols reflecting the localizable properties" do
162
+ Item.localizable_properties.size.should == 2
163
+ Item.localizable_properties.should include(:name)
164
+ Item.localizable_properties.should include(:desc)
165
+ end
166
+
167
+ end
168
+
169
+ end
@@ -0,0 +1,328 @@
1
+ require 'spec_helper'
2
+ require 'shared/shared_examples_spec'
3
+
4
+
5
+ describe "instance level API:" do
6
+
7
+ it "should create a one_to_many association that follows naming conventions" do
8
+ Item.new.should respond_to :item_translations
9
+ end
10
+
11
+ it "should add a translations alias to the one_to_many association" do
12
+ Item.new.should respond_to :translations
13
+ end
14
+
15
+ it "should create a many_to_many association to languages" do
16
+ Item.new.should respond_to :languages
17
+ end
18
+
19
+ describe "available_languages" do
20
+
21
+ before :each do
22
+ @item = Item.create
23
+ @provider = @item
24
+ end
25
+
26
+ it_should_behave_like "all available_languages providers"
27
+
28
+ end
29
+
30
+
31
+ describe "nr_of_available_languages" do
32
+
33
+ before :each do
34
+ @l1 = Language.create :code => 'en-US', :name => 'English'
35
+ @l2 = Language.create :code => 'de-AT', :name => 'Deutsch'
36
+ @i1 = Item.create
37
+ end
38
+
39
+ describe "with 0 translations" do
40
+
41
+ it "should return 0" do
42
+ @i1.nr_of_available_languages.should == 0
43
+ end
44
+
45
+ end
46
+
47
+ describe "with 1 translation" do
48
+
49
+ it "should return 1" do
50
+ ItemTranslation.create :item => @i1, :language => @l1, :name => 'Book', :desc => 'Literature'
51
+ @i1.nr_of_available_languages == 1
52
+ end
53
+
54
+ end
55
+
56
+ describe "with 2 translations in 1 language" do
57
+
58
+ it "should return 1" do
59
+ ItemTranslation.create :item => @i1, :language => @l1, :name => 'Book', :desc => 'Literature'
60
+ ItemTranslation.create :item => @i1, :language => @l1, :name => 'Book', :desc => 'Literature'
61
+ @i1.nr_of_available_languages == 1
62
+ end
63
+
64
+ end
65
+
66
+ describe "with 2 translations in 2 language" do
67
+
68
+ it "should return 2" do
69
+ ItemTranslation.create :item => @i1, :language => @l1, :name => 'Book', :desc => 'Literature'
70
+ ItemTranslation.create :item => @i1, :language => @l2, :name => 'Book', :desc => 'Literature'
71
+ @i1.nr_of_available_languages == 2
72
+ end
73
+
74
+ end
75
+
76
+ end
77
+
78
+
79
+ describe "translations_complete?" do
80
+
81
+ describe "with 1 item" do
82
+
83
+ before :each do
84
+ @i1 = Item.create
85
+ end
86
+
87
+ describe "and 0 translations" do
88
+
89
+ it "should return true" do
90
+ @i1.translations_complete?.should be_true
91
+ end
92
+
93
+ end
94
+
95
+ describe "and 1 translation" do
96
+
97
+ it "should return true" do
98
+ l = Language.create :code => 'en-US', :name => 'English'
99
+ ItemTranslation.create :item => @i1, :language => l, :name => 'Book', :desc => 'Literature'
100
+ @i1.translations_complete?.should be_true
101
+ end
102
+
103
+ end
104
+
105
+ describe "and more than 1 translation" do
106
+
107
+ it "should return true" do
108
+ l1 = Language.create :code => 'en-US', :name => 'English'
109
+ l2 = Language.create :code => 'de-AT', :name => 'Deutsch'
110
+ ItemTranslation.create :item => @i1, :language => l1, :name => 'Book', :desc => 'Literature'
111
+ ItemTranslation.create :item => @i1, :language => l2, :name => 'Buch', :desc => 'Literatur'
112
+ @i1.translations_complete?.should be_true
113
+ end
114
+
115
+ end
116
+
117
+ end
118
+
119
+ describe "with 2 items" do
120
+
121
+ before :each do
122
+ @l1 = Language.create :code => 'en-US', :name => 'English'
123
+ @l2 = Language.create :code => 'de-AT', :name => 'Deutsch'
124
+ @i1 = Item.create
125
+ @i2 = Item.create
126
+ end
127
+
128
+ describe "both having 1 translation into different languages" do
129
+
130
+ it "should return false" do
131
+ ItemTranslation.create :item => @i1, :language => @l1, :name => 'Book', :desc => 'Literature'
132
+ ItemTranslation.create :item => @i2, :language => @l2, :name => 'Buch', :desc => 'Literatur'
133
+ @i1.translations_complete?.should be_false
134
+ @i2.translations_complete?.should be_false
135
+ end
136
+
137
+ end
138
+
139
+ describe "both having 1 translation into all different languages" do
140
+
141
+ it "should return true" do
142
+ ItemTranslation.create :item => @i1, :language => @l1, :name => 'Book', :desc => 'Literature'
143
+ ItemTranslation.create :item => @i1, :language => @l2, :name => 'Buch', :desc => 'Literatur'
144
+ ItemTranslation.create :item => @i2, :language => @l1, :name => 'Hook', :desc => 'Tool'
145
+ ItemTranslation.create :item => @i2, :language => @l2, :name => 'Haken', :desc => 'Werkzeug'
146
+ @i1.translations_complete?.should be_true
147
+ @i2.translations_complete?.should be_true
148
+ end
149
+
150
+ end
151
+
152
+ end
153
+
154
+ end
155
+
156
+ describe "translate(attribute, language_code)" do
157
+
158
+ before :each do
159
+ @l1 = Language.create :code => 'en-US', :name => 'English'
160
+ @l2 = Language.create :code => 'de-AT', :name => 'Deutsch'
161
+ @i1 = Item.create
162
+ @t1 = ItemTranslation.create :item => @i1, :language => @l1, :name => 'Book', :desc => 'Literature'
163
+ @t2 = ItemTranslation.create :item => @i1, :language => @l2, :name => 'Buch', :desc => 'Literatur'
164
+ end
165
+
166
+ describe "with an existing attribute" do
167
+
168
+ describe "and an existing language_code" do
169
+
170
+ describe "passed as Symbol" do
171
+
172
+ it "should return the translated string" do
173
+ @i1.translate(:name, :en_US).should == 'Book'
174
+ @i1.translate(:desc, :en_US).should == 'Literature'
175
+ @i1.translate(:name, :de_AT).should == 'Buch'
176
+ @i1.translate(:desc, :de_AT).should == 'Literatur'
177
+ end
178
+
179
+ end
180
+
181
+ describe "passed as String" do
182
+
183
+ it "should return the translated string" do
184
+ @i1.translate(:name, 'en_US').should == 'Book'
185
+ @i1.translate(:desc, 'en_US').should == 'Literature'
186
+ @i1.translate(:name, 'de_AT').should == 'Buch'
187
+ @i1.translate(:desc, 'de_AT').should == 'Literatur'
188
+ end
189
+
190
+ end
191
+
192
+ end
193
+
194
+ describe "and a non existent language_code" do
195
+
196
+ describe "passed as Symbol" do
197
+
198
+ it "should return the translated string" do
199
+ @i1.translate(:name, :it).should be_nil
200
+ end
201
+
202
+ end
203
+
204
+ describe "passed as String" do
205
+
206
+ it "should return the translated string" do
207
+ @i1.translate(:name, 'it').should be_nil
208
+ end
209
+
210
+ end
211
+
212
+ end
213
+
214
+ end
215
+
216
+ describe "with a non existent attribute" do
217
+
218
+ describe "and an existing language_code" do
219
+
220
+ describe "passed as Symbol" do
221
+
222
+ it "should return the translated string" do
223
+ @i1.translate(:foo, :en_US).should be_nil
224
+ end
225
+
226
+ end
227
+
228
+ describe "passed as String" do
229
+
230
+ it "should return the translated string" do
231
+ @i1.translate(:foo, 'en_US').should be_nil
232
+ end
233
+
234
+ end
235
+
236
+ end
237
+
238
+ describe "and a non existent language_code" do
239
+
240
+ describe "passed as Symbol" do
241
+
242
+ it "should return the translated string" do
243
+ @i1.translate(:foo, :it).should be_nil
244
+ end
245
+
246
+ end
247
+
248
+ describe "passed as String" do
249
+
250
+ it "should return the translated string" do
251
+ @i1.translate(:foo, 'it').should be_nil
252
+ end
253
+
254
+ end
255
+
256
+ end
257
+
258
+ end
259
+
260
+ end
261
+
262
+ describe "property_name(language_code)" do
263
+
264
+ before :each do
265
+ @l1 = Language.create :code => 'en-US', :name => 'English'
266
+ @l2 = Language.create :code => 'de-AT', :name => 'Deutsch'
267
+ @i1 = Item.create
268
+ @t1 = ItemTranslation.create :item => @i1, :language => @l1, :name => 'Book', :desc => 'Literature'
269
+ @t2 = ItemTranslation.create :item => @i1, :language => @l2, :name => 'Buch', :desc => 'Literatur'
270
+ end
271
+
272
+ describe "with a nil language_code" do
273
+
274
+ it "should return nil" do
275
+ @i1.name(nil).should be_nil
276
+ end
277
+
278
+ end
279
+
280
+ describe "with a non existent language_code" do
281
+
282
+ describe "passed as Symbol" do
283
+
284
+ it "should return nil" do
285
+ @i1.name(:it).should be_nil
286
+ end
287
+
288
+ end
289
+
290
+ describe "passed as String" do
291
+
292
+ it "should return nil" do
293
+ @i1.name('it').should be_nil
294
+ end
295
+
296
+ end
297
+
298
+ end
299
+
300
+ describe "with an existing language_code" do
301
+
302
+ describe "passed as Symbol" do
303
+
304
+ it "should return the translated property" do
305
+ @i1.name(:en_US).should == 'Book'
306
+ @i1.desc(:en_US).should == 'Literature'
307
+ @i1.name(:de_AT).should == 'Buch'
308
+ @i1.desc(:de_AT).should == 'Literatur'
309
+ end
310
+
311
+ end
312
+
313
+ describe "passed as String" do
314
+
315
+ it "should return the translated property" do
316
+ @i1.name('en_US').should == 'Book'
317
+ @i1.desc('en_US').should == 'Literature'
318
+ @i1.name('de_AT').should == 'Buch'
319
+ @i1.desc('de_AT').should == 'Literatur'
320
+ end
321
+
322
+ end
323
+
324
+ end
325
+
326
+ end
327
+
328
+ end
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+
3
+ describe "is_localizable with no options" do
4
+
5
+ before :each do
6
+ @i = Item.create
7
+ @l = Language.create :code => 'en', :name => 'English'
8
+ @t = ItemTranslation.new :item => @i, :language => @l
9
+ end
10
+
11
+ it "should belong_to a resource" do
12
+ @t.respond_to?(:item).should == true
13
+ @t.item.should be_instance_of(Item)
14
+ end
15
+
16
+ it "should belong_to a language" do
17
+ @t.respond_to?(:language).should == true
18
+ @t.language.should be_instance_of(Language)
19
+ @t.language.code.should == 'en'
20
+ @t.language.name.should == 'English'
21
+ end
22
+
23
+ it "should store properties defined inside the block in the translations resource" do
24
+ @t.respond_to?(:name).should == true
25
+ @t.respond_to?(:desc).should == true
26
+ end
27
+
28
+ end