enumerize 2.0.1 → 2.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. checksums.yaml +5 -5
  2. data/.github/workflows/ruby.yml +73 -0
  3. data/CHANGELOG.md +80 -3
  4. data/Gemfile +3 -3
  5. data/Gemfile.global +2 -9
  6. data/Gemfile.mongo_mapper +2 -2
  7. data/Gemfile.rails60 +6 -0
  8. data/Gemfile.rails61 +6 -0
  9. data/Gemfile.rails70 +9 -0
  10. data/Gemfile.railsmaster +5 -0
  11. data/README.md +209 -92
  12. data/Rakefile +2 -0
  13. data/lib/enumerize/activemodel.rb +47 -0
  14. data/lib/enumerize/activerecord.rb +79 -0
  15. data/lib/enumerize/attribute.rb +19 -11
  16. data/lib/enumerize/attribute_map.rb +2 -0
  17. data/lib/enumerize/base.rb +6 -6
  18. data/lib/enumerize/hooks/formtastic.rb +4 -1
  19. data/lib/enumerize/hooks/sequel_dataset.rb +2 -0
  20. data/lib/enumerize/hooks/simple_form.rb +4 -1
  21. data/lib/enumerize/hooks/uniqueness.rb +5 -1
  22. data/lib/enumerize/integrations/rails_admin.rb +3 -1
  23. data/lib/enumerize/integrations/rspec/matcher.rb +7 -2
  24. data/lib/enumerize/integrations/rspec.rb +2 -0
  25. data/lib/enumerize/module.rb +2 -0
  26. data/lib/enumerize/module_attributes.rb +2 -0
  27. data/lib/enumerize/mongoid.rb +16 -0
  28. data/lib/enumerize/predicatable.rb +3 -1
  29. data/lib/enumerize/predicates.rb +2 -0
  30. data/lib/enumerize/scope/activerecord.rb +16 -0
  31. data/lib/enumerize/scope/mongoid.rb +15 -0
  32. data/lib/enumerize/scope/sequel.rb +16 -0
  33. data/lib/enumerize/sequel.rb +9 -4
  34. data/lib/enumerize/set.rb +2 -0
  35. data/lib/enumerize/utils.rb +12 -0
  36. data/lib/enumerize/value.rb +14 -15
  37. data/lib/enumerize/version.rb +3 -1
  38. data/lib/enumerize.rb +9 -0
  39. data/lib/sequel/plugins/enumerize.rb +18 -0
  40. data/spec/enumerize/integrations/rspec/matcher_spec.rb +13 -10
  41. data/spec/spec_helper.rb +2 -0
  42. data/test/activemodel_test.rb +114 -0
  43. data/test/activerecord_test.rb +277 -79
  44. data/test/attribute_map_test.rb +9 -7
  45. data/test/attribute_test.rb +37 -30
  46. data/test/base_test.rb +66 -64
  47. data/test/formtastic_test.rb +25 -0
  48. data/test/module_attributes_test.rb +10 -8
  49. data/test/mongo_mapper_test.rb +19 -10
  50. data/test/mongoid_test.rb +51 -22
  51. data/test/multiple_test.rb +26 -18
  52. data/test/predicates_test.rb +30 -28
  53. data/test/rails_admin_test.rb +8 -6
  54. data/test/sequel_test.rb +111 -58
  55. data/test/set_test.rb +28 -26
  56. data/test/simple_form_test.rb +25 -0
  57. data/test/support/mock_controller.rb +6 -0
  58. data/test/support/shared_enums.rb +43 -0
  59. data/test/support/view_test_helper.rb +14 -1
  60. data/test/test_helper.rb +2 -0
  61. data/test/value_test.rb +51 -30
  62. metadata +14 -27
  63. data/.travis.yml +0 -18
  64. data/Gemfile.rails42 +0 -6
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_helper'
2
4
 
3
5
  module Enumerize
