enumerize 0.8.0 → 2.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +27 -12
  5. data/CHANGELOG.md +135 -0
  6. data/Gemfile +5 -21
  7. data/Gemfile.global +20 -0
  8. data/Gemfile.mongo_mapper +7 -0
  9. data/Gemfile.rails42 +7 -0
  10. data/Gemfile.rails51 +7 -0
  11. data/README.md +185 -9
  12. data/Rakefile +2 -4
  13. data/enumerize.gemspec +1 -0
  14. data/lib/enumerize/activerecord.rb +55 -27
  15. data/lib/enumerize/attribute.rb +41 -9
  16. data/lib/enumerize/base.rb +28 -17
  17. data/lib/enumerize/hooks/formtastic.rb +3 -8
  18. data/lib/enumerize/hooks/sequel_dataset.rb +15 -0
  19. data/lib/enumerize/hooks/simple_form.rb +5 -11
  20. data/lib/enumerize/hooks/uniqueness.rb +3 -8
  21. data/lib/enumerize/integrations/rspec/matcher.rb +107 -26
  22. data/lib/enumerize/mongoid.rb +13 -0
  23. data/lib/enumerize/predicatable.rb +7 -16
  24. data/lib/enumerize/predicates.rb +3 -1
  25. data/lib/enumerize/scope/activerecord.rb +37 -0
  26. data/lib/enumerize/scope/mongoid.rb +35 -0
  27. data/lib/enumerize/scope/sequel.rb +40 -0
  28. data/lib/enumerize/sequel.rb +57 -0
  29. data/lib/enumerize/set.rb +18 -8
  30. data/lib/enumerize/value.rb +10 -9
  31. data/lib/enumerize/version.rb +3 -1
  32. data/lib/enumerize.rb +23 -1
  33. data/spec/enumerize/integrations/rspec/matcher_spec.rb +258 -0
  34. data/spec/spec_helper.rb +28 -0
  35. data/test/activerecord_test.rb +233 -10
  36. data/test/attribute_test.rb +22 -0
  37. data/test/base_test.rb +7 -7
  38. data/test/formtastic_test.rb +3 -12
  39. data/test/mongo_mapper_test.rb +7 -1
  40. data/test/mongoid_test.rb +63 -7
  41. data/test/multiple_test.rb +21 -0
  42. data/test/predicates_test.rb +6 -0
  43. data/test/sequel_test.rb +291 -0
  44. data/test/set_test.rb +14 -0
  45. data/test/simple_form_test.rb +0 -1
  46. data/test/support/mock_controller.rb +4 -0
  47. data/test/support/view_test_helper.rb +4 -0
  48. data/test/test_helper.rb +23 -2
  49. data/test/value_test.rb +51 -21
  50. metadata +40 -9
  51. data/Gemfile.rails4 +0 -23
  52. data/lib/enumerize/form_helper.rb +0 -23
  53. data/test/rspec_matcher_test.rb +0 -76
  54. data/test/rspec_spec.rb +0 -13
@@ -1,11 +1,39 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_helper'
2
4
  require 'active_record'
3
5
  require 'logger'
4
6
 
7
+ db = (ENV['DB'] || 'sqlite3').to_sym
8
+
5
9
  silence_warnings do
6
10
  ActiveRecord::Migration.verbose = false
7
11
  ActiveRecord::Base.logger = Logger.new(nil)
8
- ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
12
+ ActiveRecord::Base.configurations = {
13
+ 'sqlite3' => {
14
+ 'adapter' => 'sqlite3',
15
+ 'database' => ':memory:'
16
+ },
17
+ 'postgresql' => {
18
+ 'adapter' => 'postgresql',
19
+ 'username' => ENV['DB_USER'],
20
+ 'password' => ENV['DB_PASSD'],
21
+ 'database' => 'enumerize_test'
22
+ },
23
+ 'postgresql_master' => {
24
+ 'adapter' => 'postgresql',
25
+ 'username' => ENV['DB_USER'],
26
+ 'password' => ENV['DB_PASS'],
27
+ 'database' => 'template1',
28
+ 'schema_search_path' => 'public'
29
+ }
30
+ }
31
+ if db == :postgresql
32
+ ActiveRecord::Base.establish_connection(:postgresql_master)
33
+ ActiveRecord::Base.connection.recreate_database('enumerize_test')
34
+ end
35
+
36
+ ActiveRecord::Base.establish_connection(db)
9
37
  end
