enumerize 2.0.0 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a1ee47a15b97ca4432e16a29da74e928811cfa59
4
- data.tar.gz: 025e85c0386121a06550ed0989bf0b8648b3df3e
3
+ metadata.gz: 1b707373f1cda45d2a21fd4f4b6fa2e512e94d0c
4
+ data.tar.gz: 989f1bbf21977dd0ab191ed2a41d865193702b82
5
5
  SHA512:
6
- metadata.gz: 8df3f6bac28f16da9b07d8104f4e9961491827c7e04932a6449c4f61d19ab7439a31f96c8a702cae340b38ce0ba9aec42ae1e8aea688a6e6aa3a09af9a07c83a
7
- data.tar.gz: 25c120a8fbfc07367e8cf498cfba05489cf8d3d6833554422cf70560a2d470a09f56628f08aa7fef95f8d8aef49b5bc1e657c8132fde05ddcf80c74f0af2eacb
6
+ metadata.gz: 55c9e20ec4db7a12db817177e8de84caa23659186312418bba4c8f96df5acde2cbea01f68485884e315dae24bbec36d4d4485dc8013591be9cae26e638f44abd
7
+ data.tar.gz: 6c592bfc03f5187f9caa8a07138ea16107a37fe037c610dfe8e4cf9e71b42e88e2a89181014a1074d57b07d234bd70b80663c33b7bc43017caa2731b8403ab7c
data/CHANGELOG.md CHANGED
@@ -4,6 +4,12 @@
4
4
 
5
5
  ### bug fix
6
6
 
7
+ ## 2.0.1 (October 18, 2016)
8
+
9
+ ### bug fix
10
+
11
+ * Support enumerized attributes in #update_all on relation objects. (by [@lest](https://github.com/lest))
12
+
7
13
  ## 2.0.0 (August 10, 2016)
8
14
 
9
15
  ### enhancements
@@ -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
- def update_all(updates)
46
- if updates.is_a?(Hash)
47
- enumerized_attributes.each do |attr|
48
- next if updates[attr.name].blank? || attr.kind_of?(Enumerize::Multiple)
49
- updates[attr.name] = attr.find_value(updates[attr.name]).value
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
- super(updates)
59
+ super(updates)
60
+ end
54
61
  end
55
62
  end
56
63
  end
@@ -1,3 +1,3 @@
1
1
  module Enumerize
2
- VERSION = "2.0.0"
2
+ VERSION = "2.0.1"
3
3
  end
@@ -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.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-08-10 00:00:00.000000000 Z
11
+ date: 2016-10-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport