deferring 0.1.2 → 0.1.3
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/lib/deferring.rb +0 -3
- data/lib/deferring/version.rb +1 -1
- data/spec/lib/deferring_has_many_spec.rb +37 -0
- data/spec/spec_helper.rb +1 -0
- data/spec/support/active_record.rb +7 -0
- data/spec/support/models/address.rb +5 -0
- data/spec/support/models/person.rb +5 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a3f9cfb39bdad6c3d5e0c4f9e98e338dabe64b1c
|
4
|
+
data.tar.gz: 9555e0057cc0930aebdb32de04b4303bc2aa35a5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4913b6ef30424be10cf8ba5255ec0c86a502adddc687970d2f986d9f01fc9219c491650e8bf10d8b5d1737b2cb7283edd79e908125e01f5c59c0dc1e4c1d9a1b
|
7
|
+
data.tar.gz: 92f38c9128d3549e3c38eb933f88f9f7c89ea124b679941e2c21fc470d2e768a27b8b828f578e0f6af78e24052da2fcc0bce3aee9d44bc34b62594c7efb6ca65
|
data/lib/deferring.rb
CHANGED
@@ -47,7 +47,6 @@ module Deferring
|
|
47
47
|
|
48
48
|
def deferred_accepts_nested_attributes_for(*args)
|
49
49
|
options = args.extract_options!
|
50
|
-
inverse_association_name = options.fetch(:as, self.name.underscore.to_sym)
|
51
50
|
reject_if = options.delete(:reject_if)
|
52
51
|
accepts_nested_attributes_for(*args, options)
|
53
52
|
|
@@ -55,8 +54,6 @@ module Deferring
|
|
55
54
|
|
56
55
|
# teams_attributes=
|
57
56
|
define_method :"#{association_name}_attributes=" do |attributes_collection|
|
58
|
-
find_or_create_deferred_association(association_name, [], inverse_association_name)
|
59
|
-
|
60
57
|
unless attributes_collection.is_a?(Hash) || attributes_collection.is_a?(Array)
|
61
58
|
raise ArgumentError, "Hash or Array expected, got #{attributes_collection.class.name} (#{attributes_collection.inspect})"
|
62
59
|
end
|
data/lib/deferring/version.rb
CHANGED
@@ -350,4 +350,41 @@ RSpec.describe 'deferred has_many associations' do
|
|
350
350
|
end
|
351
351
|
end
|
352
352
|
end # active record api
|
353
|
+
|
354
|
+
describe 'polymorphic association' do
|
355
|
+
it 'sets the parent on the associated record before saving' do
|
356
|
+
bob = Person.where(name: 'Bob').first
|
357
|
+
bob.addresses << Address.new(street: '221B Baker St.')
|
358
|
+
|
359
|
+
address = bob.addresses[0]
|
360
|
+
expect(address.addressable).to eq(bob)
|
361
|
+
expect(address.addressable_id).to eq(bob.id)
|
362
|
+
expect(address.addressable_type).to eq('Person')
|
363
|
+
end
|
364
|
+
|
365
|
+
it 'adds the associated record' do
|
366
|
+
bob = Person.where(name: 'Bob').first
|
367
|
+
bob.addresses << Address.new(street: '221B Baker St.')
|
368
|
+
bob.save!
|
369
|
+
|
370
|
+
bob.reload
|
371
|
+
address = bob.addresses[0]
|
372
|
+
expect(address.street).to eq('221B Baker St.')
|
373
|
+
end
|
374
|
+
|
375
|
+
context 'when using nested attributes' do
|
376
|
+
it 'sets the parent on the associated record before saving' do
|
377
|
+
bob = Person.where(name: 'Bob').first
|
378
|
+
bob.attributes = {
|
379
|
+
addresses_attributes: [{ street: '221B Baker St.' }]
|
380
|
+
}
|
381
|
+
|
382
|
+
address = bob.addresses[0]
|
383
|
+
expect(address.addressable).to eq(bob)
|
384
|
+
expect(address.addressable_id).to eq(bob.id)
|
385
|
+
expect(address.addressable_type).to eq('Person')
|
386
|
+
expect(address.street).to eq('221B Baker St.')
|
387
|
+
end
|
388
|
+
end
|
389
|
+
end
|
353
390
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -25,6 +25,13 @@ ActiveRecord::Schema.define version: 0 do
|
|
25
25
|
t.timestamps
|
26
26
|
end
|
27
27
|
|
28
|
+
create_table :addresses do |t|
|
29
|
+
t.integer :addressable_id
|
30
|
+
t.string :addressable_type
|
31
|
+
t.string :street
|
32
|
+
t.timestamps
|
33
|
+
end
|
34
|
+
|
28
35
|
create_table :non_validated_issues do |t|
|
29
36
|
t.string :subject
|
30
37
|
t.integer :person_id
|
@@ -19,6 +19,11 @@ class Person < ActiveRecord::Base
|
|
19
19
|
after_remove: :removed_issue,
|
20
20
|
validate: false
|
21
21
|
|
22
|
+
# Polymorphic has-many association
|
23
|
+
deferred_has_many :addresses, as: :addressable,
|
24
|
+
autosave: true,
|
25
|
+
dependent: :delete_all
|
26
|
+
deferred_accepts_nested_attributes_for :addresses, allow_destroy: true
|
22
27
|
|
23
28
|
validates_presence_of :name
|
24
29
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: deferring
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Robin Roestenburg
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-04-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -128,6 +128,7 @@ files:
|
|
128
128
|
- spec/lib/deferring_nested_attributes_spec.rb
|
129
129
|
- spec/spec_helper.rb
|
130
130
|
- spec/support/active_record.rb
|
131
|
+
- spec/support/models/address.rb
|
131
132
|
- spec/support/models/issue.rb
|
132
133
|
- spec/support/models/non_validated_issue.rb
|
133
134
|
- spec/support/models/person.rb
|
@@ -163,6 +164,7 @@ test_files:
|
|
163
164
|
- spec/lib/deferring_nested_attributes_spec.rb
|
164
165
|
- spec/spec_helper.rb
|
165
166
|
- spec/support/active_record.rb
|
167
|
+
- spec/support/models/address.rb
|
166
168
|
- spec/support/models/issue.rb
|
167
169
|
- spec/support/models/non_validated_issue.rb
|
168
170
|
- spec/support/models/person.rb
|