@@ -9,12 +11,12 @@ module Enumerize
9
11
  end
10
12
 
11
13
  it 'empty when no attrs' do
12
- subject.must_be_empty
14
+ expect(subject).must_be_empty
13
15
  end
14
16
 
15
17
  it 'not empty when attr added' do
16
18
  subject << make_attr(:a)
17
- subject.wont_be_empty
19
+ expect(subject).wont_be_empty
18
20
  end
19
21
 
20
22
  it 'iterates over added attrs' do
@@ -32,20 +34,20 @@ module Enumerize
32
34
  actual << element
33
35
  end
34
36
 
35
- count.must_equal 2
36
- actual.must_equal [attr_1, attr_2]
37
+ expect(count).must_equal 2
38
+ expect(actual).must_equal [attr_1, attr_2]
37
39
  end
38
40
 
39
41
  it 'reads attribute by name' do
40
42
  attr = make_attr(:a)
41
43
  subject << attr
42
- subject[:a].must_equal attr
44
+ expect(subject[:a]).must_equal attr
43
45
  end
44
46
 
45
47
  it 'reads attribute by name using string' do
46
48
  attr = make_attr(:a)
47
49
  subject << attr
48
- subject['a'].must_equal attr
50
+ expect(subject['a']).must_equal attr
49
51
  end
50
52
 
51
53
  it 'updates dependants' do
@@ -62,7 +64,7 @@ module Enumerize
62
64
  subject << attr
63
65
  dependant = AttributeMap.new
64
66
  subject.add_dependant dependant
65
- dependant[:a].must_equal attr
67
+ expect(dependant[:a]).must_equal attr
66
68
  end
67
69
  end
68
70
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_helper'
2
4
 
3
5
  describe Enumerize::Attribute do
@@ -11,28 +13,33 @@ describe Enumerize::Attribute do
11
13
 
12
14
  it 'returns values' do
13
15
  build_attr nil, :foo, :in => [:a, :b]
14
- attr.values.must_equal %w[a b]
16
+ expect(attr.values).must_equal %w[a b]
17
+ end
18
+
19
+ it 'returns frozen values' do
20
+ build_attr nil, :foo, :in => [:a, :b]
21
+ expect(attr.values.map(&:frozen?)).must_equal [true, true]
15
22
  end
16
23
 
17
24
  it 'converts name to symbol' do
18
25
  build_attr nil, 'foo', :in => %w[a b]
19
- attr.name.must_equal :foo
26
+ expect(attr.name).must_equal :foo
20
27
  end
21
28
 
22
29
  it 'uses custom value class' do
23
30
  value_class = Class.new(Enumerize::Value)
24
31
  build_attr nil, 'foo', :in => %w[a b], :value_class => value_class
25
- attr.values.first.must_be_instance_of value_class
32
+ expect(attr.values.first).must_be_instance_of value_class
26
33
  end
27
34
 
28
35
  describe 'i18n scopes' do
29
36
  it 'returns scopes from options' do
30
37
  build_attr nil, 'foo', :in => %w[a b], :i18n_scope => %w[bar buzz]
31
- attr.i18n_scopes.must_equal %w[bar buzz]
38
+ expect(attr.i18n_scopes).must_equal %w[bar buzz]
32
39
  end
33
40
 
34
41
  it 'accepts only string scopes' do
35
- proc { build_attr nil, 'foo', :in => %w[a b], :i18n_scope => [%w[bar buzz], "bar.buzz"] }.must_raise ArgumentError
42
+ expect(proc { build_attr nil, 'foo', :in => %w[a b], :i18n_scope => [%w[bar buzz], "bar.buzz"] }).must_raise ArgumentError
36
43
  end
37
44
  end
38
45
 
@@ -40,32 +47,32 @@ describe Enumerize::Attribute do
40
47
  it 'returns all options for select' do
41
48
  store_translations(:en, :enumerize => {:foo => {:a => 'a text', :b => 'b text'}}) do
