symbolize 3.3.0pre → 4.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,438 +1,8 @@
1
1
  # -*- coding: utf-8 -*-
2
2
  require File.dirname(__FILE__) + '/spec_helper'
3
3
 
4
- #
5
- # Test model
6
- class User < ActiveRecord::Base
7
- symbolize :other
8
- symbolize :language, :in => [:pt, :en]
9
- symbolize :sex, :in => [true, false], :scopes => true
10
- symbolize :status , :in => [:active, :inactive], :i18n => false, :capitalize => true, :scopes => true
11
- symbolize :so, :allow_blank => true, :in => {
12
- :linux => 'Linux',
13
- :mac => 'Mac OS X',
14
- :win => 'Videogame'
15
- }, :scopes => true
16
- symbolize :gui, :allow_blank => true, :in => [:cocoa, :qt, :gtk], :i18n => false
17
- symbolize :karma, :in => %w{ good bad ugly}, :methods => true, :i18n => false, :allow_nil => true
18
- symbolize :cool, :in => [true, false], :scopes => true
19
-
20
- has_many :extras, :dependent => :destroy, :class_name => "UserExtra"
21
- has_many :access, :dependent => :destroy, :class_name => "UserAccess"
22
- end
23
-
24
- class UserSkill < ActiveRecord::Base
25
- symbolize :kind, :in => [:agility, :magic]
26
- end
27
-
28
- class UserExtra < ActiveRecord::Base
29
- symbolize :key, :in => [:one, :another]
30
- end
31
-
32
- class Permission < ActiveRecord::Base
33
- validates_presence_of :name
34
- symbolize :kind, :in => [:temp, :perm], :default => :perm
35
- symbolize :lvl, :in => (1..9).to_a, :i18n => false#, :default => 1
36
- end
37
-
38
- # Make with_scope public-usable for testing
39
- #if ActiveRecord::VERSION::MAJOR < 3
40
- class << ActiveRecord::Base
41
- public :with_scope
42
- end
43
- #end
44
-
45
- # Test records
46
- User.create(:name => 'Anna', :other => :fo, :status => :active , :so => :linux, :gui => :qt, :language => :pt, :sex => true, :cool => true)
47
- User.create!(:name => 'Bob' , :other => :bar,:status => :inactive, :so => :mac, :gui => :gtk, :language => :en, :sex => false, :cool => false)
48
-
49
4
 
50
5
  describe "Symbolize" do
51
6
 