10
38
 
11
39
  ActiveRecord::Base.connection.instance_eval do
@@ -15,13 +43,15 @@ ActiveRecord::Base.connection.instance_eval do
15
43
  t.string :lambda_role
16
44
  t.string :name
17
45
  t.string :interests
18
- t.string :status
46
+ t.integer :status
19
47
  t.string :account_type, :default => :basic
20
48
  end
21
49
 
22
50
  create_table :documents do |t|
51
+ t.integer :user_id
23
52
  t.string :visibility
24
- t.timestamps
53
+ t.integer :status
54
+ t.timestamps null: true
25
55
  end
26
56
  end
27
57
 
@@ -33,6 +63,9 @@ class BaseEntity < ActiveRecord::Base
33
63
  end
34
64
 
35
65
  class Document < BaseEntity
66
+ belongs_to :user
67
+
68
+ enumerize :status, in: {draft: 1, release: 2}
36
69
  end
37
70
 
38
71
  module RoleEnum
@@ -53,17 +86,28 @@ class User < ActiveRecord::Base
53
86
  enumerize :status, :in => { active: 1, blocked: 2 }, scope: true
54
87
 
55
88
  enumerize :account_type, :in => [:basic, :premium]
89
+
90
+ # There is no column for relationship enumeration for testing purposes: model
91
+ # should not be broken even if the associated column does not exist yet.
92
+ enumerize :relationship, :in => [:single, :married]
93
+
94
+ has_many :documents
56
95
  end
57
96
 
58
97
  class UniqStatusUser < User
59
98
  validates :status, uniqueness: true
99
+ validates :sex, presence: true
100
+ end
101
+
102
+ class InterestsRequiredUser < User
103
+ validates :interests, presence: true
60
104
  end
61
105
 
62
106
  describe Enumerize::ActiveRecordSupport do
63
107
  it 'sets nil if invalid value is passed' do
64
108
  user = User.new
65
109
  user.sex = :invalid
66
- user.sex.must_equal nil
110
+ user.sex.must_be_nil
67
111
  end
68
112
 
69
113
  it 'saves value' do
@@ -92,7 +136,8 @@ describe Enumerize::ActiveRecordSupport do
92
136
  it 'does not set default value for not selected attributes' do
93
137
  User.delete_all
94
138
  User.create!(:sex => :male)
95
- User.select(:id).collect(&:id)
139
+
140
+ assert_equal ['id'], User.select(:id).first.attributes.keys
96
141
  end
97
142
 
98
143
  it 'has default value with lambda' do
@@ -128,9 +173,18 @@ describe Enumerize::ActiveRecordSupport do
128
173
  user.errors[:role].must_include 'is not included in the list'
129
174
  end
130
175
 
131
- it 'validates inclusion when using write_attribute' do
176
+ it 'validates inclusion when using write_attribute with string attribute' do
177
+ user = User.new
178
+ user.send(:write_attribute, 'role', 'wrong')
179
+ user.read_attribute_for_validation(:role).must_equal 'wrong'
180
+ user.wont_be :valid?
181
+ user.errors[:role].must_include 'is not included in the list'
182
+ end
183
+
184
+ it 'validates inclusion when using write_attribute with symbol attribute' do
132
185
  user = User.new
133
186
  user.send(:write_attribute, :role, 'wrong')
187
+ user.read_attribute_for_validation(:role).must_equal 'wrong'
134
188
  user.wont_be :valid?
135
189
  user.errors[:role].must_include 'is not included in the list'
136
190
  end
@@ -143,7 +197,7 @@ describe Enumerize::ActiveRecordSupport do
143
197
 
144
198
  it "uses persisted value for validation if it hasn't been set" do
