enumerize 2.4.0 → 2.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ruby.yml +73 -0
- data/CHANGELOG.md +7 -0
- data/Gemfile.global +1 -8
- data/Gemfile.rails70 +9 -0
- data/Gemfile.railsmaster +5 -0
- data/README.md +182 -101
- data/lib/enumerize/activerecord.rb +6 -2
- data/lib/enumerize/scope/activerecord.rb +4 -0
- data/lib/enumerize/scope/mongoid.rb +4 -0
- data/lib/enumerize/scope/sequel.rb +4 -0
- data/lib/enumerize/version.rb +1 -1
- data/test/activemodel_test.rb +22 -22
- data/test/activerecord_test.rb +129 -106
- data/test/attribute_map_test.rb +7 -7
- data/test/attribute_test.rb +31 -31
- data/test/base_test.rb +35 -35
- data/test/module_attributes_test.rb +8 -8
- data/test/mongo_mapper_test.rb +8 -8
- data/test/mongoid_test.rb +30 -27
- data/test/multiple_test.rb +8 -8
- data/test/predicates_test.rb +18 -18
- data/test/rails_admin_test.rb +2 -2
- data/test/sequel_test.rb +56 -53
- data/test/set_test.rb +23 -23
- data/test/value_test.rb +28 -35
- metadata +10 -30
- data/.travis.yml +0 -35
data/test/attribute_test.rb
CHANGED
@@ -13,33 +13,33 @@ describe Enumerize::Attribute do
|
|
13
13
|
|
14
14
|
it 'returns values' do
|
15
15
|
build_attr nil, :foo, :in => [:a, :b]
|
16
|
-
attr.values.must_equal %w[a b]
|
16
|
+
expect(attr.values).must_equal %w[a b]
|
17
17
|
end
|
18
18
|
|
19
19
|
it 'returns frozen values' do
|
20
20
|
build_attr nil, :foo, :in => [:a, :b]
|
21
|
-
attr.values.map(&:frozen?).must_equal [true, true]
|
21
|
+
expect(attr.values.map(&:frozen?)).must_equal [true, true]
|
22
22
|
end
|
23
23
|
|
24
24
|
it 'converts name to symbol' do
|
25
25
|
build_attr nil, 'foo', :in => %w[a b]
|
26
|
-
attr.name.must_equal :foo
|
26
|
+
expect(attr.name).must_equal :foo
|
27
27
|
end
|
28
28
|
|
29
29
|
it 'uses custom value class' do
|
30
30
|
value_class = Class.new(Enumerize::Value)
|
31
31
|
build_attr nil, 'foo', :in => %w[a b], :value_class => value_class
|
32
|
-
attr.values.first.must_be_instance_of value_class
|
32
|
+
expect(attr.values.first).must_be_instance_of value_class
|
33
33
|
end
|
34
34
|
|
35
35
|
describe 'i18n scopes' do
|
36
36
|
it 'returns scopes from options' do
|
37
37
|
build_attr nil, 'foo', :in => %w[a b], :i18n_scope => %w[bar buzz]
|
38
|
-
attr.i18n_scopes.must_equal %w[bar buzz]
|
38
|
+
expect(attr.i18n_scopes).must_equal %w[bar buzz]
|
39
39
|
end
|
40
40
|
|
41
41
|
it 'accepts only string scopes' do
|
42
|
-
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
|
43
43
|
end
|
44
44
|
end
|
45
45
|
|
@@ -47,32 +47,32 @@ describe Enumerize::Attribute do
|
|
47
47
|
it 'returns all options for select' do
|
48
48
|
store_translations(:en, :enumerize => {:foo => {:a => 'a text', :b => 'b text'}}) do
|
49
49
|
build_attr nil, :foo, :in => %w[a b]
|
50
|
-
attr.options.must_equal [['a text', 'a'], ['b text', 'b']]
|
50
|
+
expect(attr.options).must_equal [['a text', 'a'], ['b text', 'b']]
|
51
51
|
end
|
52
52
|
end
|
53
53
|
|
54
54
|
it 'returns requested options for select via :only' do
|
55
55
|
store_translations(:en, :enumerize => {:foo => {:a => 'a text', :b => 'b text'}}) do
|
56
56
|
build_attr nil, :foo, :in => %w[a b]
|
57
|
-
attr.options(:only => :a).must_equal [['a text', 'a']]
|
58
|
-
attr.options(:only => [:b]).must_equal [['b text', 'b']]
|
59
|
-
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 []
|
60
60
|
end
|
61
61
|
end
|
62
62
|
|
63
63
|
it 'returns requested options for select via :except' do
|
64
64
|
store_translations(:en, :enumerize => {:foo => {:a => 'a text', :b => 'b text'}}) do
|
65
65
|
build_attr nil, :foo, :in => %w[a b]
|
66
|
-
attr.options(:except => :a).must_equal [['b text', 'b']]
|
67
|
-
attr.options(:except => :b).must_equal [['a text', 'a']]
|
68
|
-
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']]
|
69
69
|
end
|
70
70
|
end
|
71
71
|
|
72
72
|
it 'does not work with both :only and :except' do
|
73
73
|
store_translations(:en, :enumerize => {:foo => {:a => 'a text', :b => 'b text'}}) do
|
74
74
|
build_attr nil, :foo, :in => %w[a b]
|
75
|
-
proc { attr.options(:except => [], :only => []) }.must_raise ArgumentError
|
75
|
+
expect(proc { attr.options(:except => [], :only => []) }).must_raise ArgumentError
|
76
76
|
end
|
77
77
|
end
|
78
78
|
end
|
@@ -83,25 +83,25 @@ describe Enumerize::Attribute do
|
|
83
83
|
end
|
84
84
|
|
85
85
|
it 'returns hash keys as values' do
|
86
|
-
attr.values.must_equal %w[a b]
|
86
|
+
expect(attr.values).must_equal %w[a b]
|
87
87
|
end
|
88
88
|
|
89
89
|
it 'finds values by hash values' do
|
90
|
-
attr.find_value(1).must_equal 'a'
|
91
|
-
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'
|
92
92
|
end
|
93
93
|
end
|
94
94
|
|
95
95
|
it 'sets up shortcut methods for each value' do
|
96
96
|
build_attr nil, :foo, :in => {:a => 1, :b => 2}
|
97
97
|
|
98
|
-
attr.must_respond_to :a
|
99
|
-
attr.must_respond_to :b
|
98
|
+
expect(attr).must_respond_to :a
|
99
|
+
expect(attr).must_respond_to :b
|
100
100
|
|
101
|
-
attr.a.value.must_equal 1
|
102
|
-
attr.b.value.must_equal 2
|
103
|
-
attr.a.text.must_equal 'A'
|
104
|
-
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'
|
105
105
|
end
|
106
106
|
|
107
107
|
describe 'values hash with zero' do
|
@@ -110,17 +110,17 @@ describe Enumerize::Attribute do
|
|
110
110
|
end
|
111
111
|
|
112
112
|
it 'returns hash keys as values' do
|
113
|
-
attr.values.must_equal %w[a b c]
|
113
|
+
expect(attr.values).must_equal %w[a b c]
|
114
114
|
end
|
115
115
|
|
116
116
|
it 'finds values by hash values' do
|
117
|
-
attr.find_value(1).must_equal 'a'
|
118
|
-
attr.find_value(2).must_equal 'b'
|
119
|
-
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'
|
120
120
|
end
|
121
121
|
|
122
122
|
it 'finds all values by hash values' do
|
123
|
-
attr.find_values(1, 2, 0).must_equal ['a', 'b', 'c']
|
123
|
+
expect(attr.find_values(1, 2, 0)).must_equal ['a', 'b', 'c']
|
124
124
|
end
|
125
125
|
end
|
126
126
|
|
@@ -130,12 +130,12 @@ describe Enumerize::Attribute do
|
|
130
130
|
end
|
131
131
|
|
132
132
|
it 'returns hash keys as values' do
|
133
|
-
attr.values.must_equal %w[a b]
|
133
|
+
expect(attr.values).must_equal %w[a b]
|
134
134
|
end
|
135
135
|
|
136
136
|
it 'finds values by hash values' do
|
137
|
-
attr.find_value(true).must_equal 'a'
|
138
|
-
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'
|
139
139
|
end
|
140
140
|
end
|
141
141
|
end
|
data/test/base_test.rb
CHANGED
@@ -17,29 +17,29 @@ describe Enumerize::Base do
|
|
17
17
|
|
18
18
|
it 'returns nil when not set' do
|
19
19
|
kklass.enumerize(:foo, :in => [:a, :b])
|
20
|
-
object.foo.must_be_nil
|
20
|
+
expect(object.foo).must_be_nil
|
21
21
|
end
|
22
22
|
|
23
23
|
it 'returns value that was set' do
|
24
24
|
kklass.enumerize(:foo, :in => [:a, :b])
|
25
25
|
object.foo = :a
|
26
|
-
object.foo.must_equal 'a'
|
26
|
+
expect(object.foo).must_equal 'a'
|
27
27
|
end
|
28
28
|
|
29
29
|
it 'returns translation' do
|
30
30
|
store_translations(:en, :enumerize => {:foo => {:a => 'a text'}}) do
|
31
31
|
kklass.enumerize(:foo, :in => [:a, :b])
|
32
32
|
object.foo = :a
|
33
|
-
object.foo.text.must_equal 'a text'
|
34
|
-
object.foo_text.must_equal 'a text'
|
35
|
-
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'
|
36
36
|
end
|
37
37
|
end
|
38
38
|
|
39
39
|
it 'returns nil as translation when value is nil' do
|
40
40
|
store_translations(:en, :enumerize => {:foo => {:a => 'a text'}}) do
|
41
41
|
kklass.enumerize(:foo, :in => [:a, :b])
|
42
|
-
object.foo_text.must_be_nil
|
42
|
+
expect(object.foo_text).must_be_nil
|
43
43
|
end
|
44
44
|
end
|
45
45
|
|
@@ -56,8 +56,8 @@ describe Enumerize::Base do
|
|
56
56
|
store_translations(:en, :enumerize => {:example_class => {:foo => {:a => 'a text scoped'}}}) do
|
57
57
|
kklass.enumerize(:foo, :in => [:a, :b])
|
58
58
|
object.foo = :a
|
59
|
-
object.foo.text.must_equal 'a text scoped'
|
60
|
-
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'
|
61
61
|
end
|
62
62
|
end
|
63
63
|
|
@@ -65,88 +65,88 @@ describe Enumerize::Base do
|
|
65
65
|
store_translations(:en, :enumerize => {}) do
|
66
66
|
kklass.enumerize(:foo, :in => [:a, :b])
|
67
67
|
object.foo = :a
|
68
|
-
object.foo_text.must_equal 'A'
|
68
|
+
expect(object.foo_text).must_equal 'A'
|
69
69
|
end
|
70
70
|
end
|
71
71
|
|
72
72
|
it 'stores value as string' do
|
73
73
|
kklass.enumerize(:foo, :in => [:a, :b])
|
74
74
|
object.foo = :a
|
75
|
-
object.instance_variable_get(:@foo).must_be_instance_of String
|
75
|
+
expect(object.instance_variable_get(:@foo)).must_be_instance_of String
|
76
76
|
end
|
77
77
|
|
78
78
|
it 'handles default value' do
|
79
79
|
kklass.enumerize(:foo, :in => [:a, :b], :default => :b)
|
80
|
-
object.foo.must_equal 'b'
|
80
|
+
expect(object.foo).must_equal 'b'
|
81
81
|
end
|
82
82
|
|
83
83
|
it 'handles default value with lambda' do
|
84
84
|
kklass.enumerize(:foo, :in => [:a, :b], :default => lambda { :b })
|
85
|
-
object.foo.must_equal 'b'
|
85
|
+
expect(object.foo).must_equal 'b'
|
86
86
|
end
|
87
87
|
|
88
88
|
it 'injects object instance into lamda default value' do
|
89
89
|
kklass.enumerize(:foo, :in => [:a, :b], :default => lambda { |obj| :b if obj.is_a? kklass })
|
90
|
-
object.foo.must_equal 'b'
|
90
|
+
expect(object.foo).must_equal 'b'
|
91
91
|
end
|
92
92
|
|
93
93
|
it 'raises exception on invalid default value' do
|
94
|
-
proc {
|
94
|
+
expect(proc {
|
95
95
|
kklass.enumerize(:foo, :in => [:a, :b], :default => :c)
|
96
|
-
}.must_raise ArgumentError
|
96
|
+
}).must_raise ArgumentError
|
97
97
|
end
|
98
98
|
|
99
99
|
it 'has enumerized attributes' do
|
100
|
-
kklass.enumerized_attributes.must_be_empty
|
100
|
+
expect(kklass.enumerized_attributes).must_be_empty
|
101
101
|
kklass.enumerize(:foo, :in => %w[a b])
|
102
|
-
kklass.enumerized_attributes[:foo].must_be_instance_of Enumerize::Attribute
|
102
|
+
expect(kklass.enumerized_attributes[:foo]).must_be_instance_of Enumerize::Attribute
|
103
103
|
end
|
104
104
|
|
105
105
|
it "doesn't override existing method" do
|
106
106
|
method = kklass.method(:name)
|
107
107
|
kklass.enumerize(:name, :in => %w[a b], :default => 'a')
|
108
|
-
kklass.method(:name).must_equal method
|
108
|
+
expect(kklass.method(:name)).must_equal method
|
109
109
|
end
|
110
110
|
|
111
111
|
it "inherits enumerized attributes from a parent class" do
|
112
112
|
kklass.enumerize(:foo, :in => %w[a b])
|
113
|
-
subklass.enumerized_attributes[:foo].must_be_instance_of Enumerize::Attribute
|
113
|
+
expect(subklass.enumerized_attributes[:foo]).must_be_instance_of Enumerize::Attribute
|
114
114
|
end
|
115
115
|
|
116
116
|
it "inherits enumerized attributes from a grandparent class" do
|
117
117
|
kklass.enumerize(:foo, :in => %w[a b])
|
118
|
-
Class.new(subklass).enumerized_attributes[:foo].must_be_instance_of Enumerize::Attribute
|
118
|
+
expect(Class.new(subklass).enumerized_attributes[:foo]).must_be_instance_of Enumerize::Attribute
|
119
119
|
end
|
120
120
|
|
121
121
|
it "doesn't add enumerized attributes to parent class" do
|
122
122
|
kklass.enumerize(:foo, :in => %w[a b])
|
123
123
|
subklass.enumerize(:bar, :in => %w[c d])
|
124
124
|
|
125
|
-
kklass.enumerized_attributes[:bar].must_be_nil
|
125
|
+
expect(kklass.enumerized_attributes[:bar]).must_be_nil
|
126
126
|
end
|
127
127
|
|
128
128
|
it 'adds new parent class attributes to subclass' do
|
129
129
|
subklass = Class.new(kklass)
|
130
130
|
kklass.enumerize :foo, :in => %w[a b]
|
131
|
-
subklass.enumerized_attributes[:foo].must_be_instance_of Enumerize::Attribute
|
131
|
+
expect(subklass.enumerized_attributes[:foo]).must_be_instance_of Enumerize::Attribute
|
132
132
|
end
|
133
133
|
|
134
134
|
it 'stores nil value' do
|
135
135
|
kklass.enumerize(:foo, :in => [:a, :b])
|
136
136
|
object.foo = nil
|
137
|
-
object.instance_variable_get(:@foo).must_be_nil
|
137
|
+
expect(object.instance_variable_get(:@foo)).must_be_nil
|
138
138
|
end
|
139
139
|
|
140
140
|
it 'casts value to string for validation' do
|
141
141
|
kklass.enumerize(:foo, :in => [:a, :b])
|
142
142
|
object.foo = :c
|
143
|
-
object.read_attribute_for_validation(:foo).must_equal 'c'
|
143
|
+
expect(object.read_attribute_for_validation(:foo)).must_equal 'c'
|
144
144
|
end
|
145
145
|
|
146
146
|
it "doesn't cast nil to string for validation" do
|
147
147
|
kklass.enumerize(:foo, :in => [:a, :b])
|
148
148
|
object.foo = nil
|
149
|
-
object.read_attribute_for_validation(:foo).must_be_nil
|
149
|
+
expect(object.read_attribute_for_validation(:foo)).must_be_nil
|
150
150
|
end
|
151
151
|
|
152
152
|
it 'calls super in the accessor method' do
|
@@ -172,33 +172,33 @@ describe Enumerize::Base do
|
|
172
172
|
end
|
173
173
|
|
174
174
|
object = klass.new
|
175
|
-
object.foo.must_be_nil
|
176
|
-
object.attributes.must_equal({})
|
175
|
+
expect(object.foo).must_be_nil
|
176
|
+
expect(object.attributes).must_equal({})
|
177
177
|
|
178
178
|
object.foo = 'test'
|
179
|
-
object.foo.must_equal 'test'
|
180
|
-
object.attributes.must_equal(:foo => 'test')
|
179
|
+
expect(object.foo).must_equal 'test'
|
180
|
+
expect(object.attributes).must_equal(:foo => 'test')
|
181
181
|
end
|
182
182
|
|
183
183
|
it 'stores hash values' do
|
184
184
|
kklass.enumerize(:foo, :in => {:a => 1, :b => 2})
|
185
185
|
|
186
186
|
object.foo = :a
|
187
|
-
object.instance_variable_get(:@foo).must_equal 1
|
188
|
-
object.foo.must_equal 'a'
|
187
|
+
expect(object.instance_variable_get(:@foo)).must_equal 1
|
188
|
+
expect(object.foo).must_equal 'a'
|
189
189
|
|
190
190
|
object.foo = :b
|
191
|
-
object.instance_variable_get(:@foo).must_equal 2
|
192
|
-
object.foo.must_equal 'b'
|
191
|
+
expect(object.instance_variable_get(:@foo)).must_equal 2
|
192
|
+
expect(object.foo).must_equal 'b'
|
193
193
|
end
|
194
194
|
|
195
195
|
it 'returns custom value' do
|
196
196
|
kklass.enumerize(:foo, :in => {:a => 1, :b => 2})
|
197
197
|
|
198
198
|
object.foo = :a
|
199
|
-
object.foo_value.must_equal 1
|
199
|
+
expect(object.foo_value).must_equal 1
|
200
200
|
|
201
201
|
object.foo = :b
|
202
|
-
object.foo_value.must_equal 2
|
202
|
+
expect(object.foo_value).must_equal 2
|
203
203
|
end
|
204
204
|
end
|
@@ -11,9 +11,9 @@ class ModuleAttributesSpec < MiniTest::Spec
|
|
11
11
|
|
12
12
|
klass = Class.new
|
13
13
|
klass.send :include, mod
|
14
|
-
klass.enumerized_attributes[:sex].must_be_instance_of Enumerize::Attribute
|
15
|
-
klass.new.sex.must_equal 'male'
|
16
|
-
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
|
17
17
|
end
|
18
18
|
|
19
19
|
it 'uses new attributes from the module' do
|
@@ -24,9 +24,9 @@ class ModuleAttributesSpec < MiniTest::Spec
|
|
24
24
|
klass = Class.new
|
25
25
|
klass.send :include, mod
|
26
26
|
mod.enumerize :sex, :in => %w[male female], :default => 'male'
|
27
|
-
klass.enumerized_attributes[:sex].must_be_instance_of Enumerize::Attribute
|
28
|
-
klass.new.sex.must_equal 'male'
|
29
|
-
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
|
30
30
|
end
|
31
31
|
|
32
32
|
it 'validates attributes' do
|
@@ -46,7 +46,7 @@ class ModuleAttributesSpec < MiniTest::Spec
|
|
46
46
|
|
47
47
|
object = klass.new
|
48
48
|
object.sex = 'wrong'
|
49
|
-
object.wont_be :valid?
|
50
|
-
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'
|
51
51
|
end
|
52
52
|
end
|
data/test/mongo_mapper_test.rb
CHANGED
@@ -32,7 +32,7 @@ describe Enumerize do
|
|
32
32
|
it 'sets nil if invalid value is passed' do
|
33
33
|
user = model.new
|
34
34
|
user.sex = :invalid
|
35
|
-
user.sex.must_be_nil
|
35
|
+
expect(user.sex).must_be_nil
|
36
36
|
end
|
37
37
|
|
38
38
|
it 'saves value' do
|
@@ -40,7 +40,7 @@ describe Enumerize do
|
|
40
40
|
user = model.new
|
41
41
|
user.sex = :female
|
42
42
|
user.save!
|
43
|
-
user.sex.must_equal 'female'
|
43
|
+
expect(user.sex).must_equal 'female'
|
44
44
|
end
|
45
45
|
|
46
46
|
it 'loads value' do
|
@@ -48,25 +48,25 @@ describe Enumerize do
|
|
48
48
|
model.create!(:sex => :male)
|
49
49
|
store_translations(:en, :enumerize => {:sex => {:male => 'Male'}}) do
|
50
50
|
user = model.first
|
51
|
-
user.sex.must_equal 'male'
|
52
|
-
user.sex_text.must_equal 'Male'
|
51
|
+
expect(user.sex).must_equal 'male'
|
52
|
+
expect(user.sex_text).must_equal 'Male'
|
53
53
|
end
|
54
54
|
end
|
55
55
|
|
56
56
|
it 'has default value' do
|
57
|
-
model.new.role.must_equal 'user'
|
57
|
+
expect(model.new.role).must_equal 'user'
|
58
58
|
end
|
59
59
|
|
60
60
|
it 'validates inclusion' do
|
61
61
|
user = model.new
|
62
62
|
user.role = 'wrong'
|
63
|
-
user.wont_be :valid?
|
63
|
+
expect(user).wont_be :valid?
|
64
64
|
end
|
65
65
|
|
66
66
|
it 'does not validate inclusion when :skip_validations option passed' do
|
67
67
|
user = model.new
|
68
68
|
user.foo = 'wrong'
|
69
|
-
user.must_be :valid?
|
69
|
+
expect(user).must_be :valid?
|
70
70
|
end
|
71
71
|
|
72
72
|
it 'assigns value on loaded record' do
|
@@ -74,7 +74,7 @@ describe Enumerize do
|
|
74
74
|
model.create!(:sex => :male)
|
75
75
|
user = model.first
|
76
76
|
user.sex = :female
|
77
|
-
user.sex.must_equal 'female'
|
77
|
+
expect(user.sex).must_equal 'female'
|
78
78
|
end
|
79
79
|
end
|
80
80
|
|
data/test/mongoid_test.rb
CHANGED
@@ -40,7 +40,7 @@ describe Enumerize do
|
|
40
40
|
it 'sets nil if invalid value is passed' do
|
41
41
|
user = model.new
|
42
42
|
user.sex = :invalid
|
43
|
-
user.sex.must_be_nil
|
43
|
+
expect(user.sex).must_be_nil
|
44
44
|
end
|
45
45
|
|
46
46
|
it 'saves value' do
|
@@ -48,7 +48,7 @@ describe Enumerize do
|
|
48
48
|
user = model.new
|
49
49
|
user.sex = :female
|
50
50
|
user.save!
|
51
|
-
user.sex.must_equal 'female'
|
51
|
+
expect(user.sex).must_equal 'female'
|
52
52
|
end
|
53
53
|
|
54
54
|
it 'loads value' do
|
@@ -56,13 +56,13 @@ describe Enumerize do
|
|
56
56
|
model.create!(:sex => :male)
|
57
57
|
store_translations(:en, :enumerize => {:sex => {:male => 'Male'}}) do
|
58
58
|
user = model.first
|
59
|
-
user.sex.must_equal 'male'
|
60
|
-
user.sex_text.must_equal 'Male'
|
59
|
+
expect(user.sex).must_equal 'male'
|
60
|
+
expect(user.sex_text).must_equal 'Male'
|
61
61
|
end
|
62
62
|
end
|
63
63
|
|
64
64
|
it 'has default value' do
|
65
|
-
model.new.role.must_equal 'user'
|
65
|
+
expect(model.new.role).must_equal 'user'
|
66
66
|
end
|
67
67
|
|
68
68
|
it 'uses after_initialize callback to set default value' do
|
@@ -70,7 +70,7 @@ describe Enumerize do
|
|
70
70
|
model.create!(sex: 'male', role: nil)
|
71
71
|
|
72
72
|
user = model.where(sex: 'male').first
|
73
|
-
user.role.must_equal 'user'
|
73
|
+
expect(user.role).must_equal 'user'
|
74
74
|
end
|
75
75
|
|
76
76
|
it 'does not set default value for not selected attributes' do
|
@@ -83,21 +83,21 @@ describe Enumerize do
|
|
83
83
|
it 'validates inclusion' do
|
84
84
|
user = model.new
|
85
85
|
user.role = 'wrong'
|
86
|
-
user.wont_be :valid?
|
86
|
+
expect(user).wont_be :valid?
|
87
87
|
end
|
88
88
|
|
89
89
|
it 'does not validate inclusion when :skip_validations option passed' do
|
90
90
|
user = model.new
|
91
91
|
user.foo = 'wrong'
|
92
|
-
user.must_be :valid?
|
92
|
+
expect(user).must_be :valid?
|
93
93
|
end
|
94
94
|
|
95
95
|
it 'sets value to enumerized field from db when record is reloaded' do
|
96
96
|
user = model.create!(mult: [:one])
|
97
97
|
model.find(user.id).update(mult: %i[two three])
|
98
|
-
user.mult.must_equal %w[one]
|
98
|
+
expect(user.mult).must_equal %w[one]
|
99
99
|
user.reload
|
100
|
-
user.mult.must_equal %w[two three]
|
100
|
+
expect(user.mult).must_equal %w[two three]
|
101
101
|
end
|
102
102
|
|
103
103
|
it 'assigns value on loaded record' do
|
@@ -105,7 +105,7 @@ describe Enumerize do
|
|
105
105
|
model.create!(:sex => :male)
|
106
106
|
user = model.first
|
107
107
|
user.sex = :female
|
108
|
-
user.sex.must_equal 'female'
|
108
|
+
expect(user.sex).must_equal 'female'
|
109
109
|
end
|
110
110
|
|
111
111
|
it 'loads multiple properly' do
|
@@ -113,29 +113,32 @@ describe Enumerize do
|
|
113
113
|
|
114
114
|
model.create!(:mult => ['one', 'two'])
|
115
115
|
user = model.first
|
116
|
-
user.mult.to_a.must_equal ['one', 'two']
|
116
|
+
expect(user.mult.to_a).must_equal ['one', 'two']
|
117
117
|
end
|
118
118
|
|
119
119
|
it 'adds scope' do
|
120
120
|
model.delete_all
|
121
121
|
|
122
|
-
user_1 = model.create!(sex: :male, role: :admin)
|
123
|
-
user_2 = model.create!(sex: :female, role: :user)
|
122
|
+
user_1 = model.create!(sex: :male, skill: :noob, role: :admin, account_type: :basic)
|
123
|
+
user_2 = model.create!(sex: :female, skill: :noob, role: :user, account_type: :basic)
|
124
124
|
user_3 = model.create!(skill: :pro, account_type: :premium)
|
125
125
|
|
126
|
-
model.with_sex(:male).to_a.must_equal [user_1]
|
127
|
-
model.with_sex(:female).to_a.must_equal [user_2]
|
128
|
-
model.with_sex(:male, :female).to_set.must_equal [user_1, user_2].to_set
|
126
|
+
expect(model.with_sex(:male).to_a).must_equal [user_1]
|
127
|
+
expect(model.with_sex(:female).to_a).must_equal [user_2]
|
128
|
+
expect(model.with_sex(:male, :female).to_set).must_equal [user_1, user_2].to_set
|
129
129
|
|
130
|
-
model.without_sex(:male).to_set.must_equal [user_2, user_3].to_set
|
131
|
-
model.without_sex(:female).to_set.must_equal [user_1, user_3].to_set
|
132
|
-
model.without_sex(:male, :female).to_a.must_equal [user_3]
|
130
|
+
expect(model.without_sex(:male).to_set).must_equal [user_2, user_3].to_set
|
131
|
+
expect(model.without_sex(:female).to_set).must_equal [user_1, user_3].to_set
|
132
|
+
expect(model.without_sex(:male, :female).to_a).must_equal [user_3]
|
133
133
|
|
134
|
-
model.having_role(:admin).to_a.must_equal [user_1]
|
135
|
-
model.having_role(:user).to_a.must_equal [user_2, user_3]
|
134
|
+
expect(model.having_role(:admin).to_a).must_equal [user_1]
|
135
|
+
expect(model.having_role(:user).to_a).must_equal [user_2, user_3]
|
136
136
|
|
137
|
-
model.pro.to_a.must_equal [user_3]
|
138
|
-
model.premium.to_a.must_equal [user_3]
|
137
|
+
expect(model.pro.to_a).must_equal [user_3]
|
138
|
+
expect(model.premium.to_a).must_equal [user_3]
|
139
|
+
|
140
|
+
expect(model.not_pro.to_set).must_equal [user_1, user_2].to_set
|
141
|
+
expect(model.not_premium.to_set).must_equal [user_1, user_2].to_set
|
139
142
|
end
|
140
143
|
|
141
144
|
it 'chains scopes' do
|
@@ -145,14 +148,14 @@ describe Enumerize do
|
|
145
148
|
user_2 = model.create!(status: :warning)
|
146
149
|
user_3 = model.create!(status: :error)
|
147
150
|
|
148
|
-
model.with_status(:notice, :warning).with_status(:notice, :error).to_a.must_equal [user_1]
|
149
|
-
model.with_status(:notice, :warning).union.with_status(:notice, :error).to_a.must_equal [user_1, user_2, user_3]
|
151
|
+
expect(model.with_status(:notice, :warning).with_status(:notice, :error).to_a).must_equal [user_1]
|
152
|
+
expect(model.with_status(:notice, :warning).union.with_status(:notice, :error).to_a).must_equal [user_1, user_2, user_3]
|
150
153
|
end
|
151
154
|
|
152
155
|
it 'ignores not enumerized values that passed to the scope method' do
|
153
156
|
model.delete_all
|
154
157
|
|
155
|
-
model.with_sex(:foo).must_equal []
|
158
|
+
expect(model.with_sex(:foo)).must_equal []
|
156
159
|
end
|
157
160
|
end
|
158
161
|
|
data/test/multiple_test.rb
CHANGED
@@ -17,38 +17,38 @@ describe Enumerize::Base do
|
|
17
17
|
|
18
18
|
it 'returns [] when not set' do
|
19
19
|
kklass.enumerize :foos, in: %w(a b), multiple: true
|
20
|
-
object.foos.must_equal []
|
20
|
+
expect(object.foos).must_equal []
|
21
21
|
end
|
22
22
|
|
23
23
|
it 'returns setted array' do
|
24
24
|
kklass.enumerize :foos, in: %w(a b c), multiple: true
|
25
25
|
object.foos = %w(a c)
|
26
|
-
object.foos.must_equal %w(a c)
|
26
|
+
expect(object.foos).must_equal %w(a c)
|
27
27
|
end
|
28
28
|
|
29
29
|
it 'sets default value as single value' do
|
30
30
|
kklass.enumerize :foos, in: %w(a b c), default: 'b', multiple: true
|
31
|
-
object.foos.must_equal %w(b)
|
31
|
+
expect(object.foos).must_equal %w(b)
|
32
32
|
end
|
33
33
|
|
34
34
|
it 'sets default value as array of one element' do
|
35
35
|
kklass.enumerize :foos, in: %w(a b c), default: %w(b), multiple: true
|
36
|
-
object.foos.must_equal %w(b)
|
36
|
+
expect(object.foos).must_equal %w(b)
|
37
37
|
end
|
38
38
|
|
39
39
|
it 'sets default value as array of several elements' do
|
40
40
|
kklass.enumerize :foos, in: %w(a b c), default: %w(b c), multiple: true
|
41
|
-
object.foos.must_equal %w(b c)
|
41
|
+
expect(object.foos).must_equal %w(b c)
|
42
42
|
end
|
43
43
|
|
44
44
|
it "doesn't define _text method" do
|
45
45
|
kklass.enumerize :foos, in: %w(a b c), multiple: true
|
46
|
-
object.wont_respond_to :foos_text
|
46
|
+
expect(object).wont_respond_to :foos_text
|
47
47
|
end
|
48
48
|
|
49
49
|
it "doesn't define _value method" do
|
50
50
|
kklass.enumerize :foos, in: %w(a b c), multiple: true
|
51
|
-
object.wont_respond_to :foos_value
|
51
|
+
expect(object).wont_respond_to :foos_value
|
52
52
|
end
|
53
53
|
|
54
54
|
it "cannot define multiple with scope" do
|
@@ -60,6 +60,6 @@ describe Enumerize::Base do
|
|
60
60
|
it 'assign a name with the first letter capitalized' do
|
61
61
|
kklass.enumerize :Foos, in: %w(a b c), multiple: true
|
62
62
|
object.Foos = %w(a c)
|
63
|
-
object.Foos.must_equal %w(a c)
|
63
|
+
expect(object.Foos).must_equal %w(a c)
|
64
64
|
end
|
65
65
|
end
|