52
- it "should respond to symbolize" do
53
- ActiveRecord::Base.should respond_to :symbolize
54
- end
55
-
56
- describe "User Instantiated" do
57
- before(:each) do
58
- @user = User.first
59
- end
60
-
61
- it "test_symbolize_string" do
62
- @user.status = 'inactive'
63
- @user.status.should eql(:inactive)
64
- # @user.status_before_type_cast.should eql(:inactive)
65
- # @user.read_attribute(:status).should eql('inactive')
66
- end
67
-
68
- it "test_symbolize_symbol" do
69
- @user.status = :active
70
- @user.status.should eql(:active)
71
- @user.status_before_type_cast.should eql(:active)
72
- # @user.read_attribute(:status).should eql('active')
73
- end
74
-
75
- it "should work nice with numbers" do
76
- @user.status = 43
77
- @user.status.should_not be_nil
78
- # @user.status_before_type_cast.should be_nil
79
- # @user.read_attribute(:status).should be_nil
80
- end
81
-
82
- it "should acts nice with nil" do
83
- @user.status = nil
84
- @user.status.should be_nil
85
- @user.status_before_type_cast.should be_nil
86
- @user.read_attribute(:status).should be_nil
87
- end
88
-
89
- it "should acts nice with blank" do
90
- @user.status = ""
91
- @user.status.should be_nil
92
- @user.status_before_type_cast.should be_nil
93
- @user.read_attribute(:status).should be_nil
94
- end
95
-
96
- it "test_symbols_quoted_id" do
97
- pending
98
- @user.status = :active
99
- @user.status.quoted_id.should eql("'active'")
100
- end
101
-
102
- it "should not validates other" do
103
- @user.other = nil
104
- @user.should be_valid
105
- @user.other = ""
106
- @user.should be_valid
107
- end
108
-
109
- it "should get the correct values" do
110
- User.get_status_values.should eql([["Active", :active],["Inactive", :inactive]])
111
- User::STATUS_VALUES.should eql({:inactive=>"Inactive", :active=>"Active"})
112
- end
113
-
114
- it "test_symbolize_humanize" do
115
- @user.status_text.should eql("Active")
116
- end
117
-
118
- it "should get the correct values" do
119
- User.get_gui_values.should =~ [["cocoa", :cocoa], ["qt", :qt], ["gtk", :gtk]]
120
- User::GUI_VALUES.should eql({:cocoa=>"cocoa", :qt=>"qt", :gtk=>"gtk"})
121
- end
122
-
123
- it "test_symbolize_humanize" do
124
- @user.gui_text.should eql("qt")
125
- end
126
-
127
- it "should get the correct values" do
128
- User.get_so_values.should =~ [["Linux", :linux], ["Mac OS X", :mac], ["Videogame", :win]]
129
- User::SO_VALUES.should eql({:linux => "Linux", :mac => "Mac OS X", :win => "Videogame"})
130
- end
131
-
132
- it "test_symbolize_humanize" do
133
- @user.so_text.should eql("Linux")
134
- end
135
-
136
- it "test_symbolize_humanize" do
137
- @user.so = :mac
138
- @user.so_text.should eql("Mac OS X")
139
- end
140
-
141
- it "should stringify" do
142
- @user.other_text.should eql("fo")
143
- @user.other = :foo
144
- @user.other_text.should eql("foo")
145
- end
146
-
147
- it "should validate status" do
148
- @user.status = nil
149
- @user.should_not be_valid
150
- @user.should have(1).errors
151
- end
152
-
153
- it "should not validate so" do
154
- @user.so = nil
155
- @user.should be_valid
156
- end
157
-
158
- it "test_symbols_with_weird_chars_quoted_id" do
159
- @user.status = :"weird'; chars"
160
- @user.status_before_type_cast.should eql(:"weird'; chars")
161
- # assert_equal "weird'; chars", @user.read_attribute(:status)
162
- # assert_equal "'weird''; chars'", @user.status.quoted_id
163
- end
164
-
165
- it "should work fine through relations" do
166
- @user.extras.create(:key => :one)
167
- UserExtra.first.key.should eql(:one)
168
- end
169
-
170
- it "should play fine with null db columns" do
171
- new_extra = @user.extras.build
172
- new_extra.should_not be_valid
173
- end
174
-
175
- it "should play fine with null db columns" do
176
- new_extra = @user.extras.build
177
- new_extra.should_not be_valid
178
- end
179
-
180
-
181
-
182
- describe "View helpers" do
183
- include ActionView::Helpers::FormHelper
184
- include ActionView::Helpers::FormOptionsHelper
185
-
186
- before(:each) do
187
- @options_status = [['Active', :active], ['Inactive', :inactive]]
188
- @options_gui = [["cocoa", :cocoa], ["qt", :qt], ["gtk", :gtk]]
189
- @options_so = [["Linux", :linux] , ["Mac OS X", :mac], ["Videogame", :win]]
190
- end
191
-
192
- it "test_helper_select_sym" do
193
- @user.status = :inactive
194
- output = "<select id=\"user_status\" name=\"user[status]\">#{options_for_select(@options_status, @user.status)}</select>"
195
- output.should eql(select_sym("user", "status", nil))
196
-
197
-
198
- output = "<select id=\"user_status\" name=\"user[status]\">#{options_for_select(@options_status, @user.status)}</select>"
199
- output.should eql(select_sym("user", "status", nil))
200
- end
201
-
202
- def test_helper_select_sym_order
203
- output_so = "<select id=\"user_so\" name=\"user[so]\">#{options_for_select(@options_so, @user.so)}</select>"
204
- output_office = "<select id=\"user_office\" name=\"user[office]\">#{options_for_select(@options_office, @user.office)}</select>"
205
-
206
- assert_equal output_so, select_sym("user", "so", nil)
207
- assert_equal output_office, select_sym("user", "office", nil)
208
- end
209
-
210
- def test_helper_radio_sym
211
- output = radio_sym("user", "status", nil)
212
- assert_equal("<label>Active: <input checked=\"checked\" id=\"user_status_active\" name=\"user[status]\" type=\"radio\" value=\"active\" /></label><label>Inactive: <input id=\"user_status_inactive\" name=\"user[status]\" type=\"radio\" value=\"inactive\" /></label>", output)
213
- end
214
-
215
- end
216
-
217
- describe "i18n" do
218
-
219
- it "should test i18n ones" do
220
- @user.language_text.should eql("Português")
221
- end
222
-
223
- it "should get the correct values" do
224
- User.get_language_values.should =~ [["Português", :pt], ["Inglês", :en]]
225
- end
226
-
227
- it "should get the correct values" do
228
- User::LANGUAGE_VALUES.should eql({:pt=>"pt", :en=>"en"})
229
- end
230
-
231
- it "should test boolean" do
232
- @user.sex_text.should eql("Feminino")
233
- end
234
-
235
- it "should get the correct values" do
236
- User.get_sex_values.should eql([["Feminino", true],["Masculino", false]])
237
- end
238
-
239
- it "should get the correct values" do
240
- User::SEX_VALUES.should eql({true=>"true", false=>"false"})
241
- end
242
-
243
- it "should translate a multiword class" do
244
- @skill = UserSkill.create(:kind => :magic)
245
- @skill.kind_text.should eql("Mágica")
246
- end
247
-
248
- it "should return nil if there's no value" do
249
- @skill = UserSkill.create(:kind => nil)
250
- @skill.kind_text.should be_nil
251
- end
252
-
253
- end
254
-
255
- describe "Methods" do
256
-
257
- it "should play nice with other stuff" do
258
- @user.karma.should be_nil
259
- User::KARMA_VALUES.should eql({:bad => "bad", :ugly => "ugly", :good => "good"})
260
- end
261
-
262
- it "should provide a boolean method" do
263
- @user.should_not be_good
264
- @user.karma = :ugly
265
- @user.should be_ugly
266
- end
267
-
268
- it "should work" do
269
- @user.karma = "good"
270
- @user.should be_good
271
- @user.should_not be_bad
272
- end
273
-
274
- end
275
-
276
- end
277
-
278
- describe "more tests on Permission" do
279
-
280
- it "should use default value on object build" do
281
- Permission.new.kind.should eql(:perm)
282
- end
283
-
284
- it "should not interfer on create" do
285
- Permission.create!(:name => "p7", :kind =>:temp, :lvl => 7)
286
- Permission.find_by_name("p7").kind.should eql(:temp)
287
- end
288
-
289
- it "should work on create" do
290
- pm = Permission.new(:name => "p7", :lvl => 7)
291
- pm.should be_valid
292
- pm.save.should be_true
293
- end
294
-
295
- it "should work on create" do
296
- Permission.create!(:name => "p8", :lvl => 9)
297
- Permission.find_by_name("p8").kind.should eql(:perm)
298
- end
299
-
300
- it "should work on edit" do
301
- pm = Permission.find_by_name("p8")
302
- pm.kind = :temp
303
- pm.save
304
- Permission.find_by_name("p8").kind.should eql(:temp)
305
- end
306
-
307
- it "should work with default values" do
308
- pm = Permission.new(:name => "p9")
309
- pm.lvl = 9
310
- pm.save
311
- Permission.find_by_name("p9").lvl.to_i.should eql(9)
312
- end
313
-
314
- end
315
-
316
- describe "ActiveRecord stuff" do
317
-
318
- #
319
- # ActiveRecord <= 2
320
- #
321
- if ActiveRecord::VERSION::MAJOR <= 2
322
-
323
- it "test_symbolized_finder" do
324
- User.find(:all, :conditions => { :status => :inactive }).map(&:name).should eql(['Bob'])
325
- User.find_all_by_status(:inactive).map(&:name).should eql(['Bob'])
326
- end
327
-
328
- it "test_symbolized_with_scope" do
329
- User.with_scope(:find => { :conditions => { :status => :inactive }}) do
330
- User.find(:all).map(&:name).should eql(['Bob'])
331
- end
332
- end
333
-
334
- describe "dirty tracking / changed flag" do
335
- before do
336
- @anna = User.find_by_name!('Anna')
337
- end
338
-
339
- it "is dirty if you change the attribute value" do
340
- @anna.language.should == :pt
341
- @anna.language_changed?.should be_false
342
-
343
- return_value = @anna.language = :en
344
- return_value.should == :en
345
- @anna.language_changed?.should be_true
346
- end
347
-
348
- it "is not dirty if you set the attribute value to the same value it was originally" do
349
- @anna.language.should == :pt
350
- @anna.language_changed?.should be_false
351
-
352
- return_value = @anna.language = :pt
353
- return_value.should == :pt
354
- @anna.language_changed?.should be_false
355
- end
356
- end
357
-
358
- #
359
- # ActiveRecord >= 3
360
- #
361
- else
362
-
363
- it "test_symbolized_finder" do
364
- User.where({ :status => :inactive }).all.map(&:name).should eql(['Bob'])
365
- User.find_all_by_status(:inactive).map(&:name).should eql(['Bob'])
366
- end
367
-
368
- it "test_symbolized_with_scope" do
369
- User.with_scope(:find => { :conditions => { :status => :inactive }}) do
370
- User.find(:all).map(&:name).should eql(['Bob'])
371
- end
372
- end
373
-
374
- describe "dirty tracking / changed flag" do
375
- before do
376
- @anna = User.find_by_name!('Anna')
377
- end
378
-
379
- it "is dirty if you change the attribute value" do
380
- @anna.language.should == :pt
381
- @anna.language_changed?.should be_false
382
-
383
- return_value = @anna.language = :en
384
- return_value.should == :en
385
- @anna.language_changed?.should be_true
386
- end
387
-
388
- it "is not dirty if you set the attribute value to the same value it was originally" do
389
- @anna.language.should == :pt
390
- @anna.language_changed?.should be_false
391
-
392
- return_value = @anna.language = :pt
393
- return_value.should == :pt
394
- p @anna.changes
395
- @anna.language_changed?.should be_false
396
- end
397
- end
398
-
399
- if ActiveRecord::VERSION::STRING <= "3.0"
400
- describe "Named Scopes" do
401
-
402
- before do
403
- @anna = User.find_by_name!('Anna')
404
- @bob = User.find_by_name!('Bob')
405
- end
406
-
407
- it "should have main named scope" do
408
- User.inactive.should == [@bob]
409
- end
410
-
411
- it "should have other to test better" do
412
- User.linux.should == [@anna]
413
- end
414
-
415
- it "should have 'with' helper" do
416
- User.with_sex.should == [@anna]
417
- end
418
-
419
- it "should have 'without' helper" do
420
- User.without_sex.should == [@bob]
421
- end
422
-
423
- it "should have 'attr_name' helper" do
424
- User.cool.should == [@anna]
425
- end
426
-
427
- it "should have 'not_attr_name' helper" do
428
- User.not_cool.should == [@bob]
429
- end
430
-
431
- end
432
- end
433
-
434
- end
435
-
436
- end
437
7
 