42
49
  build_attr nil, :foo, :in => %w[a b]
43
- attr.options.must_equal [['a text', 'a'], ['b text', 'b']]
50
+ expect(attr.options).must_equal [['a text', 'a'], ['b text', 'b']]
44
51
  end
45
52
  end
46
53
 
47
54
  it 'returns requested options for select via :only' do
48
55
  store_translations(:en, :enumerize => {:foo => {:a => 'a text', :b => 'b text'}}) do
49
56
  build_attr nil, :foo, :in => %w[a b]
50
- attr.options(:only => :a).must_equal [['a text', 'a']]
51
- attr.options(:only => [:b]).must_equal [['b text', 'b']]
52
- attr.options(:only => []).must_equal []
57
+ expect(attr.options(:only => :a)).must_equal [['a text', 'a']]
58
+ expect(attr.options(:only => [:b])).must_equal [['b text', 'b']]
59
+ expect(attr.options(:only => [])).must_equal []
53
60
  end
54
61
  end
55
62
 
56
63
  it 'returns requested options for select via :except' do
57
64
  store_translations(:en, :enumerize => {:foo => {:a => 'a text', :b => 'b text'}}) do
58
65
  build_attr nil, :foo, :in => %w[a b]
59
- attr.options(:except => :a).must_equal [['b text', 'b']]
60
- attr.options(:except => :b).must_equal [['a text', 'a']]
61
- attr.options(:except => []).must_equal [['a text', 'a'], ['b text', 'b']]
66
+ expect(attr.options(:except => :a)).must_equal [['b text', 'b']]
67
+ expect(attr.options(:except => :b)).must_equal [['a text', 'a']]
68
+ expect(attr.options(:except => [])).must_equal [['a text', 'a'], ['b text', 'b']]
62
69
  end
63
70
  end
64
71
 
65
72
  it 'does not work with both :only and :except' do
66
73
  store_translations(:en, :enumerize => {:foo => {:a => 'a text', :b => 'b text'}}) do
67
74
  build_attr nil, :foo, :in => %w[a b]
68
- proc { attr.options(:except => [], :only => []) }.must_raise ArgumentError
75
+ expect(proc { attr.options(:except => [], :only => []) }).must_raise ArgumentError
69
76
  end
70
77
  end
71
78
  end
@@ -76,25 +83,25 @@ describe Enumerize::Attribute do
76
83
  end
77
84
 
78
85
  it 'returns hash keys as values' do
79
- attr.values.must_equal %w[a b]
86
+ expect(attr.values).must_equal %w[a b]
80
87
  end
81
88
 
82
89
  it 'finds values by hash values' do
83
- attr.find_value(1).must_equal 'a'
84
- attr.find_value(2).must_equal 'b'
90
+ expect(attr.find_value(1)).must_equal 'a'
91
+ expect(attr.find_value(2)).must_equal 'b'
85
92
  end
86
93
  end
87
94
 
88
95
  it 'sets up shortcut methods for each value' do
89
96
  build_attr nil, :foo, :in => {:a => 1, :b => 2}
90
97
 
91
- attr.must_respond_to :a
92
- attr.must_respond_to :b
98
+ expect(attr).must_respond_to :a
99
+ expect(attr).must_respond_to :b
93
100
 
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'
101
+ expect(attr.a.value).must_equal 1
102
+ expect(attr.b.value).must_equal 2
103
+ expect(attr.a.text).must_equal 'A'
104
+ expect(attr.b.text).must_equal 'B'
98
105
  end
99
106
 
100
107
  describe 'values hash with zero' do
@@ -103,17 +110,17 @@ describe Enumerize::Attribute do
103
110
  end
104
111
 
105
112
  it 'returns hash keys as values' do
106
- attr.values.must_equal %w[a b c]
113
+ expect(attr.values).must_equal %w[a b c]
107
114
  end
108
115
 
109
116
  it 'finds values by hash values' do
