symbolize 4.5.0 → 4.5.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/Rakefile +3 -1
- data/lib/symbolize.rb +0 -1
- data/lib/symbolize/active_record.rb +178 -170
- data/lib/symbolize/mongoid.rb +80 -68
- data/lib/symbolize/version.rb +1 -1
- data/spec/db/001_create_testing_structure.rb +3 -3
- data/spec/spec_helper.rb +11 -14
- data/spec/symbolize/active_record_spec.rb +120 -121
- data/spec/symbolize/mongoid_spec.rb +125 -129
- metadata +17 -3
@@ -13,22 +13,22 @@ class Person
|
|
13
13
|
|
14
14
|
symbolize :language, :in => [:pt, :en]
|
15
15
|
symbolize :sex, :type => Boolean, :scopes => true, :i18n => true
|
16
|
-
symbolize :status
|
16
|
+
symbolize :status, :in => [:active, :inactive], :i18n => false, :capitalize => true, :scopes => :shallow
|
17
17
|
symbolize :so, :allow_blank => true, :in => {
|
18
18
|
:linux => 'Linux',
|
19
|
-
:mac
|
20
|
-
:win
|
21
|
-
},
|
19
|
+
:mac => 'Mac OS X',
|
20
|
+
:win => 'Videogame',
|
21
|
+
}, :scopes => true
|
22
22
|
symbolize :gui, :allow_blank => true, :in => [:cocoa, :qt, :gtk], :i18n => false
|
23
|
-
symbolize :karma, :in => %w
|
24
|
-
symbolize :planet, :in => %w
|
23
|
+
symbolize :karma, :in => %w(good bad ugly), :methods => true, :i18n => false, :allow_nil => true
|
24
|
+
symbolize :planet, :in => %w(earth centauri tatooine), :default => :earth
|
25
25
|
# symbolize :cool, :in => [true, false], :scopes => true
|
26
26
|
|
27
27
|
symbolize :year, :in => Time.now.year.downto(1980).to_a, :validate => false
|
28
28
|
|
29
29
|
has_many :rights, :dependent => :destroy
|
30
|
-
has_many :extras, :dependent => :destroy, :class_name =>
|
31
|
-
embeds_many :skills, :class_name =>
|
30
|
+
has_many :extras, :dependent => :destroy, :class_name => 'PersonExtra'
|
31
|
+
embeds_many :skills, :class_name => 'PersonSkill'
|
32
32
|
end
|
33
33
|
|
34
34
|
class PersonSkill
|
@@ -56,7 +56,6 @@ class Right
|
|
56
56
|
symbolize :kind, :in => [:temp, :perm], :default => :perm
|
57
57
|
end
|
58
58
|
|
59
|
-
|
60
59
|
class Project
|
61
60
|
include Mongoid::Document
|
62
61
|
|
@@ -67,32 +66,30 @@ class Project
|
|
67
66
|
default_scope -> { where(:state => 'active') }
|
68
67
|
scope :inactive, -> { any_in(:state => [:done, :wip]) }
|
69
68
|
scope :dead, -> { all_of(:state => :wip, :name => 'zim') }
|
70
|
-
|
71
69
|
end
|
72
70
|
|
71
|
+
describe 'Symbolize' do
|
73
72
|
|
74
|
-
|
75
|
-
|
76
|
-
it "should be a module" do
|
77
|
-
expect(Mongoid.const_defined?("Symbolize")).to be true
|
73
|
+
it 'should be a module' do
|
74
|
+
expect(Mongoid.const_defined?('Symbolize')).to be true
|
78
75
|
end
|
79
76
|
|
80
|
-
it
|
77
|
+
it 'should instantiate' do
|
81
78
|
anna = Person.create(:name => 'Anna', :so => :mac, :gui => :cocoa, :language => :pt, :status => :active, :sex => true)
|
82
|
-
#anna.should be_valid
|
79
|
+
# anna.should be_valid
|
83
80
|
expect(anna.errors.messages).to eql({})
|
84
81
|
end
|
85
82
|
|
86
|
-
describe
|
87
|
-
let(:person) { Person.create(:name => 'Anna', :other => :fo, :status => :active
|
83
|
+
describe 'Person Instantiated' do
|
84
|
+
let(:person) { Person.create(:name => 'Anna', :other => :fo, :status => :active, :so => :linux, :gui => :qt, :language => :pt, :sex => true, :cool => true) }
|
88
85
|
|
89
|
-
it
|
86
|
+
it 'test_symbolize_string' do
|
90
87
|
person.status = 'inactive'
|
91
88
|
expect(person.status).to eql(:inactive)
|
92
89
|
expect(person.read_attribute(:status)).to eql(:inactive)
|
93
90
|
end
|
94
91
|
|
95
|
-
it
|
92
|
+
it 'test_symbolize_symbol' do
|
96
93
|
person.status = :active
|
97
94
|
expect(person.status).to eql(:active)
|
98
95
|
person.save
|
@@ -100,8 +97,8 @@ describe "Symbolize" do
|
|
100
97
|
expect(person[:status]).to eql(:active)
|
101
98
|
end
|
102
99
|
|
103
|
-
it
|
104
|
-
other = Person.new(language
|
100
|
+
it 'should make strings symbols from initializer' do
|
101
|
+
other = Person.new(:language => 'en', :so => :mac, :gui => :cocoa, :language => :pt, :sex => true, :status => 'active')
|
105
102
|
expect(other[:status]).to eq(:active)
|
106
103
|
expect(other.status).to eq(:active)
|
107
104
|
end
|
@@ -114,136 +111,136 @@ describe "Symbolize" do
|
|
114
111
|
# # person.read_attribute(:status).should be_nil
|
115
112
|
# end
|
116
113
|
|
117
|
-
it
|
114
|
+
it 'should acts nice with nil when reading' do
|
118
115
|
person.karma = nil
|
119
116
|
expect(person.karma).to be_nil
|
120
117
|
person.save
|
121
118
|
expect(person.read_attribute(:karma)).to be_nil
|
122
119
|
end
|
123
120
|
|
124
|
-
it
|
121
|
+
it 'should acts nice with nil #symbol_text' do
|
125
122
|
person.karma = nil
|
126
123
|
expect(person.karma).to be_nil
|
127
124
|
person.save
|
128
125
|
expect(person.karma_text).to be_nil
|
129
126
|
end
|
130
127
|
|
131
|
-
it
|
132
|
-
person.so =
|
128
|
+
it 'should acts nice with blank when reading' do
|
129
|
+
person.so = ''
|
133
130
|
expect(person.so).to be_blank
|
134
131
|
person.save
|
135
132
|
expect(person.read_attribute(:so)).to be_blank
|
136
133
|
end
|
137
134
|
|
138
|
-
it
|
139
|
-
person.so =
|
135
|
+
it 'should acts nice with blank #symbol_text' do
|
136
|
+
person.so = ''
|
140
137
|
expect(person.so).to be_blank
|
141
138
|
person.save
|
142
139
|
expect(person.so_text).to be_nil
|
143
140
|
end
|
144
141
|
|
145
|
-
it
|
142
|
+
it 'should not validates other' do
|
146
143
|
person.other = nil
|
147
144
|
expect(person).to be_valid
|
148
|
-
person.other =
|
145
|
+
person.other = ''
|
149
146
|
expect(person).to be_valid
|
150
147
|
end
|
151
148
|
|
152
|
-
it
|
153
|
-
expect(Person.get_status_values).to eql([[
|
154
|
-
expect(Person::STATUS_VALUES).to eql(
|
149
|
+
it 'should get the correct values' do
|
150
|
+
expect(Person.get_status_values).to eql([['Active', :active], ['Inactive', :inactive]])
|
151
|
+
expect(Person::STATUS_VALUES).to eql(:inactive => 'Inactive', :active => 'Active')
|
155
152
|
end
|
156
153
|
|
157
|
-
it
|
158
|
-
expect(Person.status_enum).to eql([[
|
154
|
+
it 'should get the values for RailsAdmin' do
|
155
|
+
expect(Person.status_enum).to eql([['Active', :active], ['Inactive', :inactive]])
|
159
156
|
end
|
160
157
|
|
161
|
-
it
|
162
|
-
expect(person.status_text).to eql(
|
158
|
+
it 'should have a human _text method' do
|
159
|
+
expect(person.status_text).to eql('Active')
|
163
160
|
end
|
164
161
|
|
165
|
-
it
|
166
|
-
expect(person.language_text).to eql(
|
162
|
+
it 'should work nice with i18n' do
|
163
|
+
expect(person.language_text).to eql('Português')
|
167
164
|
end
|
168
165
|
|
169
|
-
it
|
170
|
-
expect(person.status_text).to eql(
|
166
|
+
it 'test_symbolize_humanize' do
|
167
|
+
expect(person.status_text).to eql('Active')
|
171
168
|
end
|
172
169
|
|
173
|
-
it
|
174
|
-
expect(Person.get_gui_values).to match_array([[
|
175
|
-
expect(Person::GUI_VALUES).to eql(
|
170
|
+
it 'should get the correct values' do
|
171
|
+
expect(Person.get_gui_values).to match_array([['cocoa', :cocoa], ['qt', :qt], ['gtk', :gtk]])
|
172
|
+
expect(Person::GUI_VALUES).to eql(:cocoa => 'cocoa', :qt => 'qt', :gtk => 'gtk')
|
176
173
|
end
|
177
174
|
|
178
|
-
it
|
179
|
-
expect(person.gui_text).to eql(
|
175
|
+
it 'test_symbolize_humanize' do
|
176
|
+
expect(person.gui_text).to eql('qt')
|
180
177
|
end
|
181
178
|
|
182
|
-
it
|
183
|
-
expect(Person.get_so_values).to match_array([[
|
184
|
-
expect(Person::SO_VALUES).to eql(
|
179
|
+
it 'should get the correct values' do
|
180
|
+
expect(Person.get_so_values).to match_array([['Linux', :linux], ['Mac OS X', :mac], ['Videogame', :win]])
|
181
|
+
expect(Person::SO_VALUES).to eql(:linux => 'Linux', :mac => 'Mac OS X', :win => 'Videogame')
|
185
182
|
end
|
186
183
|
|
187
|
-
it
|
188
|
-
expect(person.so_text).to eql(
|
184
|
+
it 'test_symbolize_humanize' do
|
185
|
+
expect(person.so_text).to eql('Linux')
|
189
186
|
end
|
190
187
|
|
191
|
-
it
|
188
|
+
it 'test_symbolize_humanize' do
|
192
189
|
person.so = :mac
|
193
|
-
expect(person.so_text).to eql(
|
190
|
+
expect(person.so_text).to eql('Mac OS X')
|
194
191
|
end
|
195
192
|
|
196
|
-
it
|
197
|
-
expect(person.other_text).to eql(
|
193
|
+
it 'should stringify' do
|
194
|
+
expect(person.other_text).to eql('fo')
|
198
195
|
person.other = :foo
|
199
|
-
expect(person.other_text).to eql(
|
196
|
+
expect(person.other_text).to eql('foo')
|
200
197
|
end
|
201
198
|
|
202
|
-
it
|
199
|
+
it 'should work with weird chars' do
|
203
200
|
person.status = :"weird'; chars"
|
204
201
|
expect(person.status).to eql(:"weird'; chars")
|
205
202
|
end
|
206
203
|
|
207
|
-
it
|
204
|
+
it 'should work fine through relations' do
|
208
205
|
person.extras.create(:key => :one)
|
209
206
|
expect(PersonExtra.first.key).to eql(:one)
|
210
207
|
end
|
211
208
|
|
212
|
-
it
|
209
|
+
it 'should work fine through embeds' do
|
213
210
|
person.skills.create(:kind => :magic)
|
214
211
|
expect(person.skills.first.kind).to eql(:magic)
|
215
212
|
end
|
216
213
|
|
217
|
-
it
|
214
|
+
it 'should default planet to earth' do
|
218
215
|
expect(Person.new.planet).to eql(:earth)
|
219
216
|
end
|
220
217
|
|
221
|
-
describe
|
218
|
+
describe 'validation' do
|
222
219
|
|
223
|
-
it
|
224
|
-
other = Person.new(language
|
220
|
+
it 'should validate from initializer' do
|
221
|
+
other = Person.new(:language => 'en', :so => :mac, :gui => :cocoa, :language => :pt, :sex => true, :status => 'active')
|
225
222
|
expect(other).to be_valid
|
226
223
|
expect(other.errors.messages).to eq({})
|
227
224
|
end
|
228
225
|
|
229
|
-
it
|
226
|
+
it 'should validate nil' do
|
230
227
|
person.status = nil
|
231
228
|
expect(person).not_to be_valid
|
232
229
|
expect(person.errors.messages).to have_key(:status)
|
233
230
|
end
|
234
231
|
|
235
|
-
it
|
232
|
+
it 'should validate not included' do
|
236
233
|
person.language = 'xx'
|
237
234
|
expect(person).not_to be_valid
|
238
235
|
expect(person.errors.messages).to have_key(:language)
|
239
236
|
end
|
240
237
|
|
241
|
-
it
|
238
|
+
it 'should not validate so' do
|
242
239
|
person.so = nil
|
243
240
|
expect(person).to be_valid
|
244
241
|
end
|
245
242
|
|
246
|
-
it
|
243
|
+
it 'should validate ok' do
|
247
244
|
person.language = 'pt'
|
248
245
|
expect(person).to be_valid
|
249
246
|
expect(person.errors.messages).to eq({})
|
@@ -251,35 +248,35 @@ describe "Symbolize" do
|
|
251
248
|
|
252
249
|
end
|
253
250
|
|
254
|
-
describe
|
251
|
+
describe 'i18n' do
|
255
252
|
|
256
|
-
it
|
257
|
-
expect(person.language_text).to eql(
|
253
|
+
it 'should test i18n ones' do
|
254
|
+
expect(person.language_text).to eql('Português')
|
258
255
|
end
|
259
256
|
|
260
|
-
it
|
261
|
-
expect(Person.get_language_values).to match_array([[
|
257
|
+
it 'should get the correct values' do
|
258
|
+
expect(Person.get_language_values).to match_array([['Português', :pt], ['Inglês', :en]])
|
262
259
|
end
|
263
260
|
|
264
|
-
it
|
265
|
-
expect(Person::LANGUAGE_VALUES).to eql(
|
261
|
+
it 'should get the correct values' do
|
262
|
+
expect(Person::LANGUAGE_VALUES).to eql(:pt => 'pt', :en => 'en')
|
266
263
|
end
|
267
264
|
|
268
|
-
it
|
269
|
-
expect(person.sex_text).to eql(
|
265
|
+
it 'should test boolean' do
|
266
|
+
expect(person.sex_text).to eql('Feminino')
|
270
267
|
end
|
271
268
|
|
272
|
-
it
|
273
|
-
expect(Person.get_sex_values).to eql([[
|
269
|
+
it 'should get the correct values' do
|
270
|
+
expect(Person.get_sex_values).to eql([['Feminino', true], ['Masculino', false]])
|
274
271
|
end
|
275
272
|
|
276
|
-
it
|
277
|
-
expect(Person::SEX_VALUES).to eql(
|
273
|
+
it 'should get the correct values' do
|
274
|
+
expect(Person::SEX_VALUES).to eql(true => 'true', false => 'false')
|
278
275
|
end
|
279
276
|
|
280
|
-
it
|
277
|
+
it 'should translate a multiword classname' do
|
281
278
|
skill = PersonSkill.new(:kind => :magic)
|
282
|
-
expect(skill.kind_text).to eql(
|
279
|
+
expect(skill.kind_text).to eql('Mágica')
|
283
280
|
end
|
284
281
|
|
285
282
|
it "should return nil if there's no value" do
|
@@ -289,30 +286,30 @@ describe "Symbolize" do
|
|
289
286
|
|
290
287
|
it "should return the proper 'false' i18n if the attr value is false" do
|
291
288
|
person = Person.new(:sex => false)
|
292
|
-
expect(person.sex_text).to eq(
|
289
|
+
expect(person.sex_text).to eq('Masculino')
|
293
290
|
end
|
294
291
|
|
295
|
-
it
|
296
|
-
expect(person.sex_text).to eql(
|
292
|
+
it 'should use i18n if i18n => true' do
|
293
|
+
expect(person.sex_text).to eql('Feminino')
|
297
294
|
end
|
298
295
|
|
299
296
|
end
|
300
297
|
|
301
|
-
describe
|
298
|
+
describe 'Methods' do
|
302
299
|
|
303
|
-
it
|
300
|
+
it 'should play nice with other stuff' do
|
304
301
|
expect(person.karma).to be_nil
|
305
|
-
expect(Person::KARMA_VALUES).to eql(
|
302
|
+
expect(Person::KARMA_VALUES).to eql(:bad => 'bad', :ugly => 'ugly', :good => 'good')
|
306
303
|
end
|
307
304
|
|
308
|
-
it
|
305
|
+
it 'should provide a boolean method' do
|
309
306
|
expect(person).not_to be_good
|
310
307
|
person.karma = :ugly
|
311
308
|
expect(person).to be_ugly
|
312
309
|
end
|
313
310
|
|
314
|
-
it
|
315
|
-
person.karma =
|
311
|
+
it 'should work' do
|
312
|
+
person.karma = 'good'
|
316
313
|
expect(person).to be_good
|
317
314
|
expect(person).not_to be_bad
|
318
315
|
end
|
@@ -321,96 +318,95 @@ describe "Symbolize" do
|
|
321
318
|
|
322
319
|
end
|
323
320
|
|
324
|
-
describe
|
321
|
+
describe 'more tests on Right' do
|
325
322
|
|
326
|
-
it
|
327
|
-
Right.create!(:name =>
|
328
|
-
expect(Right.where(name
|
323
|
+
it 'should not interfer on create' do
|
324
|
+
Right.create!(:name => 'p7', :kind => :temp)
|
325
|
+
expect(Right.where(:name => 'p7').first.kind).to eql(:temp)
|
329
326
|
end
|
330
327
|
|
331
|
-
it
|
332
|
-
pm = Right.new(:name =>
|
328
|
+
it 'should work on create' do
|
329
|
+
pm = Right.new(:name => 'p7')
|
333
330
|
expect(pm).to be_valid
|
334
331
|
expect(pm.save).to be true
|
335
332
|
end
|
336
333
|
|
337
|
-
it
|
338
|
-
Right.create(:name =>
|
339
|
-
expect(Right.find_by(name
|
334
|
+
it 'should work on create' do
|
335
|
+
Right.create(:name => 'p8')
|
336
|
+
expect(Right.find_by(:name => 'p8').kind).to eql(:perm)
|
340
337
|
end
|
341
338
|
|
342
|
-
it
|
343
|
-
Right.create(:name =>
|
344
|
-
pm = Right.where(name
|
339
|
+
it 'should work on edit' do
|
340
|
+
Right.create(:name => 'p8')
|
341
|
+
pm = Right.where(:name => 'p8').first
|
345
342
|
pm.kind = :temp
|
346
343
|
pm.save
|
347
|
-
expect(Right.where(name
|
344
|
+
expect(Right.where(:name => 'p8').first.kind).to eql(:temp)
|
348
345
|
end
|
349
346
|
|
350
347
|
end
|
351
348
|
|
352
|
-
describe
|
349
|
+
describe 'Default Values' do
|
353
350
|
|
354
|
-
it
|
351
|
+
it 'should use default value on object build' do
|
355
352
|
expect(Right.new.kind).to eql(:perm)
|
356
353
|
end
|
357
354
|
|
358
|
-
it
|
355
|
+
it 'should use default value in string' do
|
359
356
|
expect(Project.new.state).to eql('active')
|
360
357
|
end
|
361
358
|
|
362
359
|
end
|
363
360
|
|
364
|
-
|
365
|
-
|
366
|
-
it "should work under scope" do
|
361
|
+
describe 'Scopes' do
|
362
|
+
it 'should work under scope' do
|
367
363
|
# Person.with_scope({ :status => :inactive }) do
|
368
364
|
# Person.all.map(&:name).should eql(['Bob'])
|
369
365
|
# end
|
370
366
|
end
|
371
367
|
|
372
|
-
it
|
373
|
-
Project.create(:name =>
|
374
|
-
Project.create(:name =>
|
368
|
+
it 'should work under scope' do
|
369
|
+
Project.create(:name => 'A', :state => :done)
|
370
|
+
Project.create(:name => 'B', :state => :active)
|
375
371
|
expect(Project.count).to eql(1)
|
376
372
|
end
|
377
373
|
|
378
|
-
it
|
379
|
-
Person.create(:name => 'Bob'
|
374
|
+
it 'should now shallow scoped scopes' do
|
375
|
+
Person.create(:name => 'Bob', :other => :bar, :status => :active, :so => :linux, :gui => :gtk, :language => :en, :sex => false, :cool => false)
|
380
376
|
expect(Person).not_to respond_to(:status)
|
381
377
|
end
|
382
378
|
|
383
|
-
it
|
384
|
-
Person.create(:name => 'Bob'
|
379
|
+
it 'should set some scopes' do
|
380
|
+
Person.create(:name => 'Bob', :other => :bar, :status => :active, :so => :linux, :gui => :gtk, :language => :en, :sex => false, :cool => false)
|
385
381
|
expect(Person.so(:linux)).to be_a(Mongoid::Criteria)
|
386
382
|
expect(Person.so(:linux).count).to eq(1)
|
387
383
|
end
|
388
384
|
|
389
|
-
it
|
390
|
-
Person.create!(:name => 'Bob'
|
385
|
+
it 'should work with a shallow scope too' do
|
386
|
+
Person.create!(:name => 'Bob', :other => :bar, :status => :active, :so => :linux, :gui => :gtk, :language => :en, :sex => false, :cool => false)
|
391
387
|
expect(Person.active).to be_a(Mongoid::Criteria)
|
392
388
|
expect(Person.active.count).to eq(1)
|
393
389
|
end
|
394
390
|
|
395
391
|
end
|
396
392
|
|
397
|
-
describe
|
393
|
+
describe 'Mongoid stuff' do
|
398
394
|
|
399
|
-
it
|
400
|
-
Person.create(:name => 'Bob'
|
395
|
+
it 'test_symbolized_finder' do
|
396
|
+
Person.create(:name => 'Bob', :other => :bar, :status => :inactive, :so => :mac, :gui => :gtk, :language => :en, :sex => false, :cool => false)
|
401
397
|
|
402
|
-
expect(Person.where(
|
403
|
-
expect(Person.where(status
|
398
|
+
expect(Person.where(:status => :inactive).all.map(&:name)).to eql(['Bob'])
|
399
|
+
expect(Person.where(:status => :inactive).map(&:name)).to eql(['Bob'])
|
404
400
|
end
|
405
401
|
|
406
|
-
describe
|
402
|
+
describe 'dirty tracking / changed flag' do
|
407
403
|
|
408
404
|
before do
|
409
|
-
Person.create!(:name => 'Anna', :other => :fo, :status => :active
|
410
|
-
@anna = Person.where(name
|
405
|
+
Person.create!(:name => 'Anna', :other => :fo, :status => :active, :so => :linux, :gui => :qt, :language => :pt, :sex => true, :cool => true)
|
406
|
+
@anna = Person.where(:name => 'Anna').first
|
411
407
|
end
|
412
408
|
|
413
|
-
it
|
409
|
+
it 'is dirty if you change the attribute value' do
|
414
410
|
expect(@anna.language).to eq(:pt)
|
415
411
|
expect(@anna.language_changed?).to be false
|
416
412
|
|
@@ -419,7 +415,7 @@ describe "Symbolize" do
|
|
419
415
|
expect(@anna.language_changed?).to be true
|
420
416
|
end
|
421
417
|
|
422
|
-
it
|
418
|
+
it 'is not dirty if you set the attribute value to the same value it was originally' do
|
423
419
|
expect(@anna.language).to eq(:pt)
|
424
420
|
expect(@anna.language_changed?).to be false
|
425
421
|
|