438
8
  end
data/symbolize.gemspec CHANGED
@@ -5,12 +5,12 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{symbolize}
8
- s.version = "3.3.0pre"
8
+ s.version = "4.0.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Marcos Piccinini"]
12
- s.date = %q{2011-07-12}
13
- s.description = %q{ActiveRecord enums with i18n}
12
+ s.date = %q{2011-09-28}
13
+ s.description = %q{ActiveRecord/Mongoid enums with i18n}
14
14
  s.email = %q{x@nofxx.com}
15
15
  s.extra_rdoc_files = [
16
16
  "README.rdoc"
@@ -21,22 +21,26 @@ Gem::Specification.new do |s|
21
21
  "README.rdoc",
22
22
  "Rakefile",
23
23
  "VERSION",
24
- "init.rb",
25
24
  "lib/symbolize.rb",
25
+ "lib/symbolize/active_record.rb",
26
+ "lib/symbolize/mongoid.rb",
26
27
  "lib/symbolize/railtie.rb",
27
28
  "lib/symbolize/symbolize_helper.rb",
28
- "rails/init.rb",
29
29
  "spec/db/001_create_testing_structure.rb",
30
30
  "spec/locales/en.yml",
31
31
  "spec/locales/pt.yml",
32
32
  "spec/spec_helper.rb",
33
+ "spec/spec_helper_ar.rb",
34
+ "spec/spec_helper_mongoid.rb",
35
+ "spec/symbolize/active_record_spec.rb",
36
+ "spec/symbolize/mongoid_spec.rb",
33
37
  "spec/symbolize_spec.rb",
34
38
  "symbolize.gemspec"
35
39
  ]