110
- attr.find_value(1).must_equal 'a'
111
- attr.find_value(2).must_equal 'b'
112
- attr.find_value(0).must_equal 'c'
117
+ expect(attr.find_value(1)).must_equal 'a'
118
+ expect(attr.find_value(2)).must_equal 'b'
119
+ expect(attr.find_value(0)).must_equal 'c'
113
120
  end
114
121
 
115
122
  it 'finds all values by hash values' do
116
- attr.find_values(1, 2, 0).must_equal ['a', 'b', 'c']
123
+ expect(attr.find_values(1, 2, 0)).must_equal ['a', 'b', 'c']
117
124
  end
118
125
  end
119
126
 
@@ -123,12 +130,12 @@ describe Enumerize::Attribute do
123
130
  end
124
131
 
125
132
  it 'returns hash keys as values' do
126
- attr.values.must_equal %w[a b]
133
+ expect(attr.values).must_equal %w[a b]
127
134
  end
128
135
 
129
136
  it 'finds values by hash values' do
130
- attr.find_value(true).must_equal 'a'
131
- attr.find_value(false).must_equal 'b'
137
+ expect(attr.find_value(true)).must_equal 'a'
138
+ expect(attr.find_value(false)).must_equal 'b'
132
139
  end
133
140
  end
134
141
  end
data/test/base_test.rb CHANGED
@@ -1,49 +1,51 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_helper'
2
4
 
3
5
  describe Enumerize::Base do
4
- let(:klass) do
6
+ let(:kklass) do
5
7
  Class.new do
6
8
  extend Enumerize
7
9
  end
8
10
  end
9
11
 
10
12
  let(:subklass) do
11
- Class.new(klass)
13
+ Class.new(kklass)
12
14
  end
13
15
 
14
- let(:object) { klass.new }
16
+ let(:object) { kklass.new }
15
17
 
16
18
  it 'returns nil when not set' do
17
- klass.enumerize(:foo, :in => [:a, :b])
18
- object.foo.must_equal nil
19
+ kklass.enumerize(:foo, :in => [:a, :b])
20
+ expect(object.foo).must_be_nil
19
21
  end
20
22
 
21
23
  it 'returns value that was set' do
22
- klass.enumerize(:foo, :in => [:a, :b])
24
+ kklass.enumerize(:foo, :in => [:a, :b])
23
25
  object.foo = :a
24
- object.foo.must_equal 'a'
26
+ expect(object.foo).must_equal 'a'
25
27
  end
26
28
 
27
29
  it 'returns translation' do
28
30
  store_translations(:en, :enumerize => {:foo => {:a => 'a text'}}) do
29
- klass.enumerize(:foo, :in => [:a, :b])
31
+ kklass.enumerize(:foo, :in => [:a, :b])
30
32
  object.foo = :a
31
- object.foo.text.must_equal 'a text'
32
- object.foo_text.must_equal 'a text'
33
- object.foo_text.must_equal 'a text'
33
+ expect(object.foo.text).must_equal 'a text'
34
+ expect(object.foo_text).must_equal 'a text'
35
+ expect(object.foo_text).must_equal 'a text'
34
36
  end
35
37
  end
36
38
 
37
39
  it 'returns nil as translation when value is nil' do
38
40
  store_translations(:en, :enumerize => {:foo => {:a => 'a text'}}) do
39
- klass.enumerize(:foo, :in => [:a, :b])
40
- object.foo_text.must_equal nil
41
+ kklass.enumerize(:foo, :in => [:a, :b])
42
+ expect(object.foo_text).must_be_nil
41
43
  end
42
44
  end
43
45
 
44
46
  it 'scopes translation by i18n key' do
45
- def klass.model_name
46
- name = "ExampleClass"
47
+ def kklass.model_name
48
+ name = String.new("ExampleClass")
47
49
  def name.i18n_key
48
50
  'example_class'
49
51
  end
@@ -52,99 +54,99 @@ describe Enumerize::Base do
52
54
  end
53
55
 
54
56
  store_translations(:en, :enumerize => {:example_class => {:foo => {:a => 'a text scoped'}}}) do
55
- klass.enumerize(:foo, :in => [:a, :b])
57
+ kklass.enumerize(:foo, :in => [:a, :b])
56
58
  object.foo = :a
57
- object.foo.text.must_equal 'a text scoped'
58
- object.foo_text.must_equal 'a text scoped'
59
+ expect(object.foo.text).must_equal 'a text scoped'
60
+ expect(object.foo_text).must_equal 'a text scoped'
59
61
  end
60
62
  end
61
63
 
62
64
  it 'returns humanized value if there are no translations' do
63
65
  store_translations(:en, :enumerize => {}) do
64
- klass.enumerize(:foo, :in => [:a, :b])
66
+ kklass.enumerize(:foo, :in => [:a, :b])
65
67
  object.foo = :a
66
- object.foo_text.must_equal 'A'
68
+ expect(object.foo_text).must_equal 'A'
67
69
  end
68
70
  end
69
71
 
70
72
  it 'stores value as string' do
71
- klass.enumerize(:foo, :in => [:a, :b])
73
+ kklass.enumerize(:foo, :in => [:a, :b])
72
74
  object.foo = :a
73
- object.instance_variable_get(:@foo).must_be_instance_of String
75
+ expect(object.instance_variable_get(:@foo)).must_be_instance_of String
74
76
  end
75
77
 
76
78
  it 'handles default value' do
77
- klass.enumerize(:foo, :in => [:a, :b], :default => :b)
78
- object.foo.must_equal 'b'
79
+ kklass.enumerize(:foo, :in => [:a, :b], :default => :b)
80
+ expect(object.foo).must_equal 'b'
79
81
  end
80
82
 
81
83
  it 'handles default value with lambda' do
82
- klass.enumerize(:foo, :in => [:a, :b], :default => lambda { :b })
83
- object.foo.must_equal 'b'
84
+ kklass.enumerize(:foo, :in => [:a, :b], :default => lambda { :b })
85
+ expect(object.foo).must_equal 'b'
84
86
  end
85
87
 
86
88
  it 'injects object instance into lamda default value' do
87
- klass.enumerize(:foo, :in => [:a, :b], :default => lambda { |obj| :b if obj.is_a? klass })
88
- object.foo.must_equal 'b'
89
+ kklass.enumerize(:foo, :in => [:a, :b], :default => lambda { |obj| :b if obj.is_a? kklass })
90
+ expect(object.foo).must_equal 'b'
89
91
  end
90
92
 
91
93
  it 'raises exception on invalid default value' do
92
- proc {
93
- klass.enumerize(:foo, :in => [:a, :b], :default => :c)
94
- }.must_raise ArgumentError
94
+ expect(proc {
95
+ kklass.enumerize(:foo, :in => [:a, :b], :default => :c)
96
+ }).must_raise ArgumentError
95
97
  end
96
98
 
97
99
  it 'has enumerized attributes' do
98
- klass.enumerized_attributes.must_be_empty
99
- klass.enumerize(:foo, :in => %w[a b])
100
- klass.enumerized_attributes[:foo].must_be_instance_of Enumerize::Attribute
100
+ expect(kklass.enumerized_attributes).must_be_empty
101
+ kklass.enumerize(:foo, :in => %w[a b])
102
+ expect(kklass.enumerized_attributes[:foo]).must_be_instance_of Enumerize::Attribute
101
103
  end
102
104
 
103
105
  it "doesn't override existing method" do
104
- method = klass.method(:name)
105
- klass.enumerize(:name, :in => %w[a b], :default => 'a')
106
- klass.method(:name).must_equal method
106
+ method = kklass.method(:name)
107
+ kklass.enumerize(:name, :in => %w[a b], :default => 'a')
108
+ expect(kklass.method(:name)).must_equal method
107
109
  end
108
110
 
109
111
  it "inherits enumerized attributes from a parent class" do
110
- klass.enumerize(:foo, :in => %w[a b])
111
- subklass.enumerized_attributes[:foo].must_be_instance_of Enumerize::Attribute
112
+ kklass.enumerize(:foo, :in => %w[a b])
113
+ expect(subklass.enumerized_attributes[:foo]).must_be_instance_of Enumerize::Attribute
112
114
  end
113
115
 
114
116
  it "inherits enumerized attributes from a grandparent class" do
115
- klass.enumerize(:foo, :in => %w[a b])
116
- Class.new(subklass).enumerized_attributes[:foo].must_be_instance_of Enumerize::Attribute
117
+ kklass.enumerize(:foo, :in => %w[a b])
118
+ expect(Class.new(subklass).enumerized_attributes[:foo]).must_be_instance_of Enumerize::Attribute
117
119
  end
118
120
 
119
121
  it "doesn't add enumerized attributes to parent class" do
120
- klass.enumerize(:foo, :in => %w[a b])
122
+ kklass.enumerize(:foo, :in => %w[a b])
121
123
  subklass.enumerize(:bar, :in => %w[c d])
122
124
 
123
- klass.enumerized_attributes[:bar].must_equal nil
125
+ expect(kklass.enumerized_attributes[:bar]).must_be_nil
124
126
  end
125
127
 
126
128
  it 'adds new parent class attributes to subclass' do
127
- subklass = Class.new(klass)
128
- klass.enumerize :foo, :in => %w[a b]
129
- subklass.enumerized_attributes[:foo].must_be_instance_of Enumerize::Attribute
129
+ subklass = Class.new(kklass)
130
+ kklass.enumerize :foo, :in => %w[a b]
131
+ expect(subklass.enumerized_attributes[:foo]).must_be_instance_of Enumerize::Attribute
130
132
  end
131
133
 
132
134
  it 'stores nil value' do
133
- klass.enumerize(:foo, :in => [:a, :b])
135
+ kklass.enumerize(:foo, :in => [:a, :b])
134
136
  object.foo = nil
135
- object.instance_variable_get(:@foo).must_equal nil
137
+ expect(object.instance_variable_get(:@foo)).must_be_nil
136
138
  end
137
139
 
138
140
  it 'casts value to string for validation' do
139
- klass.enumerize(:foo, :in => [:a, :b])
141
+ kklass.enumerize(:foo, :in => [:a, :b])
140
142
  object.foo = :c
141
- object.read_attribute_for_validation(:foo).must_equal 'c'
143
+ expect(object.read_attribute_for_validation(:foo)).must_equal 'c'
142
144
  end
143
145
 
144
146
  it "doesn't cast nil to string for validation" do
145
- klass.enumerize(:foo, :in => [:a, :b])
147
+ kklass.enumerize(:foo, :in => [:a, :b])
146
148
  object.foo = nil
147
- object.read_attribute_for_validation(:foo).must_equal nil
149
+ expect(object.read_attribute_for_validation(:foo)).must_be_nil
148
150
  end
149
151
 
150
152
  it 'calls super in the accessor method' do
@@ -170,33 +172,33 @@ describe Enumerize::Base do
170
172
  end
171
173
 
172
174
  object = klass.new
173
- object.foo.must_be_nil
174
- object.attributes.must_equal({})
175
+ expect(object.foo).must_be_nil
176
+ expect(object.attributes).must_equal({})
175
177
 
176
178
  object.foo = 'test'
177
- object.foo.must_equal 'test'
178
- object.attributes.must_equal(:foo => 'test')
179
+ expect(object.foo).must_equal 'test'
180
+ expect(object.attributes).must_equal(:foo => 'test')
179
181
  end
180
182
 
181
183
  it 'stores hash values' do
182
- klass.enumerize(:foo, :in => {:a => 1, :b => 2})
184
+ kklass.enumerize(:foo, :in => {:a => 1, :b => 2})
183
185
 
184
186
  object.foo = :a
185
- object.instance_variable_get(:@foo).must_equal 1
186
- object.foo.must_equal 'a'
187
+ expect(object.instance_variable_get(:@foo)).must_equal 1
188
+ expect(object.foo).must_equal 'a'
187
189
 
188
190
  object.foo = :b
189
- object.instance_variable_get(:@foo).must_equal 2
190
- object.foo.must_equal 'b'
191
+ expect(object.instance_variable_get(:@foo)).must_equal 2
192
+ expect(object.foo).must_equal 'b'
191
193
  end
192
194
 
193
195
  it 'returns custom value' do
194
- klass.enumerize(:foo, :in => {:a => 1, :b => 2})
196
+ kklass.enumerize(:foo, :in => {:a => 1, :b => 2})
195
197
 
196
198
  object.foo = :a
197
- object.foo_value.must_equal 1
199
+ expect(object.foo_value).must_equal 1
198
200
 
199
201
  object.foo = :b
200
- object.foo_value.must_equal 2
202
+ expect(object.foo_value).must_equal 2
201
203
  end
202
204
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_helper'
2
4
 
3
5
  Formtastic::FormBuilder.action_class_finder = Formtastic::ActionClassFinder
@@ -42,6 +44,12 @@ class FormtasticSpec < MiniTest::Spec
42
44
  end
43
45
  end
44
46
 
47
+ class Registration < Struct.new(:sex)
48
+ extend Enumerize
49
+
50
+ enumerize :sex, in: [:male, :female]
51
+ end
52
+
45
53
  before { $VERBOSE = nil }
46
54
  after { $VERBOSE = true }
47
55
 
@@ -124,4 +132,21 @@ class FormtasticSpec < MiniTest::Spec
124
132
 
125
133
  assert_select 'input[type=text]'
126
134
  end
135
+
136
+ it 'renders select with enumerized values for non-ActiveModel object' do
137
+ concat(semantic_form_for(Registration.new, as: 'registration', url: '/') do |f|
138
+ f.input(:sex)
139
+ end)
140
+
141
+ assert_select 'select option[value=male]'
142
+ assert_select 'select option[value=female]'
143
+ end
144
+
145
+ it 'does not affect forms without object' do
146
+ concat(semantic_form_for('') do |f|
147
+ f.input(:name)
148
+ end)
149
+
150
+ assert_select 'input[type=text]'
151
+ end
127
152
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_helper'
2
4
 
3
5
  class ModuleAttributesSpec < MiniTest::Spec
@@ -9,9 +11,9 @@ class ModuleAttributesSpec < MiniTest::Spec
9
11
 
10
12
  klass = Class.new
11
13
  klass.send :include, mod
12
- klass.enumerized_attributes[:sex].must_be_instance_of Enumerize::Attribute
13
- klass.new.sex.must_equal 'male'
14
- klass.sex.must_be_instance_of Enumerize::Attribute
14
+ expect(klass.enumerized_attributes[:sex]).must_be_instance_of Enumerize::Attribute
15
+ expect(klass.new.sex).must_equal 'male'
16
+ expect(klass.sex).must_be_instance_of Enumerize::Attribute
15
17
  end
16
18
 
17
19
  it 'uses new attributes from the module' do
@@ -22,9 +24,9 @@ class ModuleAttributesSpec < MiniTest::Spec
22
24
  klass = Class.new
23
25
  klass.send :include, mod
24
26
  mod.enumerize :sex, :in => %w[male female], :default => 'male'
25
- klass.enumerized_attributes[:sex].must_be_instance_of Enumerize::Attribute
26
- klass.new.sex.must_equal 'male'
27
- klass.sex.must_be_instance_of Enumerize::Attribute
27
+ expect(klass.enumerized_attributes[:sex]).must_be_instance_of Enumerize::Attribute
28
+ expect(klass.new.sex).must_equal 'male'
29
+ expect(klass.sex).must_be_instance_of Enumerize::Attribute
28
30
  end
