deferring 0.1.2 → 0.1.3

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: 063adf3aca91473f0ca98264db2447d2e1811bdf
4
- data.tar.gz: 2ad2362263132b43e35bdf4c64abc8e15b109821
3
+ metadata.gz: a3f9cfb39bdad6c3d5e0c4f9e98e338dabe64b1c
4
+ data.tar.gz: 9555e0057cc0930aebdb32de04b4303bc2aa35a5
5
5
  SHA512:
6
- metadata.gz: e4e49d6d106f758823de3516ce6fc5e3f93511f9e5c0161174a4bd8573eb1ae508570449490267a4e4e7aee49d9da0d8734f632ba1d03b9c5e965600a54c0ab6
7
- data.tar.gz: 03dcc4ef9d7c2f4ed0cc0bb156dea1c4cb7b5551bfc70b6c0bb1a8756f5d792458942d375c7c363072968d138ab63e74c1d3b7c1d790577ef3538cb3d2f9094f
6
+ metadata.gz: 4913b6ef30424be10cf8ba5255ec0c86a502adddc687970d2f986d9f01fc9219c491650e8bf10d8b5d1737b2cb7283edd79e908125e01f5c59c0dc1e4c1d9a1b
7
+ data.tar.gz: 92f38c9128d3549e3c38eb933f88f9f7c89ea124b679941e2c21fc470d2e768a27b8b828f578e0f6af78e24052da2fcc0bce3aee9d44bc34b62594c7efb6ca65
@@ -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
@@ -1,5 +1,5 @@
1
1
  # encoding: UTF-8
2
2
 
3
3
  module Deferring
4
- VERSION = '0.1.2'
4
+ VERSION = '0.1.3'
5
5
  end
@@ -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
@@ -3,6 +3,7 @@ require 'deferring'
3
3
  require 'support/models/person'
4
4
  require 'support/models/team'
5
5
  require 'support/models/issue'
6
+ require 'support/models/address'
6
7
  require 'support/models/non_validated_issue'
7
8
  require 'support/rails_versions'
8
9
 
@@ -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
@@ -0,0 +1,5 @@
1
+ class Address < ActiveRecord::Base
2
+
3
+ belongs_to :addressable, polymorphic: true
4
+
5
+ end
@@ -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.2
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-01-20 00:00:00.000000000 Z
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