enumerize 2.0.0 → 2.7.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.
- checksums.yaml +5 -5
- data/.github/workflows/ruby.yml +75 -0
- data/CHANGELOG.md +117 -3
- data/Gemfile +3 -3
- data/Gemfile.global +5 -9
- data/Gemfile.mongo_mapper +2 -2
- data/Gemfile.rails60 +6 -0
- data/Gemfile.rails61 +6 -0
- data/Gemfile.rails70 +9 -0
- data/Gemfile.railsmaster +5 -0
- data/README.md +210 -92
- data/Rakefile +9 -3
- data/enumerize.gemspec +1 -1
- data/lib/enumerize/activemodel.rb +47 -0
- data/lib/enumerize/activerecord.rb +92 -6
- data/lib/enumerize/attribute.rb +19 -11
- data/lib/enumerize/attribute_map.rb +2 -0
- data/lib/enumerize/base.rb +22 -18
- data/lib/enumerize/hooks/formtastic.rb +4 -1
- data/lib/enumerize/hooks/sequel_dataset.rb +2 -0
- data/lib/enumerize/hooks/simple_form.rb +4 -1
- data/lib/enumerize/hooks/uniqueness.rb +5 -1
- data/lib/enumerize/integrations/rails_admin.rb +3 -1
- data/lib/enumerize/integrations/rspec/matcher.rb +7 -2
- 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 +23 -0
- data/lib/enumerize/predicatable.rb +3 -1
- data/lib/enumerize/predicates.rb +16 -0
- data/lib/enumerize/scope/activerecord.rb +16 -0
- data/lib/enumerize/scope/mongoid.rb +15 -0
- data/lib/enumerize/scope/sequel.rb +16 -0
- data/lib/enumerize/sequel.rb +9 -4
- data/lib/enumerize/set.rb +2 -0
- data/lib/enumerize/utils.rb +12 -0
- data/lib/enumerize/value.rb +14 -15
- data/lib/enumerize/version.rb +3 -1
- data/lib/enumerize.rb +9 -0
- data/lib/sequel/plugins/enumerize.rb +18 -0
- data/spec/enumerize/integrations/rspec/matcher_spec.rb +13 -10
- data/spec/spec_helper.rb +2 -0
- data/test/activemodel_test.rb +114 -0
- data/test/activerecord_test.rb +327 -78
- data/test/attribute_map_test.rb +9 -7
- data/test/attribute_test.rb +37 -34
- data/test/base_test.rb +117 -65
- data/test/formtastic_test.rb +25 -0
- data/test/module_attributes_test.rb +10 -8
- data/test/mongo_mapper_test.rb +20 -11
- data/test/mongoid_test.rb +52 -23
- data/test/multiple_test.rb +27 -19
- data/test/predicates_test.rb +51 -29
- data/test/rails_admin_test.rb +8 -6
- data/test/sequel_test.rb +228 -177
- data/test/set_test.rb +29 -27
- data/test/simple_form_test.rb +25 -0
- data/test/support/mock_controller.rb +6 -0
- data/test/support/shared_enums.rb +43 -0
- data/test/support/view_test_helper.rb +14 -1
- data/test/test_helper.rb +10 -0
- data/test/value_test.rb +59 -31
- metadata +16 -7
- data/.travis.yml +0 -18
- data/Gemfile.rails42 +0 -6
data/test/simple_form_test.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'test_helper'
|
2
4
|
require 'simple_form/version'
|
3
5
|
|
@@ -40,6 +42,12 @@ class SimpleFormSpec < MiniTest::Spec
|
|
40
42
|
end
|
41
43
|
end
|
42
44
|
|
45
|
+
class Registration < Struct.new(:sex)
|
46
|
+
extend Enumerize
|
47
|
+
|
48
|
+
enumerize :sex, in: [:male, :female]
|
49
|
+
end
|
50
|
+
|
43
51
|
let(:user) { User.new }
|
44
52
|
let(:post) { Post.new }
|
45
53
|
|
@@ -128,4 +136,21 @@ class SimpleFormSpec < MiniTest::Spec
|
|
128
136
|
|
129
137
|
assert_select 'input.string'
|
130
138
|
end
|
139
|
+
|
140
|
+
it 'renders select with enumerized values for non-ActiveModel object' do
|
141
|
+
concat(simple_form_for(Registration.new, as: 'registration', url: '/') do |f|
|
142
|
+
f.input(:sex)
|
143
|
+
end)
|
144
|
+
|
145
|
+
assert_select 'select option[value=male]'
|
146
|
+
assert_select 'select option[value=female]'
|
147
|
+
end
|
148
|
+
|
149
|
+
it 'does not affect forms without object' do
|
150
|
+
concat(simple_form_for('') do |f|
|
151
|
+
f.input(:name)
|
152
|
+
end)
|
153
|
+
|
154
|
+
assert_select 'input.string'
|
155
|
+
end
|
131
156
|
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_record'
|
4
|
+
require 'sequel'
|
5
|
+
|
6
|
+
module EnumerizeExtention
|
7
|
+
def self.included(base)
|
8
|
+
case
|
9
|
+
when base < ActiveRecord::Base
|
10
|
+
base.extend Enumerize
|
11
|
+
when base < Sequel::Model
|
12
|
+
base.plugin :enumerize
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
module SkipValidationsEnum
|
18
|
+
def self.included(base)
|
19
|
+
base.include EnumerizeExtention
|
20
|
+
base.enumerize :foo, :in => [:bar, :baz], :skip_validations => true
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
module DoNotSkipValidationsEnum
|
25
|
+
def self.included(base)
|
26
|
+
base.include EnumerizeExtention
|
27
|
+
base.enumerize :foo, :in => [:bar, :baz], :skip_validations => false
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
module SkipValidationsLambdaEnum
|
32
|
+
def self.included(base)
|
33
|
+
base.include EnumerizeExtention
|
34
|
+
base.enumerize :foo, :in => [:bar, :baz], :skip_validations => lambda { true }
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
module SkipValidationsLambdaWithParamEnum
|
39
|
+
def self.included(base)
|
40
|
+
base.include EnumerizeExtention
|
41
|
+
base.enumerize :foo, :in => [:bar, :baz], :skip_validations => lambda { |record| true }
|
42
|
+
end
|
43
|
+
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'active_support/concern'
|
2
4
|
require 'active_support/testing/setup_and_teardown'
|
3
5
|
|
@@ -5,10 +7,21 @@ if defined?(ActionView::RoutingUrlFor)
|
|
5
7
|
ActionView::RoutingUrlFor.send(:include, ActionDispatch::Routing::UrlFor)
|
6
8
|
end
|
7
9
|
|
8
|
-
module
|
10
|
+
module SetupAndTeardownHelper
|
9
11
|
extend ActiveSupport::Concern
|
10
12
|
|
11
13
|
include ActiveSupport::Testing::SetupAndTeardown
|
14
|
+
|
15
|
+
included do
|
16
|
+
include ActiveSupport::Callbacks
|
17
|
+
define_callbacks :setup, :teardown
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
module ViewTestHelper
|
22
|
+
extend ActiveSupport::Concern
|
23
|
+
|
24
|
+
include SetupAndTeardownHelper
|
12
25
|
include ActionView::TestCase::Behavior
|
13
26
|
|
14
27
|
included do
|
data/test/test_helper.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'minitest/autorun'
|
2
4
|
require 'minitest/spec'
|
3
5
|
require 'active_support/core_ext/kernel/reporting'
|
@@ -44,6 +46,14 @@ module MiscHelpers
|
|
44
46
|
I18n.reload!
|
45
47
|
end
|
46
48
|
end
|
49
|
+
|
50
|
+
def unsafe_yaml_load(yaml)
|
51
|
+
if YAML.respond_to?(:unsafe_load)
|
52
|
+
YAML.unsafe_load(yaml)
|
53
|
+
else
|
54
|
+
YAML.load(yaml)
|
55
|
+
end
|
56
|
+
end
|
47
57
|
end
|
48
58
|
|
49
59
|
class MiniTest::Spec
|
data/test/value_test.rb
CHANGED
@@ -1,46 +1,52 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'test_helper'
|
2
4
|
require 'yaml'
|
3
5
|
|
4
|
-
|
5
|
-
class
|
6
|
+
class ValueTest < MiniTest::Spec
|
7
|
+
class Model
|
8
|
+
end
|
9
|
+
|
10
|
+
class Attr < Struct.new(:values, :name, :i18n_scopes, :klass)
|
11
|
+
def value?(value)
|
12
|
+
values.include?(value)
|
13
|
+
end
|
6
14
|
end
|
7
15
|
|
8
|
-
let(:attr) { Attr.new([]) }
|
9
|
-
let(:val)
|
16
|
+
let(:attr) { Attr.new([], "attribute_name", [], Model) }
|
17
|
+
let(:val) { Enumerize::Value.new(attr, 'test_value', 1) }
|
10
18
|
|
11
19
|
it 'is a string' do
|
12
|
-
val.must_be_kind_of String
|
20
|
+
expect(val).must_be_kind_of String
|
13
21
|
end
|
14
22
|
|
15
23
|
describe 'equality' do
|
16
24
|
it 'is compared to string' do
|
17
|
-
val.must_be :==, 'test_value'
|
18
|
-
val.wont_be :==, 'not_value'
|
25
|
+
expect(val).must_be :==, 'test_value'
|
26
|
+
expect(val).wont_be :==, 'not_value'
|
19
27
|
end
|
20
28
|
|
21
29
|
it 'is compared to symbol' do
|
22
|
-
val.must_be :==, :test_value
|
23
|
-
val.wont_be :==, :not_value
|
30
|
+
expect(val).must_be :==, :test_value
|
31
|
+
expect(val).wont_be :==, :not_value
|
24
32
|
end
|
25
33
|
|
26
34
|
it 'is compared to integer' do
|
27
|
-
val.must_be :==, 1
|
28
|
-
val.wont_be :==, 2
|
35
|
+
expect(val).must_be :==, 1
|
36
|
+
expect(val).wont_be :==, 2
|
29
37
|
end
|
30
38
|
end
|
31
39
|
|
32
40
|
describe 'translation' do
|
33
|
-
let(:attr) { Struct.new(:values, :name, :i18n_scopes).new([], "attribute_name", []) }
|
34
|
-
|
35
41
|
it 'uses common translation' do
|
36
42
|
store_translations(:en, :enumerize => {:attribute_name => {:test_value => "Common translation"}}) do
|
37
|
-
val.text.must_be :==, "Common translation"
|
43
|
+
expect(val.text).must_be :==, "Common translation"
|
38
44
|
end
|
39
45
|
end
|
40
46
|
|
41
47
|
it 'uses default translation from the "default" section if its present' do
|
42
48
|
store_translations(:en, :enumerize => {:defaults => {:attribute_name => {:test_value => "Common translation"}}}) do
|
43
|
-
val.text.must_be :==, "Common translation"
|
49
|
+
expect(val.text).must_be :==, "Common translation"
|
44
50
|
end
|
45
51
|
end
|
46
52
|
|
@@ -48,7 +54,7 @@ describe Enumerize::Value do
|
|
48
54
|
attr.i18n_scopes = ["enumerize.model_name.attribute_name"]
|
49
55
|
|
50
56
|
store_translations(:en, :enumerize => {:model_name => {:attribute_name => {:test_value => "Model Specific translation"}}}) do
|
51
|
-
val.text.must_be :==, "Model Specific translation"
|
57
|
+
expect(val.text).must_be :==, "Model Specific translation"
|
52
58
|
end
|
53
59
|
end
|
54
60
|
|
@@ -56,13 +62,13 @@ describe Enumerize::Value do
|
|
56
62
|
attr.i18n_scopes = ["enumerize.model_name.attribute_name"]
|
57
63
|
|
58
64
|
store_translations(:en, :enumerize => {:attribute_name => {:test_value => "Common translation"}, :model_name => {:attribute_name => {:test_value => "Model Specific translation"}}}) do
|
59
|
-
val.text.must_be :==, "Model Specific translation"
|
65
|
+
expect(val.text).must_be :==, "Model Specific translation"
|
60
66
|
end
|
61
67
|
end
|
62
68
|
|
63
69
|
it 'uses simply humanized value when translation is undefined' do
|
64
70
|
store_translations(:en, :enumerize => {}) do
|
65
|
-
val.text.must_be :==, "Test value"
|
71
|
+
expect(val.text).must_be :==, "Test value"
|
66
72
|
end
|
67
73
|
end
|
68
74
|
|
@@ -70,7 +76,7 @@ describe Enumerize::Value do
|
|
70
76
|
attr.i18n_scopes = ["other.scope"]
|
71
77
|
|
72
78
|
store_translations(:en, :other => {:scope => {:test_value => "Scope specific translation"}}) do
|
73
|
-
val.text.must_be :==, "Scope specific translation"
|
79
|
+
expect(val.text).must_be :==, "Scope specific translation"
|
74
80
|
end
|
75
81
|
end
|
76
82
|
|
@@ -78,7 +84,15 @@ describe Enumerize::Value do
|
|
78
84
|
attr.i18n_scopes = ["nonexistent.scope", "other.scope"]
|
79
85
|
|
80
86
|
store_translations(:en, :other => {:scope => {:test_value => "Scope specific translation"}}) do
|
81
|
-
val.text.must_be :==, "Scope specific translation"
|
87
|
+
expect(val.text).must_be :==, "Scope specific translation"
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
it 'allows to pass a proc as i18n_scopes param' do
|
92
|
+
attr.i18n_scopes = [proc { |v| "other.scope.#{v}" }]
|
93
|
+
|
94
|
+
store_translations(:en, :other => {:scope => {:"1" => {:test_value => "Scope specific translation"}}}) do
|
95
|
+
expect(val.text).must_be :==, "Scope specific translation"
|
82
96
|
end
|
83
97
|
end
|
84
98
|
end
|
@@ -89,36 +103,44 @@ describe Enumerize::Value do
|
|
89
103
|
end
|
90
104
|
|
91
105
|
it 'returns true if value equals method' do
|
92
|
-
val.test_value
|
106
|
+
expect(val.test_value?).must_equal true
|
93
107
|
end
|
94
108
|
|
95
109
|
it 'returns false if value does not equal method' do
|
96
|
-
val.other_value
|
110
|
+
expect(val.other_value?).must_equal false
|
97
111
|
end
|
98
112
|
|
99
113
|
it 'raises NoMethodError if there are no values like boolean method' do
|
100
|
-
proc {
|
114
|
+
expect(proc {
|
101
115
|
val.some_method?
|
102
|
-
}.must_raise NoMethodError
|
116
|
+
}).must_raise NoMethodError
|
103
117
|
end
|
104
118
|
|
105
119
|
it 'raises ArgumentError if arguments are passed' do
|
106
|
-
proc {
|
120
|
+
expect(proc {
|
107
121
|
val.other_value?('<3')
|
108
|
-
}.must_raise ArgumentError
|
122
|
+
}).must_raise ArgumentError
|
109
123
|
end
|
110
124
|
|
111
125
|
it 'responds to methods for existing values' do
|
112
|
-
val.must_respond_to :test_value?
|
113
|
-
val.must_respond_to :other_value?
|
126
|
+
expect(val).must_respond_to :test_value?
|
127
|
+
expect(val).must_respond_to :other_value?
|
114
128
|
end
|
115
129
|
|
116
130
|
it 'returns a method object' do
|
117
|
-
val.method(:test_value?).must_be_instance_of Method
|
131
|
+
expect(val.method(:test_value?)).must_be_instance_of Method
|
118
132
|
end
|
119
133
|
|
120
134
|
it "doesn't respond to a method for not existing value" do
|
121
|
-
val.wont_respond_to :some_method?
|
135
|
+
expect(val).wont_respond_to :some_method?
|
136
|
+
end
|
137
|
+
|
138
|
+
it "doesn't respond to methods is value was modified" do
|
139
|
+
modified_value = val.upcase
|
140
|
+
|
141
|
+
expect(modified_value.upcase).wont_respond_to :some_method?
|
142
|
+
expect(modified_value.upcase).wont_respond_to :test_value?
|
143
|
+
expect(modified_value.upcase).wont_respond_to :other_value?
|
122
144
|
end
|
123
145
|
end
|
124
146
|
|
@@ -131,7 +153,13 @@ describe Enumerize::Value do
|
|
131
153
|
|
132
154
|
it 'serializes with Marshal' do
|
133
155
|
dump_value = Marshal.dump(val)
|
134
|
-
Marshal.load(dump_value).must_equal 'test_value'
|
156
|
+
expect(Marshal.load(dump_value)).must_equal 'test_value'
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
describe 'initialize' do
|
161
|
+
it 'no output if undefined boolean method' do
|
162
|
+
assert_silent() { Enumerize::Value.new(attr, 'test_value') }
|
135
163
|
end
|
136
164
|
end
|
137
165
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: enumerize
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sergey Nartimov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-07-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -31,19 +31,23 @@ executables: []
|
|
31
31
|
extensions: []
|
32
32
|
extra_rdoc_files: []
|
33
33
|
files:
|
34
|
+
- ".github/workflows/ruby.yml"
|
34
35
|
- ".gitignore"
|
35
36
|
- ".rspec"
|
36
|
-
- ".travis.yml"
|
37
37
|
- CHANGELOG.md
|
38
38
|
- Gemfile
|
39
39
|
- Gemfile.global
|
40
40
|
- Gemfile.mongo_mapper
|
41
|
-
- Gemfile.
|
41
|
+
- Gemfile.rails60
|
42
|
+
- Gemfile.rails61
|
43
|
+
- Gemfile.rails70
|
44
|
+
- Gemfile.railsmaster
|
42
45
|
- MIT-LICENSE
|
43
46
|
- README.md
|
44
47
|
- Rakefile
|
45
48
|
- enumerize.gemspec
|
46
49
|
- lib/enumerize.rb
|
50
|
+
- lib/enumerize/activemodel.rb
|
47
51
|
- lib/enumerize/activerecord.rb
|
48
52
|
- lib/enumerize/attribute.rb
|
49
53
|
- lib/enumerize/attribute_map.rb
|
@@ -65,10 +69,13 @@ files:
|
|
65
69
|
- lib/enumerize/scope/sequel.rb
|
66
70
|
- lib/enumerize/sequel.rb
|
67
71
|
- lib/enumerize/set.rb
|
72
|
+
- lib/enumerize/utils.rb
|
68
73
|
- lib/enumerize/value.rb
|
69
74
|
- lib/enumerize/version.rb
|
75
|
+
- lib/sequel/plugins/enumerize.rb
|
70
76
|
- spec/enumerize/integrations/rspec/matcher_spec.rb
|
71
77
|
- spec/spec_helper.rb
|
78
|
+
- test/activemodel_test.rb
|
72
79
|
- test/activerecord_test.rb
|
73
80
|
- test/attribute_map_test.rb
|
74
81
|
- test/attribute_test.rb
|
@@ -84,6 +91,7 @@ files:
|
|
84
91
|
- test/set_test.rb
|
85
92
|
- test/simple_form_test.rb
|
86
93
|
- test/support/mock_controller.rb
|
94
|
+
- test/support/shared_enums.rb
|
87
95
|
- test/support/view_test_helper.rb
|
88
96
|
- test/test_helper.rb
|
89
97
|
- test/value_test.rb
|
@@ -99,21 +107,21 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
99
107
|
requirements:
|
100
108
|
- - ">="
|
101
109
|
- !ruby/object:Gem::Version
|
102
|
-
version:
|
110
|
+
version: '2.7'
|
103
111
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
104
112
|
requirements:
|
105
113
|
- - ">="
|
106
114
|
- !ruby/object:Gem::Version
|
107
115
|
version: '0'
|
108
116
|
requirements: []
|
109
|
-
|
110
|
-
rubygems_version: 2.5.1
|
117
|
+
rubygems_version: 3.1.6
|
111
118
|
signing_key:
|
112
119
|
specification_version: 4
|
113
120
|
summary: Enumerated attributes with I18n and ActiveRecord/Mongoid/MongoMapper support
|
114
121
|
test_files:
|
115
122
|
- spec/enumerize/integrations/rspec/matcher_spec.rb
|
116
123
|
- spec/spec_helper.rb
|
124
|
+
- test/activemodel_test.rb
|
117
125
|
- test/activerecord_test.rb
|
118
126
|
- test/attribute_map_test.rb
|
119
127
|
- test/attribute_test.rb
|
@@ -129,6 +137,7 @@ test_files:
|
|
129
137
|
- test/set_test.rb
|
130
138
|
- test/simple_form_test.rb
|
131
139
|
- test/support/mock_controller.rb
|
140
|
+
- test/support/shared_enums.rb
|
132
141
|
- test/support/view_test_helper.rb
|
133
142
|
- test/test_helper.rb
|
134
143
|
- test/value_test.rb
|
data/.travis.yml
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
language: ruby
|
2
|
-
sudo: false
|
3
|
-
services:
|
4
|
-
- mongodb
|
5
|
-
gemfile:
|
6
|
-
- Gemfile
|
7
|
-
- Gemfile.rails42
|
8
|
-
- Gemfile.mongo_mapper
|
9
|
-
rvm:
|
10
|
-
- 2.2.5
|
11
|
-
- 2.3.1
|
12
|
-
- jruby-9.1.2.0
|
13
|
-
matrix:
|
14
|
-
allow_failures:
|
15
|
-
- gemfile: Gemfile
|
16
|
-
rvm: jruby-9.1.2.0
|
17
|
-
notifications:
|
18
|
-
email: false
|