29
31
 
30
32
  it 'validates attributes' do
@@ -44,7 +46,7 @@ class ModuleAttributesSpec < MiniTest::Spec
44
46
 
45
47
  object = klass.new
46
48
  object.sex = 'wrong'
47
- object.wont_be :valid?
48
- object.errors[:sex].must_include 'is not included in the list'
49
+ expect(object).wont_be :valid?
50
+ expect(object.errors[:sex]).must_include 'is not included in the list'
49
51
  end
50
52
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_helper'
2
4
 
3
5
  begin
@@ -6,8 +8,7 @@ silence_warnings do
6
8
  require 'mongo_mapper'
7
9
  end
8
10
 
9
- MongoMapper.connection = Mongo::Connection.new('localhost', 27017)
10
- MongoMapper.database = 'enumerize-test-suite-of-mongomapper'
11
+ MongoMapper.connection = Mongo::Client.new(['localhost:27017'], database: 'enumerize-test-suite-of-mongomapper')
11
12
 
12
13
  describe Enumerize do
13
14
  class MongoMapperUser
@@ -16,9 +17,11 @@ describe Enumerize do
16
17
 
17
18
  key :sex
18
19
  key :role
20
+ key :foo
19
21
 
20
- enumerize :sex, :in => %w[male female]
22
+ enumerize :sex, :in => %w[male female]
21
23
  enumerize :role, :in => %w[admin user], :default => 'user'
24
+ enumerize :foo, :in => %w[bar baz], :skip_validations => true
22
25
  end
23
26
 
24
27
  before { $VERBOSE = nil }
@@ -29,7 +32,7 @@ describe Enumerize do
29
32
  it 'sets nil if invalid value is passed' do
30
33
  user = model.new
31
34
  user.sex = :invalid
32
- user.sex.must_equal nil
35
+ expect(user.sex).must_be_nil
33
36
  end
34
37
 
35
38
  it 'saves value' do
@@ -37,7 +40,7 @@ describe Enumerize do
37
40
  user = model.new
38
41
  user.sex = :female
39
42
  user.save!
40
- user.sex.must_equal 'female'
43
+ expect(user.sex).must_equal 'female'
41
44
  end
42
45
 
43
46
  it 'loads value' do
@@ -45,19 +48,25 @@ describe Enumerize do
45
48
  model.create!(:sex => :male)
46
49
  store_translations(:en, :enumerize => {:sex => {:male => 'Male'}}) do
47
50
  user = model.first
48
- user.sex.must_equal 'male'
49
- user.sex_text.must_equal 'Male'
51
+ expect(user.sex).must_equal 'male'
52
+ expect(user.sex_text).must_equal 'Male'
50
53
  end
51
54
  end
52
55
 
53
56
  it 'has default value' do
54
- model.new.role.must_equal 'user'
57
+ expect(model.new.role).must_equal 'user'
55
58
  end
56
59
 
57
60
  it 'validates inclusion' do
58
61
  user = model.new
59
62
  user.role = 'wrong'
60
- user.wont_be :valid?
63
+ expect(user).wont_be :valid?
64
+ end
65
+
66
+ it 'does not validate inclusion when :skip_validations option passed' do
67
+ user = model.new
68
+ user.foo = 'wrong'
69
+ expect(user).must_be :valid?
61
70
  end
62
71
 
63
72
  it 'assigns value on loaded record' do
@@ -65,7 +74,7 @@ describe Enumerize do
65
74
  model.create!(:sex => :male)
66
75
  user = model.first
67
76
  user.sex = :female
68
- user.sex.must_equal 'female'
77
+ expect(user.sex).must_equal 'female'
69
78
  end
70
79
  end
71
80