enumerize 0.8.0 → 2.6.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.github/workflows/ruby.yml +73 -0
- data/.gitignore +2 -0
- data/.rspec +2 -0
- data/CHANGELOG.md +210 -0
- data/Gemfile +4 -21
- data/Gemfile.global +12 -0
- data/Gemfile.mongo_mapper +6 -0
- data/Gemfile.rails60 +6 -0
- data/Gemfile.rails61 +6 -0
- data/Gemfile.rails70 +9 -0
- data/Gemfile.railsmaster +5 -0
- data/README.md +366 -73
- data/Rakefile +4 -4
- data/enumerize.gemspec +2 -1
- data/lib/enumerize/activemodel.rb +47 -0
- data/lib/enumerize/activerecord.rb +102 -27
- data/lib/enumerize/attribute.rb +59 -15
- data/lib/enumerize/attribute_map.rb +2 -0
- data/lib/enumerize/base.rb +32 -17
- data/lib/enumerize/hooks/formtastic.rb +7 -9
- data/lib/enumerize/hooks/sequel_dataset.rb +17 -0
- data/lib/enumerize/hooks/simple_form.rb +9 -12
- data/lib/enumerize/hooks/uniqueness.rb +7 -8
- data/lib/enumerize/integrations/rails_admin.rb +3 -1
- data/lib/enumerize/integrations/rspec/matcher.rb +112 -26
- data/lib/enumerize/integrations/rspec.rb +3 -0
- data/lib/enumerize/module.rb +2 -0
- data/lib/enumerize/module_attributes.rb +2 -0
- data/lib/enumerize/mongoid.rb +36 -0
- data/lib/enumerize/predicatable.rb +10 -17
- data/lib/enumerize/predicates.rb +5 -1
- data/lib/enumerize/scope/activerecord.rb +53 -0
- data/lib/enumerize/scope/mongoid.rb +50 -0
- data/lib/enumerize/scope/sequel.rb +56 -0
- data/lib/enumerize/sequel.rb +62 -0
- data/lib/enumerize/set.rb +20 -8
- data/lib/enumerize/utils.rb +12 -0
- data/lib/enumerize/value.rb +20 -20
- data/lib/enumerize/version.rb +3 -1
- data/lib/enumerize.rb +33 -2
- data/lib/sequel/plugins/enumerize.rb +18 -0
- data/spec/enumerize/integrations/rspec/matcher_spec.rb +261 -0
- data/spec/spec_helper.rb +30 -0
- data/test/activemodel_test.rb +114 -0
- data/test/activerecord_test.rb +434 -58
- data/test/attribute_map_test.rb +9 -7
- data/test/attribute_test.rb +52 -23
- data/test/base_test.rb +118 -66
- data/test/formtastic_test.rb +28 -12
- data/test/module_attributes_test.rb +10 -8
- data/test/mongo_mapper_test.rb +26 -11
- data/test/mongoid_test.rb +100 -15
- data/test/multiple_test.rb +41 -12
- data/test/predicates_test.rb +34 -26
- data/test/rails_admin_test.rb +8 -6
- data/test/sequel_test.rb +342 -0
- data/test/set_test.rb +42 -26
- data/test/simple_form_test.rb +25 -1
- data/test/support/mock_controller.rb +6 -0
- data/test/support/shared_enums.rb +43 -0
- data/test/support/view_test_helper.rb +18 -1
- data/test/test_helper.rb +33 -2
- data/test/value_test.rb +79 -28
- metadata +51 -12
- data/.travis.yml +0 -19
- data/Gemfile.rails4 +0 -23
- data/lib/enumerize/form_helper.rb +0 -23
- data/test/rspec_matcher_test.rb +0 -76
- data/test/rspec_spec.rb +0 -13
data/test/attribute_map_test.rb
CHANGED
@@ -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
|
data/test/attribute_test.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'test_helper'
|
2
4
|
|
3
|
-
|
5
|
+
class AttributeTest < MiniTest::Spec
|
4
6
|
def attr
|
5
7
|
@attr ||= nil
|
6
8
|
end
|
@@ -11,22 +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
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'uses custom value class' do
|
30
|
+
value_class = Class.new(Enumerize::Value)
|
31
|
+
build_attr nil, 'foo', :in => %w[a b], :value_class => value_class
|
32
|
+
expect(attr.values.first).must_be_instance_of value_class
|
20
33
|
end
|
21
34
|
|
22
35
|
describe 'i18n scopes' do
|
23
36
|
it 'returns scopes from options' do
|
24
37
|
build_attr nil, 'foo', :in => %w[a b], :i18n_scope => %w[bar buzz]
|
25
|
-
attr.i18n_scopes.must_equal %w[bar buzz]
|
38
|
+
expect(attr.i18n_scopes).must_equal %w[bar buzz]
|
26
39
|
end
|
27
40
|
|
28
41
|
it 'accepts only string scopes' do
|
29
|
-
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
|
30
43
|
end
|
31
44
|
end
|
32
45
|
|
@@ -34,32 +47,32 @@ describe Enumerize::Attribute do
|
|
34
47
|
it 'returns all options for select' do
|
35
48
|
store_translations(:en, :enumerize => {:foo => {:a => 'a text', :b => 'b text'}}) do
|
36
49
|
build_attr nil, :foo, :in => %w[a b]
|
37
|
-
attr.options.must_equal [['a text', 'a'], ['b text', 'b']]
|
50
|
+
expect(attr.options).must_equal [['a text', 'a'], ['b text', 'b']]
|
38
51
|
end
|
39
52
|
end
|
40
53
|
|
41
54
|
it 'returns requested options for select via :only' do
|
42
55
|
store_translations(:en, :enumerize => {:foo => {:a => 'a text', :b => 'b text'}}) do
|
43
56
|
build_attr nil, :foo, :in => %w[a b]
|
44
|
-
attr.options(:only => :a).must_equal [['a text', 'a']]
|
45
|
-
attr.options(:only => [:b]).must_equal [['b text', 'b']]
|
46
|
-
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 []
|
47
60
|
end
|
48
61
|
end
|
49
62
|
|
50
63
|
it 'returns requested options for select via :except' do
|
51
64
|
store_translations(:en, :enumerize => {:foo => {:a => 'a text', :b => 'b text'}}) do
|
52
65
|
build_attr nil, :foo, :in => %w[a b]
|
53
|
-
attr.options(:except => :a).must_equal [['b text', 'b']]
|
54
|
-
attr.options(:except => :b).must_equal [['a text', 'a']]
|
55
|
-
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']]
|
56
69
|
end
|
57
70
|
end
|
58
71
|
|
59
72
|
it 'does not work with both :only and :except' do
|
60
73
|
store_translations(:en, :enumerize => {:foo => {:a => 'a text', :b => 'b text'}}) do
|
61
74
|
build_attr nil, :foo, :in => %w[a b]
|
62
|
-
proc { attr.options(:except => [], :only => []) }.must_raise ArgumentError
|
75
|
+
expect(proc { attr.options(:except => [], :only => []) }).must_raise ArgumentError
|
63
76
|
end
|
64
77
|
end
|
65
78
|
end
|
@@ -70,28 +83,44 @@ describe Enumerize::Attribute do
|
|
70
83
|
end
|
71
84
|
|
72
85
|
it 'returns hash keys as values' do
|
73
|
-
attr.values.must_equal %w[a b]
|
86
|
+
expect(attr.values).must_equal %w[a b]
|
74
87
|
end
|
75
88
|
|
76
89
|
it 'finds values by hash values' do
|
77
|
-
attr.find_value(1).must_equal 'a'
|
78
|
-
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'
|
79
92
|
end
|
80
93
|
end
|
81
94
|
|
95
|
+
it 'sets up shortcut methods for each value' do
|
96
|
+
build_attr nil, :foo, :in => {:a => 1, :b => 2}
|
97
|
+
|
98
|
+
expect(attr).must_respond_to :a
|
99
|
+
expect(attr).must_respond_to :b
|
100
|
+
|
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
|
+
end
|
106
|
+
|
82
107
|
describe 'values hash with zero' do
|
83
108
|
before do
|
84
109
|
build_attr nil, :foo, :in => {:a => 1, :b => 2, :c => 0}
|
85
110
|
end
|
86
111
|
|
87
112
|
it 'returns hash keys as values' do
|
88
|
-
attr.values.must_equal %w[a b c]
|
113
|
+
expect(attr.values).must_equal %w[a b c]
|
89
114
|
end
|
90
115
|
|
91
116
|
it 'finds values by hash values' do
|
92
|
-
attr.find_value(1).must_equal 'a'
|
93
|
-
attr.find_value(2).must_equal 'b'
|
94
|
-
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
|
+
end
|
121
|
+
|
122
|
+
it 'finds all values by hash values' do
|
123
|
+
expect(attr.find_values(1, 2, 0)).must_equal ['a', 'b', 'c']
|
95
124
|
end
|
96
125
|
end
|
97
126
|
|
@@ -101,12 +130,12 @@ describe Enumerize::Attribute do
|
|
101
130
|
end
|
102
131
|
|
103
132
|
it 'returns hash keys as values' do
|
104
|
-
attr.values.must_equal %w[a b]
|
133
|
+
expect(attr.values).must_equal %w[a b]
|
105
134
|
end
|
106
135
|
|
107
136
|
it 'finds values by hash values' do
|
108
|
-
attr.find_value(true).must_equal 'a'
|
109
|
-
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'
|
110
139
|
end
|
111
140
|
end
|
112
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
|
-
|
4
|
-
let(:
|
5
|
+
class BaseTest < MiniTest::Spec
|
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(
|
13
|
+
Class.new(kklass)
|
12
14
|
end
|
13
15
|
|
14
|
-
let(:object) {
|
16
|
+
let(:object) { kklass.new }
|
15
17
|
|
16
18
|
it 'returns nil when not set' do
|
17
|
-
|
18
|
-
object.foo.
|
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
|
-
|
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
|
-
|
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
|
-
|
40
|
-
object.foo_text.
|
41
|
+
kklass.enumerize(:foo, :in => [:a, :b])
|
42
|
+
expect(object.foo_text).must_be_nil
|
41
43
|
end
|
42
44
|
end
|
43
45
|
|
44
|
-
it 'scopes translation by
|
45
|
-
def
|
46
|
-
name = "ExampleClass"
|
46
|
+
it 'scopes translation by i18n key' do
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
99
|
-
|
100
|
-
|
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 =
|
105
|
-
|
106
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
122
|
+
kklass.enumerize(:foo, :in => %w[a b])
|
121
123
|
subklass.enumerize(:bar, :in => %w[c d])
|
122
124
|
|
123
|
-
|
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(
|
128
|
-
|
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
|
-
|
135
|
+
kklass.enumerize(:foo, :in => [:a, :b])
|
134
136
|
object.foo = nil
|
135
|
-
object.instance_variable_get(:@foo).
|
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
|
-
|
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
|
-
|
147
|
+
kklass.enumerize(:foo, :in => [:a, :b])
|
146
148
|
object.foo = nil
|
147
|
-
object.read_attribute_for_validation(:foo).
|
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,83 @@ 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
|
-
|
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
|
-
|
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
|
203
|
+
end
|
204
|
+
|
205
|
+
it 'allows initialize method with arguments' do
|
206
|
+
klass = Class.new do
|
207
|
+
extend Enumerize
|
208
|
+
|
209
|
+
def initialize(argument, key_word_argument: nil); end
|
210
|
+
end
|
211
|
+
|
212
|
+
klass.new('arg1', key_word_argument: 'kwargs1')
|
213
|
+
end
|
214
|
+
|
215
|
+
it 'allows initialize method with arguments for inherited classes' do
|
216
|
+
parent_klass = Class.new do
|
217
|
+
def initialize(argument, key_word_argument: nil); end
|
218
|
+
end
|
219
|
+
|
220
|
+
klass = Class.new(parent_klass) do
|
221
|
+
extend Enumerize
|
222
|
+
|
223
|
+
def initialize(argument, key_word_argument: nil)
|
224
|
+
super
|
225
|
+
end
|
226
|
+
end
|
227
|
+
|
228
|
+
klass.new('arg1', key_word_argument: 'kwargs1')
|
229
|
+
end
|
230
|
+
|
231
|
+
it 'allows initializing object without keyword arguments' do
|
232
|
+
parent_klass = Class.new do
|
233
|
+
attr_reader :arguments
|
234
|
+
|
235
|
+
def initialize(arguments)
|
236
|
+
@arguments = arguments
|
237
|
+
end
|
238
|
+
end
|
239
|
+
|
240
|
+
klass = Class.new(parent_klass) do
|
241
|
+
extend Enumerize
|
242
|
+
|
243
|
+
def initialize(arguments)
|
244
|
+
super
|
245
|
+
end
|
246
|
+
end
|
247
|
+
|
248
|
+
params = { 'string_key' => 1, symbol_key: 2 }
|
249
|
+
|
250
|
+
object = klass.new(params)
|
251
|
+
|
252
|
+
expect(object.arguments).must_equal params
|
201
253
|
end
|
202
254
|
end
|
data/test/formtastic_test.rb
CHANGED
@@ -1,16 +1,9 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'test_helper'
|
2
|
-
|
3
|
-
|
4
|
-
|
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
|
4
|
+
|
5
|
+
Formtastic::FormBuilder.action_class_finder = Formtastic::ActionClassFinder
|
6
|
+
Formtastic::FormBuilder.input_class_finder = Formtastic::InputClassFinder
|
14
7
|
|
15
8
|
class FormtasticSpec < MiniTest::Spec
|
16
9
|
include ViewTestHelper
|
@@ -51,6 +44,12 @@ class FormtasticSpec < MiniTest::Spec
|
|
51
44
|
end
|
52
45
|
end
|
53
46
|
|
47
|
+
class Registration < Struct.new(:sex)
|
48
|
+
extend Enumerize
|
49
|
+
|
50
|
+
enumerize :sex, in: [:male, :female]
|
51
|
+
end
|
52
|
+
|
54
53
|
before { $VERBOSE = nil }
|
55
54
|
after { $VERBOSE = true }
|
56
55
|
|
@@ -133,4 +132,21 @@ class FormtasticSpec < MiniTest::Spec
|
|
133
132
|
|
134
133
|
assert_select 'input[type=text]'
|
135
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
|
136
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
|