36
40
  s.homepage = %q{http://github.com/nofxx/symbolize}
37
41
  s.require_paths = ["lib"]
38
42
  s.rubygems_version = %q{1.3.7}
39
- s.summary = %q{ActiveRecord enums with i18n}
43
+ s.summary = %q{Object enums with i18n in AR or Mongoid}
40
44
 
41
45
  if s.respond_to? :specification_version then
42
46
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
metadata CHANGED
@@ -1,12 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: symbolize
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: true
4
+ prerelease: false
5
5
  segments:
6
- - 3
7
- - 3
8
- - 0pre
9
- version: 3.3.0pre
6
+ - 4
7
+ - 0
8
+ - 1
9
+ version: 4.0.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Marcos Piccinini
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-07-12 00:00:00 -03:00
17
+ date: 2011-09-28 00:00:00 -03:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: "0"
44
44
  type: :development
45
45
  version_requirements: *id002
46
- description: ActiveRecord enums with i18n
46
+ description: ActiveRecord/Mongoid enums with i18n
47
47
  email: x@nofxx.com
48
48
  executables: []
49
49
 
@@ -57,15 +57,19 @@ files:
57
57
  - README.rdoc
58
58
  - Rakefile
59
59
  - VERSION
60
- - init.rb
61
60
  - lib/symbolize.rb
61
+ - lib/symbolize/active_record.rb
62
+ - lib/symbolize/mongoid.rb
62
63
  - lib/symbolize/railtie.rb
63
64
  - lib/symbolize/symbolize_helper.rb
64
- - rails/init.rb
65
65
  - spec/db/001_create_testing_structure.rb
66
66
  - spec/locales/en.yml
67
67
  - spec/locales/pt.yml
68
68
  - spec/spec_helper.rb
69
+ - spec/spec_helper_ar.rb
70
+ - spec/spec_helper_mongoid.rb
71
+ - spec/symbolize/active_record_spec.rb
72
+ - spec/symbolize/mongoid_spec.rb
69
73
  - spec/symbolize_spec.rb
70
74
  - symbolize.gemspec
71
75
  has_rdoc: true
@@ -99,6 +103,6 @@ rubyforge_project:
99
103
  rubygems_version: 1.3.7
100
104
  signing_key:
101
105
  specification_version: 3
102
- summary: ActiveRecord enums with i18n
106
+ summary: Object enums with i18n in AR or Mongoid
103
107
  test_files: []
104
108
 
data/init.rb DELETED
@@ -1 +0,0 @@
1
- require File.dirname(__FILE__) + "/rails/init"
data/rails/init.rb DELETED
@@ -1,2 +0,0 @@
1
- require File.join(File.dirname(__FILE__), '..', "lib", "symbolize", "symbolize_helper")
2
- ActiveRecord::Base.send(:include, Symbolize)