enumerize 2.0.0 → 2.0.1
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 +4 -4
- data/CHANGELOG.md +6 -0
- data/lib/enumerize/activerecord.rb +14 -7
- data/lib/enumerize/version.rb +1 -1
- data/test/activerecord_test.rb +39 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1b707373f1cda45d2a21fd4f4b6fa2e512e94d0c
|
4
|
+
data.tar.gz: 989f1bbf21977dd0ab191ed2a41d865193702b82
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 55c9e20ec4db7a12db817177e8de84caa23659186312418bba4c8f96df5acde2cbea01f68485884e315dae24bbec36d4d4485dc8013591be9cae26e638f44abd
|
7
|
+
data.tar.gz: 6c592bfc03f5187f9caa8a07138ea16107a37fe037c610dfe8e4cf9e71b42e88e2a89181014a1074d57b07d234bd70b80663c33b7bc43017caa2731b8403ab7c
|
data/CHANGELOG.md
CHANGED
@@ -7,6 +7,10 @@ module Enumerize
|
|
7
7
|
if self < ::ActiveRecord::Base
|
8
8
|
include InstanceMethods
|
9
9
|
|
10
|
+
const_get(:ActiveRecord_Relation).include(RelationMethods)
|
11
|
+
const_get(:ActiveRecord_AssociationRelation).include(RelationMethods)
|
12
|
+
const_get(:ActiveRecord_Associations_CollectionProxy).include(RelationMethods)
|
13
|
+
|
10
14
|
# Since Rails use `allocate` method on models and initializes them with `init_with` method.
|
11
15
|
# This way `initialize` method is not being called, but `after_initialize` callback always gets triggered.
|
12
16
|
after_initialize :_set_default_value_for_enumerized_attributes
|
@@ -42,15 +46,18 @@ module Enumerize
|
|
42
46
|
end
|
43
47
|
end
|
44
48
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
49
|
+
module RelationMethods
|
50
|
+
def update_all(updates)
|
51
|
+
if updates.is_a?(Hash)
|
52
|
+
enumerized_attributes.each do |attr|
|
53
|
+
next if updates[attr.name].blank? || attr.kind_of?(Enumerize::Multiple)
|
54
|
+
enumerize_value = attr.find_value(updates[attr.name])
|
55
|
+
updates[attr.name] = enumerize_value && enumerize_value.value
|
56
|
+
end
|
50
57
|
end
|
51
|
-
end
|
52
58
|
|
53
|
-
|
59
|
+
super(updates)
|
60
|
+
end
|
54
61
|
end
|
55
62
|
end
|
56
63
|
end
|
data/lib/enumerize/version.rb
CHANGED
data/test/activerecord_test.rb
CHANGED
@@ -20,7 +20,9 @@ ActiveRecord::Base.connection.instance_eval do
|
|
20
20
|
end
|
21
21
|
|
22
22
|
create_table :documents do |t|
|
23
|
+
t.integer :user_id
|
23
24
|
t.string :visibility
|
25
|
+
t.integer :status
|
24
26
|
t.timestamps null: true
|
25
27
|
end
|
26
28
|
end
|
@@ -33,6 +35,9 @@ class BaseEntity < ActiveRecord::Base
|
|
33
35
|
end
|
34
36
|
|
35
37
|
class Document < BaseEntity
|
38
|
+
belongs_to :user
|
39
|
+
|
40
|
+
enumerize :status, in: {draft: 1, release: 2}
|
36
41
|
end
|
37
42
|
|
38
43
|
module RoleEnum
|
@@ -57,6 +62,8 @@ class User < ActiveRecord::Base
|
|
57
62
|
# There is no column for relationship enumeration for testing purposes: model
|
58
63
|
# should not be broken even if the associated column does not exist yet.
|
59
64
|
enumerize :relationship, :in => [:single, :married]
|
65
|
+
|
66
|
+
has_many :documents
|
60
67
|
end
|
61
68
|
|
62
69
|
class UniqStatusUser < User
|
@@ -411,6 +418,28 @@ describe Enumerize::ActiveRecordSupport do
|
|
411
418
|
user.status.must_equal 'blocked'
|
412
419
|
end
|
413
420
|
|
421
|
+
it 'allows using update_all on relation objects' do
|
422
|
+
User.delete_all
|
423
|
+
|
424
|
+
user = User.create(status: :active, account_type: :premium)
|
425
|
+
|
426
|
+
User.all.update_all(status: :blocked)
|
427
|
+
user.reload
|
428
|
+
user.status.must_equal 'blocked'
|
429
|
+
end
|
430
|
+
|
431
|
+
it 'allows using update_all on association relation objects' do
|
432
|
+
User.delete_all
|
433
|
+
Document.delete_all
|
434
|
+
|
435
|
+
user = User.create
|
436
|
+
document = Document.create(user: user, status: :draft)
|
437
|
+
|
438
|
+
user.documents.update_all(status: :release)
|
439
|
+
document.reload
|
440
|
+
document.status.must_equal 'release'
|
441
|
+
end
|
442
|
+
|
414
443
|
it 'preserves string usage of update_all' do
|
415
444
|
User.delete_all
|
416
445
|
|
@@ -430,4 +459,14 @@ describe Enumerize::ActiveRecordSupport do
|
|
430
459
|
user.reload
|
431
460
|
user.name.must_equal 'Frederick'
|
432
461
|
end
|
462
|
+
|
463
|
+
it 'sets attribute to nil if given one is not valid' do
|
464
|
+
User.delete_all
|
465
|
+
|
466
|
+
user = User.create(status: :active)
|
467
|
+
|
468
|
+
User.update_all(status: :foo)
|
469
|
+
user.reload
|
470
|
+
user.status.must_equal nil
|
471
|
+
end
|
433
472
|
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.0.
|
4
|
+
version: 2.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sergey Nartimov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-10-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|