145
199
  user = User.create! :sex => :male
146
- User.find(user).read_attribute_for_validation(:sex).must_equal 'male'
200
+ User.find(user.id).read_attribute_for_validation(:sex).must_equal 'male'
147
201
  end
148
202
 
149
203
  it 'is valid with empty string assigned' do
@@ -155,7 +209,7 @@ describe Enumerize::ActiveRecordSupport do
155
209
  it 'stores nil when empty string assigned' do
156
210
  user = User.new
157
211
  user.role = ''
158
- user.read_attribute(:role).must_equal nil
212
+ user.read_attribute(:role).must_be_nil
159
213
  end
160
214
 
161
215
  it 'supports multiple attributes' do
@@ -179,6 +233,13 @@ describe Enumerize::ActiveRecordSupport do
179
233
  interests.must_equal %w(music dancing)
180
234
  end
181
235
 
236
+ it 'stores multiple value passed passed to new' do
237
+ user = User.new(interests: [:music, :dancing])
238
+ user.save!
239
+ user.interests.must_equal %w(music dancing)
240
+ User.find(user.id).interests.must_equal %w(music dancing)
241
+ end
242
+
182
243
  it 'returns invalid multiple value for validation' do
183
244
  user = User.new
184
245
  user.interests << :music
@@ -199,6 +260,22 @@ describe Enumerize::ActiveRecordSupport do
199
260
  user.must_be :valid?
200
261
  end
201
262
 
263
+ it 'stores custom values for multiple attributes' do
264
+ User.delete_all
265
+
266
+ klass = Class.new(User)
267
+ klass.enumerize :interests, in: { music: 0, sports: 1, dancing: 2, programming: 3}, multiple: true
268
+
269
+ user = klass.new
270
+ user.interests << :music
271
+ user.read_attribute(:interests).must_equal [0]
272
+ user.interests.must_equal %w(music)
273
+ user.save
274
+
275
+ user = klass.find(user.id)
276
+ user.interests.must_equal %w(music)
277
+ end
278
+
202
279
  it 'adds scope' do
203
280
  User.delete_all
204
281
 
@@ -215,6 +292,12 @@ describe Enumerize::ActiveRecordSupport do
215
292
  User.having_role(:admin).must_equal [user_1]
216
293
  end
217
294
 
295
+ it 'ignores not enumerized values that passed to the scope method' do
296
+ User.delete_all
297
+
298
+ User.with_status(:foo).must_equal []
299
+ end
300
+
218
301
  it 'allows either key or value as valid' do
219
302
  user_1 = User.new(status: :active)
220
303
  user_2 = User.new(status: 1)
@@ -258,6 +341,36 @@ describe Enumerize::ActiveRecordSupport do
258
341
  user.errors[:status].wont_be :empty?
259
342
  end
260
343
 
344
+ it 'validates presence with multiple attributes' do
345
+ user = InterestsRequiredUser.new
346
+ user.interests = []
347
+ user.valid?
348
+
349
+ user.errors[:interests].wont_be :empty?
350
+
351
+ user.interests = ['']
352
+ user.valid?
353
+
354
+ user.errors[:interests].wont_be :empty?
355
+
356
+ user.interests = [:dancing, :programming]
357
+ user.valid?
358
+
359
+ user.errors[:interests].must_be_empty
360
+ end
361
+
362
+ it 'is valid after #becomes' do
363
+ User.delete_all
364
+ user = User.new
365
+ user.sex = :male
366
+ user.save!
367
+
368
+ uniq_user = User.find(user.id).becomes(UniqStatusUser)
369
+ uniq_user.valid?
370
+
371
+ uniq_user.errors.must_be_empty
372
+ end
373
+
261
374
  it 'supports multiple attributes in #becomes' do
262
375
  User.delete_all
263
376
 
@@ -291,7 +404,117 @@ describe Enumerize::ActiveRecordSupport do
291
404
  user = User.create(:status => :active)
292
405
  user.status = :blocked
293
406
 
294
- expected = ActiveSupport::HashWithIndifferentAccess.new(status: [1, 2]).to_yaml
295
- assert_equal expected, user.changes.to_yaml
407
+ assert_equal [1, 2], YAML.load(user.changes.to_yaml)[:status]
408
+ end
409
+
410
+ it 'does not change by the practical same value' do
411
+ user = User.create!(status: 'active')
412
+ user.reload
413
+ user.status = 'active'
414
+
415
+ user.changes.must_be_empty
416
+ end
417
+
418
+ it 'allows using update_all' do
419
+ User.delete_all
420
+
421
+ user = User.create(status: :active, account_type: :premium)
422
+
423
+ User.update_all(status: :blocked)
424
+ user.reload
425
+ user.status.must_equal 'blocked'
426
+
427
+ User.update_all(status: :active, account_type: :basic)
428
+ user.reload
429
+ user.status.must_equal 'active'
430
+ user.account_type.must_equal 'basic'
431
+ end
432
+
433
+ it 'allows using update_all for multiple enumerize' do
434
+ User.delete_all
435
+
436
+ klass = Class.new(User)
437
+ klass.enumerize :interests, in: { music: 0, sports: 1, dancing: 2, programming: 3}, multiple: true
438
+
439
+ user = klass.create(status: :active)
440
+ klass.update_all(status: :blocked, interests: [:music, :dancing])
441
+
442
+ user = klass.find(user.id)
443
+ user.status.must_equal 'blocked'
444
+ user.interests.must_equal %w(music dancing)
445
+ end
446
+
447
+ it 'allows using update_all with values' do
448
+ User.delete_all
449
+
450
+ user = User.create(status: :active)
451
+
452
+ User.update_all(status: 2)
453
+ user.reload
454
+ user.status.must_equal 'blocked'
455
+ end
456
+
457
+ it 'allows using update_all on relation objects' do
458
+ User.delete_all
459
+
460
+ user = User.create(status: :active, account_type: :premium)
461
+
462
+ User.all.update_all(status: :blocked)
463
+ user.reload
464
+ user.status.must_equal 'blocked'
465
+ end
466
+
467
+ it 'allows using update_all on association relation objects' do
468
+ User.delete_all
469
+ Document.delete_all
470
+
471
+ user = User.create
472
+ document = Document.create(user: user, status: :draft)
473
+
474
+ user.documents.update_all(status: :release)
475
+ document.reload
476
+ document.status.must_equal 'release'
477
+ end
478
+
479
+ it 'preserves string usage of update_all' do
480
+ User.delete_all
481
+
482
+ user = User.create(name: "Fred")
483
+
484
+ User.update_all("name = 'Frederick'")
485
+ user.reload
486
+ user.name.must_equal 'Frederick'
487
+ end
488
+
489
+ it 'preserves interpolated array usage of update_all' do
490
+ User.delete_all
491
+
492
+ user = User.create(name: "Fred")
493
+
494
+ User.update_all(["name = :name", {name: 'Frederick'}])
495
+ user.reload
496
+ user.name.must_equal 'Frederick'
497
+ end
498
+
499
+ it 'sets attribute to nil if given one is not valid' do
500
+ User.delete_all
501
+
502
+ user = User.create(status: :active)
503
+
504
+ User.update_all(status: :foo)
505
+ user.reload
506
+ user.status.must_be_nil
507
+ end
508
+
509
+ it 'supports AR types serialization' do
510
+ type = User.type_for_attribute('status')
511
+ type.must_be_instance_of Enumerize::ActiveRecordSupport::Type
512
+ serialized = type.serialize('blocked')
513
+ serialized.must_equal 2
514
+ end
515
+
516
+ it 'has AR type itself JSON serializable' do
517
+ type = User.type_for_attribute('status')
518
+ type.as_json['attr'].must_equal 'status'
296
519
  end
297
520
  end
@@ -19,6 +19,12 @@ describe Enumerize::Attribute do
19
19
  attr.name.must_equal :foo
20
20
  end
21
21
 
22
+ it 'uses custom value class' do
23
+ value_class = Class.new(Enumerize::Value)
24
+ build_attr nil, 'foo', :in => %w[a b], :value_class => value_class
25
+ attr.values.first.must_be_instance_of value_class
26
+ end
27
+
22
28
  describe 'i18n scopes' do
23
29
  it 'returns scopes from options' do
24
30
  build_attr nil, 'foo', :in => %w[a b], :i18n_scope => %w[bar buzz]
@@ -79,6 +85,18 @@ describe Enumerize::Attribute do
79
85
  end
80
86
  end
81
87
 
88
+ it 'sets up shortcut methods for each value' do
89
+ build_attr nil, :foo, :in => {:a => 1, :b => 2}
90
+
91
+ attr.must_respond_to :a
92
+ attr.must_respond_to :b
93
+
94
+ attr.a.value.must_equal 1
95
+ attr.b.value.must_equal 2
96
+ attr.a.text.must_equal 'A'
97
+ attr.b.text.must_equal 'B'
98
+ end
99
+
82
100
  describe 'values hash with zero' do
83
101
  before do
84
102
  build_attr nil, :foo, :in => {:a => 1, :b => 2, :c => 0}
@@ -93,6 +111,10 @@ describe Enumerize::Attribute do
93
111
  attr.find_value(2).must_equal 'b'
94
112
  attr.find_value(0).must_equal 'c'
95
113
  end
114
+
115
+ it 'finds all values by hash values' do
116
+ attr.find_values(1, 2, 0).must_equal ['a', 'b', 'c']
117
+ end
96
118
  end
97
119
 
98
120
  describe 'boolean values hash' do
data/test/base_test.rb CHANGED
@@ -15,7 +15,7 @@ describe Enumerize::Base do
15
15
 
16
16
  it 'returns nil when not set' do
17
17
  klass.enumerize(:foo, :in => [:a, :b])
18
- object.foo.must_equal nil
18
+ object.foo.must_be_nil
19
19
  end
20
20
 
21
21
  it 'returns value that was set' do
@@ -37,11 +37,11 @@ describe Enumerize::Base do
37
37
  it 'returns nil as translation when value is nil' do
38
38
  store_translations(:en, :enumerize => {:foo => {:a => 'a text'}}) do
39
39
  klass.enumerize(:foo, :in => [:a, :b])
40
- object.foo_text.must_equal nil
40
+ object.foo_text.must_be_nil
41
41
  end
42
42
  end
43
43
 
44
- it 'scopes translation by i18 key' do
44
+ it 'scopes translation by i18n key' do
45
45
  def klass.model_name
46
46
  name = "ExampleClass"
47
47
  def name.i18n_key
@@ -120,7 +120,7 @@ describe Enumerize::Base do
120
120
  klass.enumerize(:foo, :in => %w[a b])
121
121
  subklass.enumerize(:bar, :in => %w[c d])
122
122
 
123
- klass.enumerized_attributes[:bar].must_equal nil
123
+ klass.enumerized_attributes[:bar].must_be_nil
124
124
  end
125
125
 
126
126
  it 'adds new parent class attributes to subclass' do
@@ -132,7 +132,7 @@ describe Enumerize::Base do
132
132
  it 'stores nil value' do
133
133
  klass.enumerize(:foo, :in => [:a, :b])
134
134
  object.foo = nil
135
- object.instance_variable_get(:@foo).must_equal nil
135
+ object.instance_variable_get(:@foo).must_be_nil
136
136
  end
137
137
 
138
138
  it 'casts value to string for validation' do
@@ -144,7 +144,7 @@ describe Enumerize::Base do
144
144
  it "doesn't cast nil to string for validation" do
145
145
  klass.enumerize(:foo, :in => [:a, :b])
146
146
  object.foo = nil
147
- object.read_attribute_for_validation(:foo).must_equal nil
147
+ object.read_attribute_for_validation(:foo).must_be_nil
148
148
  end
149
149
 
150
150
  it 'calls super in the accessor method' do
@@ -171,7 +171,7 @@ describe Enumerize::Base do
171
171
 
