deferring 0.0.7 → 0.0.8

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 CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- YmQ0ODk2NjQ5MWRkMDI2ZjIzZjEyNzdjNzk1MGIyNmVkOGEwNmNjZQ==
4
+ ZmJiMDFiYWJiZWI2Y2UzNTU3NDk3ZmNiOWNhNmYwNzBhNmUwZDk1Yg==
5
5
  data.tar.gz: !binary |-
6
- MmZmOThmYzFkNDkzOTMzZGNhZmFhYmFkZmViOTc5ZmJmMzZmNjFmYw==
6
+ Y2YwOWRmZjA1NzkwMzZiZGIwYzU0NDdjMWQ4MWFkZGRkODNhMjA1MQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- OWI3OTAxNjNlNGIzNWZhMGEyMGE5NDM4MzQxODcwMGViZDBmNWNmNDdjZmJl
10
- ODY0MzVjNTdhNjU4MjU4ZTQwYzBkNzMwNjU3NWQ2YzA4ZDM2OWU2NThlYTMx
11
- NTAwODZjNzYyNGMzNTQ3ZTUxMjlhMzk2NDg5OTIxZjVlMWM3MWE=
9
+ NDg0NTNkMzkwYjc2ZjJhZjgxNzE1NGMyMjE2YTcwMzVjNWE1MDEzYjM2MzVk
10
+ YTc3MDRjOGM4YjJmNTBhNGQ3YmMwYzQ5Njk1N2QxOWUxMTFhMmEzNWQwOGIw
11
+ YzNlOGFmNjAwZjg3OWI1NWY0NWZiY2VkNTdmMTM4Mzc2ZmY0NzA=
12
12
  data.tar.gz: !binary |-
13
- YTEyYzMyNTJiMzBjOTY1ODkwOGE2OWUzZDRkYzNmMGMyYjAyMTE0N2E0Yjg2
14
- ZmRjOWNkZjcxZjQxNjQ0Mjc4OGRmMzVjYjYxYjUwYTA5ZmY5OTQzMWQ0Y2Q1
15
- NWNjNGU4MDk2ZGQ4MTA5YzQ2ZDM2N2EzNTc0MWZlZDI0Yzg2Mjk=
13
+ N2RlNzQxOTBhYzk5NGQzN2Q0OWM1NjE4MDI4YmVmNjlkYmNhMTk0OGMwODk1
14
+ N2UyNzhiZGIzYzllMDVkYjY4MzJlNjViZWVhZGZhZTQxNWQ3ZmI5NDhiMzIx
15
+ MDBjZGFmYWE5YjZiOTAyMmYzOGM5NWZmMGRmZTZjOWIzNmU0NGM=
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../
3
3
  specs:
4
- deferring (0.1.0)
4
+ deferring (0.0.7)
5
5
  activerecord (> 3.0)
6
6
 
7
7
  GEM
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../
3
3
  specs:
4
- deferring (0.1.0)
4
+ deferring (0.0.7)
5
5
  activerecord (> 3.0)
6
6
 
7
7
  GEM
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../
3
3
  specs:
4
- deferring (0.1.0)
4
+ deferring (0.0.7)
5
5
  activerecord (> 3.0)
6
6
 
7
7
  GEM
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../
3
3
  specs:
4
- deferring (0.1.0)
4
+ deferring (0.0.7)
5
5
  activerecord (> 3.0)
6
6
 
7
7
  GEM
data/lib/deferring.rb CHANGED
@@ -34,20 +34,43 @@ module Deferring
34
34
  def deferred_accepts_nested_attributes_for(*args)
35
35
  options = args.extract_options!
36
36
  inverse_association_name = options.fetch(:as, self.name.underscore.to_sym)
37
+ reject_if_proc = options.delete(:reject_if)
37
38
  accepts_nested_attributes_for(*args, options)
38
39
 
39
40
  association_name = args.first.to_s
40
41
 
41
42
  # teams_attributes=
42
- define_method :"#{association_name}_attributes=" do |records|
43
+ define_method :"#{association_name}_attributes=" do |attributes|
43
44
  find_or_create_deferred_association(association_name, [], inverse_association_name)
44
45
 
