enumerize 2.5.0 → 2.6.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 +7 -7
- data/CHANGELOG.md +8 -0
- data/Gemfile +1 -1
- data/README.md +3 -3
- data/enumerize.gemspec +1 -1
- data/lib/enumerize/activerecord.rb +1 -1
- data/lib/enumerize/attribute.rb +4 -0
- data/lib/enumerize/base.rb +20 -16
- data/lib/enumerize/mongoid.rb +7 -0
- data/lib/enumerize/predicatable.rb +1 -1
- data/lib/enumerize/version.rb +1 -1
- data/lib/enumerize.rb +1 -1
- data/test/activemodel_test.rb +1 -1
- data/test/activerecord_test.rb +6 -3
- data/test/attribute_test.rb +1 -1
- data/test/base_test.rb +27 -1
- data/test/mongo_mapper_test.rb +1 -1
- data/test/mongoid_test.rb +1 -1
- data/test/multiple_test.rb +1 -1
- data/test/predicates_test.rb +1 -1
- data/test/sequel_test.rb +198 -200
- data/test/set_test.rb +1 -1
- data/test/test_helper.rb +8 -0
- data/test/value_test.rb +1 -1
- metadata +30 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7ed5759e8b7d540b7261ef4112d6ed175403650cd18f904e19c15826cc1b87b5
|
4
|
+
data.tar.gz: 44fdf94b2adcf41a5ffd35e415b9ff622dab59658fd2d16efac6f52ddd89bfcb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0aab786d5ef4ebbd156211e9c659b6397facab2b1b075767f170ed706b7787d953baec2c3ffecb9a918d0cd3f87eecd541f41b7868045f4beafbb2584ca83fe3
|
7
|
+
data.tar.gz: 06af74a445ac38938b90e15e5e14cfc529332189328b86929a976593c376f46189ae6ded196ce1ca93c3c31d9f5b68b5ef03c91d65fe7e38d8a392ba88ef1dc6
|
data/.github/workflows/ruby.yml
CHANGED
@@ -32,7 +32,7 @@ jobs:
|
|
32
32
|
strategy:
|
33
33
|
fail-fast: false
|
34
34
|
matrix:
|
35
|
-
ruby-version: ['2.
|
35
|
+
ruby-version: ['2.7', '3.0', '3.1']
|
36
36
|
gemfile:
|
37
37
|
- Gemfile
|
38
38
|
- Gemfile.rails60
|
@@ -47,14 +47,14 @@ jobs:
|
|
47
47
|
exclude:
|
48
48
|
- gemfile: Gemfile.mongo_mapper
|
49
49
|
db: postgresql
|
50
|
-
- ruby-version: '3.0
|
50
|
+
- ruby-version: '3.0'
|
51
51
|
gemfile: Gemfile
|
52
|
-
- ruby-version: '3.
|
52
|
+
- ruby-version: '3.1'
|
53
|
+
gemfile: Gemfile
|
54
|
+
- ruby-version: '3.0'
|
55
|
+
gemfile: Gemfile.mongo_mapper
|
56
|
+
- ruby-version: '3.1'
|
53
57
|
gemfile: Gemfile.mongo_mapper
|
54
|
-
- ruby-version: '2.6'
|
55
|
-
gemfile: Gemfile.rails70
|
56
|
-
- ruby-version: '2.6'
|
57
|
-
gemfile: Gemfile.railsmaster
|
58
58
|
env:
|
59
59
|
BUNDLE_GEMFILE: "${{ matrix.gemfile }}"
|
60
60
|
DB: "${{ matrix.db }}"
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,11 @@
|
|
1
|
+
## 2.6.0 (March 7, 2023)
|
2
|
+
|
3
|
+
### enhancements
|
4
|
+
|
5
|
+
* Add `Enumerize::Attribute#value?`. This new method prevents the following breakage
|
6
|
+
when autocorrecting with Performance/InefficientHashSearch cop. (by [@koic](https://github.com/koic))
|
7
|
+
* Allow usage of kwargs with inheritance in Ruby 3.x. Support only Ruby 2.7+. (by [@mortik](https://github.com/mortik))
|
8
|
+
|
1
9
|
## 2.5.0 (December 24, 2021)
|
2
10
|
|
3
11
|
### enhancements
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Enumerize [![
|
1
|
+
# Enumerize [![Ruby](https://github.com/brainspec/enumerize/actions/workflows/ruby.yml/badge.svg)](https://github.com/brainspec/enumerize/actions/workflows/ruby.yml)
|
2
2
|
|
3
3
|
Enumerated attributes with I18n and ActiveRecord/Mongoid/MongoMapper/Sequel support
|
4
4
|
|
@@ -46,7 +46,7 @@ Or install it yourself as:
|
|
46
46
|
|
47
47
|
## Supported Versions
|
48
48
|
|
49
|
-
- Ruby 2.
|
49
|
+
- Ruby 2.7+
|
50
50
|
- Rails 5.2+
|
51
51
|
|
52
52
|
## Usage
|
@@ -61,7 +61,7 @@ class User
|
|
61
61
|
end
|
62
62
|
```
|
63
63
|
|
64
|
-
Note that enumerized values are just
|
64
|
+
Note that enumerized values are just identifiers so if you want to use multi-word, etc. values then you should use `I18n` feature.
|
65
65
|
|
66
66
|
---
|
67
67
|
|
data/enumerize.gemspec
CHANGED
data/lib/enumerize/attribute.rb
CHANGED
data/lib/enumerize/base.rb
CHANGED
@@ -49,7 +49,7 @@ module Enumerize
|
|
49
49
|
end
|
50
50
|
end
|
51
51
|
|
52
|
-
def initialize(*)
|
52
|
+
def initialize(*args, **kwargs)
|
53
53
|
super
|
54
54
|
_set_default_value_for_enumerized_attributes
|
55
55
|
end
|
@@ -90,22 +90,26 @@ module Enumerize
|
|
90
90
|
|
91
91
|
def _set_default_value_for_enumerized_attributes
|
92
92
|
self.class.enumerized_attributes.each do |attr|
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
93
|
+
_set_default_value_for_enumerized_attribute(attr)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
def _set_default_value_for_enumerized_attribute(attr)
|
98
|
+
return if attr.default_value.nil?
|
99
|
+
begin
|
100
|
+
if respond_to?(attr.name)
|
101
|
+
attr_value = public_send(attr.name)
|
102
|
+
else
|
103
|
+
return
|
104
|
+
end
|
105
|
+
|
106
|
+
value_for_validation = _enumerized_values_for_validation[attr.name.to_s]
|
107
|
+
|
108
|
+
if (!attr_value || attr_value.empty?) && (!value_for_validation || value_for_validation.empty?)
|
109
|
+
value = Utils.call_if_callable(attr.default_value, self)
|
110
|
+
public_send("#{attr.name}=", value)
|
108
111
|
end
|
112
|
+
rescue ActiveModel::MissingAttributeError
|
109
113
|
end
|
110
114
|
end
|
111
115
|
end
|
data/lib/enumerize/mongoid.rb
CHANGED
data/lib/enumerize/version.rb
CHANGED
data/lib/enumerize.rb
CHANGED
data/test/activemodel_test.rb
CHANGED
data/test/activerecord_test.rb
CHANGED
@@ -8,6 +8,9 @@ db = (ENV['DB'] || 'sqlite3').to_sym
|
|
8
8
|
|
9
9
|
silence_warnings do
|
10
10
|
ActiveRecord::Migration.verbose = false
|
11
|
+
if ActiveRecord::Base.respond_to?(:use_yaml_unsafe_load)
|
12
|
+
ActiveRecord::Base.use_yaml_unsafe_load = true
|
13
|
+
end
|
11
14
|
ActiveRecord::Base.logger = Logger.new(nil)
|
12
15
|
ActiveRecord::Base.configurations = {
|
13
16
|
'sqlite3' => {
|
@@ -151,7 +154,7 @@ class SkipValidationsLambdaWithParamUser < ActiveRecord::Base
|
|
151
154
|
include SkipValidationsLambdaWithParamEnum
|
152
155
|
end
|
153
156
|
|
154
|
-
|
157
|
+
class ActiveRecordTest < MiniTest::Spec
|
155
158
|
it 'sets nil if invalid value is passed' do
|
156
159
|
user = User.new
|
157
160
|
user.sex = :invalid
|
@@ -511,7 +514,7 @@ describe Enumerize::ActiveRecordSupport do
|
|
511
514
|
user = User.create(:status => :active)
|
512
515
|
user.status = :blocked
|
513
516
|
|
514
|
-
assert_equal [1, 2],
|
517
|
+
assert_equal [1, 2], unsafe_yaml_load(user.changes.to_yaml)[:status]
|
515
518
|
end
|
516
519
|
|
517
520
|
it 'does not change by the practical same value' do
|
@@ -630,7 +633,7 @@ describe Enumerize::ActiveRecordSupport do
|
|
630
633
|
end
|
631
634
|
|
632
635
|
it "doesn't break YAML serialization" do
|
633
|
-
user =
|
636
|
+
user = unsafe_yaml_load(User.create(status: :blocked).to_yaml)
|
634
637
|
expect(user.status).must_equal 'blocked'
|
635
638
|
end
|
636
639
|
|
data/test/attribute_test.rb
CHANGED
data/test/base_test.rb
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
require 'test_helper'
|
4
4
|
|
5
|
-
|
5
|
+
class BaseTest < MiniTest::Spec
|
6
6
|
let(:kklass) do
|
7
7
|
Class.new do
|
8
8
|
extend Enumerize
|
@@ -201,4 +201,30 @@ describe Enumerize::Base do
|
|
201
201
|
object.foo = :b
|
202
202
|
expect(object.foo_value).must_equal 2
|
203
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
|
204
230
|
end
|
data/test/mongo_mapper_test.rb
CHANGED
@@ -10,7 +10,7 @@ end
|
|
10
10
|
|
11
11
|
MongoMapper.connection = Mongo::Client.new(['localhost:27017'], database: 'enumerize-test-suite-of-mongomapper')
|
12
12
|
|
13
|
-
|
13
|
+
class MongoMapperTest < MiniTest::Spec
|
14
14
|
class MongoMapperUser
|
15
15
|
include MongoMapper::Document
|
16
16
|
extend Enumerize
|
data/test/mongoid_test.rb
CHANGED
data/test/multiple_test.rb
CHANGED
data/test/predicates_test.rb
CHANGED
data/test/sequel_test.rb
CHANGED
@@ -5,7 +5,7 @@ require 'sequel'
|
|
5
5
|
require 'logger'
|
6
6
|
require 'jdbc/sqlite3' if RUBY_PLATFORM == 'java'
|
7
7
|
|
8
|
-
|
8
|
+
class SequelTest < MiniTest::Spec
|
9
9
|
silence_warnings do
|
10
10
|
DB = if RUBY_PLATFORM == 'java'
|
11
11
|
Sequel.connect('jdbc:sqlite::memory:')
|
@@ -89,256 +89,254 @@ module SequelTest
|
|
89
89
|
include SkipValidationsLambdaWithParamEnum
|
90
90
|
end
|
91
91
|
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
end
|
92
|
+
it 'sets nil if invalid value is passed' do
|
93
|
+
user = User.new
|
94
|
+
user.sex = :invalid
|
95
|
+
expect(user.sex).must_be_nil
|
96
|
+
end
|
98
97
|
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
98
|
+
it 'saves value' do
|
99
|
+
User.filter{ true }.delete
|
100
|
+
user = User.new
|
101
|
+
user.sex = :female
|
102
|
+
user.save
|
103
|
+
expect(user.sex).must_equal 'female'
|
104
|
+
end
|
106
105
|
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
end
|
106
|
+
it 'loads value' do
|
107
|
+
User.filter{ true }.delete
|
108
|
+
User.create(:sex => :male)
|
109
|
+
store_translations(:en, :enumerize => {:sex => {:male => 'Male'}}) do
|
110
|
+
user = User.first
|
111
|
+
expect(user.sex).must_equal 'male'
|
112
|
+
expect(user.sex_text).must_equal 'Male'
|
115
113
|
end
|
114
|
+
end
|
116
115
|
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
116
|
+
it 'has default value' do
|
117
|
+
expect(User.new.role).must_equal 'user'
|
118
|
+
expect(User.new.values[:role]).must_equal 'user'
|
119
|
+
end
|
121
120
|
|
122
|
-
|
123
|
-
|
124
|
-
|
121
|
+
it 'does not set default value for not selected attributes' do
|
122
|
+
User.filter{ true }.delete
|
123
|
+
User.create(:sex => :male)
|
125
124
|
|
126
|
-
|
127
|
-
|
125
|
+
assert_equal [:id], User.select(:id).first.values.keys
|
126
|
+
end
|
128
127
|
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
128
|
+
it 'has default value with lambda' do
|
129
|
+
expect(User.new.lambda_role).must_equal 'admin'
|
130
|
+
expect(User.new.values[:lambda_role]).must_equal 'admin'
|
131
|
+
end
|
132
|
+
it 'uses after_initialize callback to set default value' do
|
133
|
+
User.filter{ true }.delete
|
134
|
+
User.create(sex: 'male', lambda_role: nil)
|
136
135
|
|
137
|
-
|
138
|
-
|
139
|
-
|
136
|
+
user = User.where(:sex => 'male').first
|
137
|
+
expect(user.lambda_role).must_equal 'admin'
|
138
|
+
end
|
140
139
|
|
141
|
-
|
142
|
-
|
143
|
-
|
140
|
+
it 'uses default value from db column' do
|
141
|
+
expect(User.new.account_type).must_equal 'basic'
|
142
|
+
end
|
144
143
|
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
144
|
+
it 'validates inclusion' do
|
145
|
+
user = User.new
|
146
|
+
user.role = 'wrong'
|
147
|
+
expect(user).wont_be :valid?
|
148
|
+
expect(user.errors[:role]).must_include 'is not included in the list'
|
149
|
+
end
|
151
150
|
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
end
|
151
|
+
it 'validates inclusion on mass assignment' do
|
152
|
+
assert_raises Sequel::ValidationFailed do
|
153
|
+
User.create(role: 'wrong')
|
156
154
|
end
|
155
|
+
end
|
157
156
|
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
157
|
+
it "uses persisted value for validation if it hasn't been set" do
|
158
|
+
user = User.create :sex => :male
|
159
|
+
expect(User[user.id].read_attribute_for_validation(:sex)).must_equal 'male'
|
160
|
+
end
|
162
161
|
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
162
|
+
it 'is valid with empty string assigned' do
|
163
|
+
user = User.new
|
164
|
+
user.role = ''
|
165
|
+
expect(user).must_be :valid?
|
166
|
+
end
|
168
167
|
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
168
|
+
it 'stores nil when empty string assigned' do
|
169
|
+
user = User.new
|
170
|
+
user.role = ''
|
171
|
+
expect(user.values[:role]).must_be_nil
|
172
|
+
end
|
174
173
|
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
174
|
+
it 'validates inclusion when :skip_validations = false' do
|
175
|
+
user = DoNotSkipValidationsUser.new
|
176
|
+
user.foo = 'wrong'
|
177
|
+
expect(user).wont_be :valid?
|
178
|
+
expect(user.errors[:foo]).must_include 'is not included in the list'
|
179
|
+
end
|
181
180
|
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
181
|
+
it 'does not validate inclusion when :skip_validations = true' do
|
182
|
+
user = SkipValidationsUser.new
|
183
|
+
user.foo = 'wrong'
|
184
|
+
expect(user).must_be :valid?
|
185
|
+
end
|
187
186
|
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
187
|
+
it 'supports :skip_validations option as lambda' do
|
188
|
+
user = SkipValidationsLambdaUser.new
|
189
|
+
user.foo = 'wrong'
|
190
|
+
expect(user).must_be :valid?
|
191
|
+
end
|
193
192
|
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
193
|
+
it 'supports :skip_validations option as lambda with a parameter' do
|
194
|
+
user = SkipValidationsLambdaWithParamUser.new
|
195
|
+
user.foo = 'wrong'
|
196
|
+
expect(user).must_be :valid?
|
197
|
+
end
|
199
198
|
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
199
|
+
it 'supports multiple attributes' do
|
200
|
+
user = User.new
|
201
|
+
user.interests ||= []
|
202
|
+
expect(user.interests).must_be_empty
|
203
|
+
user.interests << "music"
|
204
|
+
expect(user.interests).must_equal %w(music)
|
205
|
+
user.save
|
206
|
+
|
207
|
+
user = User[user.id]
|
208
|
+
expect(user.interests).must_be_instance_of Enumerize::Set
|
209
|
+
expect(user.interests).must_equal %w(music)
|
210
|
+
user.interests << "sports"
|
211
|
+
expect(user.interests).must_equal %w(music sports)
|
212
|
+
|
213
|
+
user.interests = []
|
214
|
+
interests = user.interests
|
215
|
+
interests << "music"
|
216
|
+
expect(interests).must_equal %w(music)
|
217
|
+
interests << "dancing"
|
218
|
+
expect(interests).must_equal %w(music dancing)
|
219
|
+
end
|
221
220
|
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
221
|
+
it 'returns invalid multiple value for validation' do
|
222
|
+
user = User.new
|
223
|
+
user.interests << :music
|
224
|
+
user.interests << :invalid
|
225
|
+
values = user.read_attribute_for_validation(:interests)
|
226
|
+
expect(values).must_equal %w(music invalid)
|
227
|
+
end
|
229
228
|
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
229
|
+
it 'validates multiple attributes' do
|
230
|
+
user = User.new
|
231
|
+
user.interests << :invalid
|
232
|
+
expect(user).wont_be :valid?
|
234
233
|
|
235
|
-
|
236
|
-
|
234
|
+
user.interests = Object.new
|
235
|
+
expect(user).wont_be :valid?
|
237
236
|
|
238
|
-
|
239
|
-
|
240
|
-
|
237
|
+
user.interests = ['music', '']
|
238
|
+
expect(user).must_be :valid?
|
239
|
+
end
|
241
240
|
|
242
|
-
|
243
|
-
|
241
|
+
it 'stores custom values for multiple attributes' do
|
242
|
+
User.filter{ true }.delete
|
244
243
|
|
245
|
-
|
246
|
-
|
244
|
+
klass = Class.new(User)
|
245
|
+
klass.enumerize :interests, in: { music: 0, sports: 1, dancing: 2, programming: 3}, multiple: true
|
247
246
|
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
247
|
+
user = klass.new
|
248
|
+
user.interests << :music
|
249
|
+
expect(user.interests).must_equal %w(music)
|
250
|
+
user.save
|
252
251
|
|
253
|
-
|
254
|
-
|
255
|
-
|
252
|
+
user = klass[user.id]
|
253
|
+
expect(user.interests).must_equal %w(music)
|
254
|
+
end
|
256
255
|
|
257
|
-
|
258
|
-
|
256
|
+
it 'adds scope' do
|
257
|
+
User.filter{ true }.delete
|
259
258
|
|
260
|
-
|
261
|
-
|
262
|
-
|
259
|
+
user_1 = User.create(sex: :female, skill: :noob, status: :active, role: :admin)
|
260
|
+
user_2 = User.create(sex: :female, skill: :casual, status: :blocked)
|
261
|
+
user_3 = User.create(sex: :male, skill: :pro)
|
263
262
|
|
264
|
-
|
265
|
-
|
266
|
-
|
263
|
+
expect(User.with_status(:active).to_a).must_equal [user_1]
|
264
|
+
expect(User.with_status(:blocked).to_a).must_equal [user_2]
|
265
|
+
expect(User.with_status(:active, :blocked).to_set).must_equal [user_1, user_2].to_set
|
267
266
|
|
268
|
-
|
269
|
-
|
267
|
+
expect(User.without_status(:active).to_a).must_equal [user_2]
|
268
|
+
expect(User.without_status(:active, :blocked).to_a).must_equal []
|
270
269
|
|
271
|
-
|
272
|
-
|
273
|
-
|
270
|
+
expect(User.having_role(:admin).to_a).must_equal [user_1]
|
271
|
+
expect(User.male.to_a).must_equal [user_3]
|
272
|
+
expect(User.pro.to_a).must_equal [user_3]
|
274
273
|
|
275
|
-
|
276
|
-
|
277
|
-
|
274
|
+
expect(User.not_male.to_set).must_equal [user_1, user_2].to_set
|
275
|
+
expect(User.not_pro.to_set).must_equal [user_1, user_2].to_set
|
276
|
+
end
|
278
277
|
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
278
|
+
it 'allows either key or value as valid' do
|
279
|
+
user_1 = User.new(status: :active)
|
280
|
+
user_2 = User.new(status: 1)
|
281
|
+
user_3 = User.new(status: '1')
|
283
282
|
|
284
|
-
|
285
|
-
|
286
|
-
|
283
|
+
expect(user_1.status).must_equal 'active'
|
284
|
+
expect(user_2.status).must_equal 'active'
|
285
|
+
expect(user_3.status).must_equal 'active'
|
287
286
|
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
287
|
+
expect(user_1).must_be :valid?
|
288
|
+
expect(user_2).must_be :valid?
|
289
|
+
expect(user_3).must_be :valid?
|
290
|
+
end
|
292
291
|
|
293
|
-
|
294
|
-
|
292
|
+
it 'supports defining enumerized attributes on abstract class' do
|
293
|
+
Document.filter{ true }.delete
|
295
294
|
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
295
|
+
document = Document.new
|
296
|
+
document.visibility = :protected
|
297
|
+
expect(document.visibility).must_equal 'protected'
|
298
|
+
end
|
300
299
|
|
301
|
-
|
302
|
-
|
300
|
+
it 'supports defining enumerized scopes on abstract class' do
|
301
|
+
Document.filter{ true }.delete
|
303
302
|
|
304
|
-
|
305
|
-
|
303
|
+
document_1 = Document.create(visibility: :public)
|
304
|
+
document_2 = Document.create(visibility: :private)
|
306
305
|
|
307
|
-
|
308
|
-
|
306
|
+
expect(Document.with_visibility(:public).to_a).must_equal [document_1]
|
307
|
+
end
|
309
308
|
|
310
|
-
|
311
|
-
|
309
|
+
it 'validates uniqueness' do
|
310
|
+
user = User.create(status: :active, sex: "male")
|
312
311
|
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
312
|
+
user = UniqStatusUser.new
|
313
|
+
user.sex = "male"
|
314
|
+
user.status = :active
|
315
|
+
expect(user.valid?).must_equal false
|
317
316
|
|
318
|
-
|
319
|
-
|
317
|
+
expect(user.errors[:status]).wont_be :empty?
|
318
|
+
end
|
320
319
|
|
321
|
-
|
322
|
-
|
320
|
+
it "doesn't update record" do
|
321
|
+
Document.filter{ true }.delete
|
323
322
|
|
324
|
-
|
323
|
+
expected = Time.new(2010, 10, 10)
|
325
324
|
|
326
|
-
|
327
|
-
|
328
|
-
|
325
|
+
document = Document.new
|
326
|
+
document.updated_at = expected
|
327
|
+
document.save
|
329
328
|
|
330
|
-
|
331
|
-
|
329
|
+
document = Document.last
|
330
|
+
document.save
|
332
331
|
|
333
|
-
|
334
|
-
|
332
|
+
assert_equal expected, document.updated_at
|
333
|
+
end
|
335
334
|
|
336
|
-
|
337
|
-
|
338
|
-
|
335
|
+
it 'changes from dirty should be serialized as scalar values' do
|
336
|
+
user = User.create(:status => :active)
|
337
|
+
user.status = :blocked
|
339
338
|
|
340
|
-
|
341
|
-
|
342
|
-
end
|
339
|
+
expected = { status: [1, 2] }.to_yaml
|
340
|
+
assert_equal expected, user.column_changes.to_yaml
|
343
341
|
end
|
344
342
|
end
|
data/test/set_test.rb
CHANGED
data/test/test_helper.rb
CHANGED
data/test/value_test.rb
CHANGED
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.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sergey Nartimov
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-03-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -99,7 +99,7 @@ homepage: https://github.com/brainspec/enumerize
|
|
99
99
|
licenses:
|
100
100
|
- MIT
|
101
101
|
metadata: {}
|
102
|
-
post_install_message:
|
102
|
+
post_install_message:
|
103
103
|
rdoc_options: []
|
104
104
|
require_paths:
|
105
105
|
- lib
|
@@ -107,15 +107,37 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
107
107
|
requirements:
|
108
108
|
- - ">="
|
109
109
|
- !ruby/object:Gem::Version
|
110
|
-
version:
|
110
|
+
version: '2.7'
|
111
111
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
112
112
|
requirements:
|
113
113
|
- - ">="
|
114
114
|
- !ruby/object:Gem::Version
|
115
115
|
version: '0'
|
116
116
|
requirements: []
|
117
|
-
rubygems_version: 3.
|
118
|
-
signing_key:
|
117
|
+
rubygems_version: 3.4.7
|
118
|
+
signing_key:
|
119
119
|
specification_version: 4
|
120
120
|
summary: Enumerated attributes with I18n and ActiveRecord/Mongoid/MongoMapper support
|
121
|
-
test_files:
|
121
|
+
test_files:
|
122
|
+
- spec/enumerize/integrations/rspec/matcher_spec.rb
|
123
|
+
- spec/spec_helper.rb
|
124
|
+
- test/activemodel_test.rb
|
125
|
+
- test/activerecord_test.rb
|
126
|
+
- test/attribute_map_test.rb
|
127
|
+
- test/attribute_test.rb
|
128
|
+
- test/base_test.rb
|
129
|
+
- test/formtastic_test.rb
|
130
|
+
- test/module_attributes_test.rb
|
131
|
+
- test/mongo_mapper_test.rb
|
132
|
+
- test/mongoid_test.rb
|
133
|
+
- test/multiple_test.rb
|
134
|
+
- test/predicates_test.rb
|
135
|
+
- test/rails_admin_test.rb
|
136
|
+
- test/sequel_test.rb
|
137
|
+
- test/set_test.rb
|
138
|
+
- test/simple_form_test.rb
|
139
|
+
- test/support/mock_controller.rb
|
140
|
+
- test/support/shared_enums.rb
|
141
|
+
- test/support/view_test_helper.rb
|
142
|
+
- test/test_helper.rb
|
143
|
+
- test/value_test.rb
|