172
172
  object = klass.new
173
173
  object.foo.must_be_nil
174
- object.attributes.must_equal({:foo => nil})
174
+ object.attributes.must_equal({})
175
175
 
176
176
  object.foo = 'test'
177
177
  object.foo.must_equal 'test'
@@ -1,16 +1,7 @@
1
1
  require 'test_helper'
2
- require 'rails'
3
-
4
- module Formtastic
5
- module Helpers
6
- module InputHelper
7
- remove_method :input_class
8
- def input_class(as)
9
- input_class_with_const_defined(as)
10
- end
11
- end
12
- end
13
- end
2
+
3
+ Formtastic::FormBuilder.action_class_finder = Formtastic::ActionClassFinder
4
+ Formtastic::FormBuilder.input_class_finder = Formtastic::InputClassFinder
14
5
 
15
6
  class FormtasticSpec < MiniTest::Spec
16
7
  include ViewTestHelper
@@ -1,5 +1,7 @@
1
1
  require 'test_helper'
2
2
 
3
+ begin
4
+
3
5
  silence_warnings do
4
6
  require 'mongo_mapper'
5
7
  end
@@ -27,7 +29,7 @@ describe Enumerize do
27
29
  it 'sets nil if invalid value is passed' do
28
30
  user = model.new
29
31
  user.sex = :invalid
30
- user.sex.must_equal nil
32
+ user.sex.must_be_nil
31
33
  end
32
34
 
33
35
  it 'saves value' do
@@ -66,3 +68,7 @@ describe Enumerize do
66
68
  user.sex.must_equal 'female'
67
69
  end
68
70
  end
71
+
72
+ rescue LoadError
73
+ # Skip
74
+ end
data/test/mongoid_test.rb CHANGED
@@ -1,13 +1,14 @@
1
1
  require 'test_helper'
2
2
 
3
+ begin
4
+
3
5
  silence_warnings do
4
6
  require 'mongoid'
5
7
  end
6
8
 
7
9
  Mongoid.configure do |config|
8
- config.sessions = { :default => { :database => 'enumerize-test-suite', hosts: ['127.0.0.1:27017'] } }
9
- config.use_utc = true
10
- config.include_root_in_json = true
10
+ config.connect_to('enumerize-test-suite')
11
+ config.options = { use_utc: true, include_root_in_json: true }
11
12
  end
12
13
 
13
14
  describe Enumerize do
@@ -17,9 +18,10 @@ describe Enumerize do
17
18
 
18
19
  field :sex
19
20
  field :role
20
- enumerize :sex, :in => %w[male female]
21
- enumerize :role, :in => %w[admin user], :default => 'user'
22
- enumerize :mult, :in => %w[one two three four], :multiple => true
21
+ enumerize :sex, :in => %w[male female], scope: true
22
+ enumerize :status, :in => %w[notice warning error], scope: true
23
+ enumerize :role, :in => %w[admin user], :default => 'user', scope: :having_role
24
+ enumerize :mult, :in => %w[one two three four], :multiple => true
23
25
  end
24
26
 
25
27
  before { $VERBOSE = nil }
@@ -30,7 +32,7 @@ describe Enumerize do
30
32
  it 'sets nil if invalid value is passed' do
31
33
  user = model.new
32
34
  user.sex = :invalid
33
- user.sex.must_equal nil
35
+ user.sex.must_be_nil
34
36
  end
35
37
 
36
38
  it 'saves value' do
@@ -55,6 +57,21 @@ describe Enumerize do
55
57
  model.new.role.must_equal 'user'
56
58
  end
57
59
 
60
+ it 'uses after_initialize callback to set default value' do
61
+ model.delete_all
62
+ model.create!(sex: 'male', role: nil)
63
+
64
+ user = model.where(sex: 'male').first
65
+ user.role.must_equal 'user'
66
+ end
67
+
68
+ it 'does not set default value for not selected attributes' do
69
+ model.delete_all
70
+ model.create!(sex: :male)
71
+
72
+ assert_equal ['_id'], model.only(:id).first.attributes.keys
73
+ end
74
+
58
75
  it 'validates inclusion' do
59
76
  user = model.new
60
77
  user.role = 'wrong'
@@ -76,4 +93,43 @@ describe Enumerize do
76
93
  user = model.first
77
94
  user.mult.to_a.must_equal ['one', 'two']
78
95
  end
96
+
97
+ it 'adds scope' do
98
+ model.delete_all
99
+
100
+ user_1 = model.create!(sex: :male, role: :admin)
101
+ user_2 = model.create!(sex: :female, role: :user)
102
+
103
+ model.with_sex(:male).to_a.must_equal [user_1]
104
+ model.with_sex(:female).to_a.must_equal [user_2]
105
+ model.with_sex(:male, :female).to_set.must_equal [user_1, user_2].to_set
106
+
107
+ model.without_sex(:male).to_a.must_equal [user_2]
108
+ model.without_sex(:female).to_a.must_equal [user_1]
109
+ model.without_sex(:male, :female).to_a.must_equal []
110
+
111
+ model.having_role(:admin).to_a.must_equal [user_1]
112
+ model.having_role(:user).to_a.must_equal [user_2]
113
+ end
114
+
115
+ it 'chains scopes' do
116
+ model.delete_all
117
+
118
+ user_1 = model.create!(status: :notice)
119
+ user_2 = model.create!(status: :warning)
120
+ user_3 = model.create!(status: :error)
121
+
122
+ model.with_status(:notice, :warning).with_status(:notice, :error).to_a.must_equal [user_1]
123
+ model.with_status(:notice, :warning).union.with_status(:notice, :error).to_a.must_equal [user_1, user_2, user_3]
124
+ end
125
+
126
+ it 'ignores not enumerized values that passed to the scope method' do
127
+ model.delete_all
128
+
129
+ model.with_sex(:foo).must_equal []
130
+ end
131
+ end
132
+
133
+ rescue LoadError
134
+ # Skip
79
135
  end
@@ -24,6 +24,21 @@ describe Enumerize::Base do
24
24
  object.foos.must_equal %w(a c)
25
25
  end
26
26
 
27
+ it 'sets default value as single value' do
28
+ klass.enumerize :foos, in: %w(a b c), default: 'b', multiple: true
29
+ object.foos.must_equal %w(b)
30
+ end
31
+
32
+ it 'sets default value as array of one element' do
33
+ klass.enumerize :foos, in: %w(a b c), default: %w(b), multiple: true
34
+ object.foos.must_equal %w(b)
35
+ end
36
+
37
+ it 'sets default value as array of several elements' do
38
+ klass.enumerize :foos, in: %w(a b c), default: %w(b c), multiple: true
39
+ object.foos.must_equal %w(b c)
40
+ end
41
+
27
42
  it "doesn't define _text method" do
28
43
  klass.enumerize :foos, in: %w(a b c), multiple: true
29
44
  object.wont_respond_to :foos_text
@@ -33,4 +48,10 @@ describe Enumerize::Base do
33
48
  klass.enumerize :foos, in: %w(a b c), multiple: true
34
49
  object.wont_respond_to :foos_value
35
50
  end
51
+
52
+ it "cannot define multiple with scope" do
53
+ assert_raises ArgumentError do
54
+ klass.enumerize :foos, in: %w(a b c), multiple: true, scope: true
55
+ end
56
+ end
36
57
  end
@@ -15,6 +15,12 @@ describe Enumerize::Predicates do
15
15
  object.must_respond_to :b?
16
16
  end
17
17
 
18
+ it 'creates predicate methods when enumerized values have dash in it' do
19
+ klass.enumerize(:foo, in: %w(foo-bar bar-foo), predicates: true)
20
+ object.must_respond_to :foo_bar?
21
+ object.must_respond_to :bar_foo?
22
+ end
23
+
18
24
  it 'creates predicate methods on multiple attribute' do
19
25
  klass.enumerize(:foo, in: %w(a b), predicates: true, multiple: true)
20
26
  object.must_respond_to :a?