45
- # Remove the records that are to be destroyed from the ids that are to be
46
- # assigned to the DeferredAssociation instance.
47
- records.reject! { |record| record[:_destroy] }
46
+ # Convert the attributes to an array if a Hash is passed. This is possible
47
+ # as the keys of the hash are ignored in this case.
48
+ #
49
+ # Example:
50
+ # {
51
+ # first: { name: 'Service Desk' },
52
+ # second: { name: 'DBA' }
53
+ # }
54
+ # becomes
55
+ # [
56
+ # { name: 'Service Desk' },
57
+ # { name: 'DBA'}
58
+ # ]
59
+ attributes = attributes.values if attributes.is_a? Hash
60
+
61
+ # Remove the attributes that are to be destroyed from the ids that are to
62
+ # be assigned to the DeferredAssociation instance.
63
+ attributes.reject! { |record| record.delete(:_destroy) == '1' }
64
+
65
+ # Remove the attributes that fail the pass :reject_if proc.
66
+ attributes.reject! { |record| reject_if_proc.call(record) } if reject_if_proc
48
67
 
49
68
  klass = self.class.reflect_on_association(:"#{association_name}").klass
50
- objects = klass.find(records.map { |record| record[:id] })
69
+
70
+ objects = attributes.map do |record|
71
+ record[:id] ? klass.find(record[:id]) : klass.new(record)
72
+ end
73
+
51
74
  send(:"deferred_#{association_name}").objects = objects
52
75
  end
53
76
 
@@ -1,5 +1,5 @@
1
1
  # encoding: UTF-8
2
2
 
3
3
  module Deferring
4
- VERSION = '0.0.7'
4
+ VERSION = '0.0.8'
5
5
  end
@@ -101,24 +101,39 @@ RSpec.describe 'deferred has_many associations' do
101
101
  end
102
102
 
103
103
  describe 'accepts_nested_attributes' do
104
- it 'should mass-assign' do
104
+ it 'sets associated records when posting an array of hashes' do
105
105
  p = Person.first
106
106
  p.issues << printer_issue << db_issue << sandwich_issue
107
107
  p.save
108
108
 
109
- # Destroy db and sandwich issues. Keep printer issue.
109
+ # Destroy db and sandwich issues. Keep printer issue and create a new one.
110
110
  p = Person.first
111
111
  p.attributes = {
112
112
  issues_attributes: [
113
113
  { id: printer_issue.id },
114
- { id: sandwich_issue.id, _destroy: true },
115
- { id: db_issue.id, _destroy: true }
114
+ { subject: 'Kapow!' },
115
+ { id: sandwich_issue.id, _destroy: '1' },
116
+ { id: db_issue.id, _destroy: '1' }
116
117
  ]
117
118
  }
118
- expect(p.issues.length).to eq(1)
119
- expect(p.issue_ids.sort).to eq([1])
119
+ expect(p.issues.length).to eq(2)
120
+ expect(p.issue_ids).to eq([printer_issue.id, nil])
120
121
 
121
- expect{ p.save! }.to change{ Person.first.issues.size }.from(3).to(1)
122
+ expect{ p.save! }.to change{ Person.first.issues.size }.from(3).to(2)
123
+ end
124
+
125
+ it 'sets associated records when posting a hash of hashes' do
126
+ p = Person.first
127
+ p.attributes = {
128
+ issues_attributes: {
129
+ first: { subject: 'Kapow!' },
130
+ second: { id: printer_issue.id }
131
+ }
132
+ }
133
+ expect(p.issues.length).to eq(2)
134
+ expect(p.issue_ids).to eq([nil, printer_issue.id])
135
+
136
+ expect{ p.save! }.to change{ Person.first.issues.size }.from(0).to(2)
122
137
  end
123
138
 
124
139
  it 'sets the belongs_to association of the associated record' do
@@ -29,8 +29,8 @@ RSpec.describe 'deferred accepts_nested_attributes' do
29
29
  p.attributes = {
30
30
  teams_attributes: [
31
31
  { id: 1 },
32
- { id: 3, _destroy: true },
33
- { id: 2, _destroy: true }
32
+ { id: 3, _destroy: '1' },
33
+ { id: 2, _destroy: '1' }
34
34
  ]
35
35
  }
36
36
  expect(p.teams.length).to eq(1)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: deferring
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robin Roestenburg