nested_attribute_reassignable 0.6.2 → 0.6.4

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: 1bda179cf854b25b013cadbbb7838bf11572c4f7
4
- data.tar.gz: 8b4afb437325e23f618343374d58f9c8a7c57257
3
+ metadata.gz: ca262e237c92c19f98aa14f5130ab052ed640779
4
+ data.tar.gz: 8de6a7ad83c263f570a0400de578f3bc0502bcdc
5
5
  SHA512:
6
- metadata.gz: 6e0c05e1fce17388ebf90640b94f5785e78c93bcf96f76e8344f27c724bbc093a78fafc6faef9a267f9455da52b2afdced51a243014e518ba284dcd194ed664d
7
- data.tar.gz: ea6998aa4c6bca31fb52ce89ecd3f97390e6a26e0bc9e6915ace85314de280acd30aaf5bf5d688e3179ff8bc1d3940fec9623a2f31650b9b0dfcff7d57bc4d72
6
+ metadata.gz: aa177283b04f1598a8e0dba06b37c62f2b446c808cca7cb86e8781312d83eb4d453ab0e3506903f0db7294a926ee03f3c06709c80d1bec217abd6a2f6987792b
7
+ data.tar.gz: 60c2b434cad91e80d244f077f3fa95b884c68e1fa3d45667eccf94fcc1be7e109c87b96de19d69ef92d99e0413099b6263d41597e70a1b34da9c9edf0cb95229
data/README.md CHANGED
@@ -105,6 +105,20 @@ Normal `accepts_nested_attributes_for` accepts a `_destroy` parameter
105
105
  for destroying the association. This will destroy the underlying record.
106
106
  If you only want to disassociate the record, you can now use `_delete`.
107
107
 
108
+ ### nonexistent_id
109
+
110
+ If an `id` or `lookup_key` is passed, and a record with this identifier
111
+ does not exist, this library will raise an error by default.
112
+
113
+ You may not want this behavior, for instance if your API is accepting
114
+ client-generated IDs on POSTs to `create`. In this scenario:
115
+
116
+ ```ruby
117
+ belongs_to :thing, nonexistent_id: :create
118
+ ```
119
+
120
+ Instead of raising, this will create a record with the given ID.
121
+
108
122
  ### Running the tests
109
123
 
110
124
  This library uses [appraisal](https://github.com/thoughtbot/appraisal) to test against activerecord >= 4.1. Run
@@ -55,6 +55,7 @@ It is invalid to create a new '#{@relation}' relation when one already exists, a
55
55
  def reassignable_nested_attributes_for(association_name, *args)
56
56
  options = args.extract_options!.symbolize_keys
57
57
  options.update({ :allow_destroy => true })
58
+ nonexistent_id = options.delete(:nonexistent_id) || :raise
58
59
 
59
60
  accepts_nested_attributes_for(association_name, options.except(:lookup_key))
60
61
 
@@ -100,7 +101,12 @@ It is invalid to create a new '#{@relation}' relation when one already exists, a
100
101
  self.send(association_name).concat(existing_record)
101
102
  end
102
103
  else
103
- raise_nested_attributes_record_not_found!(association_name, id_attributes[:id])
104
+ if nonexistent_id == :create
105
+ new_record = association_klass.new(lookup_key => id_attributes[:id])
106
+ self.send(association_name).concat(new_record)
107
+ else
108
+ raise_nested_attributes_record_not_found!(association_name, id_attributes[:id])
109
+ end
104
110
  end
105
111
  end
106
112
  non_id_attribute_sets = attributes.reject { |a| a.has_key?(:id) }
@@ -119,7 +125,12 @@ It is invalid to create a new '#{@relation}' relation when one already exists, a
119
125
  existing_record.assign_attributes(attributes)
120
126
  self.send("#{association_name}=", existing_record)
121
127
  else
122
- raise_nested_attributes_record_not_found!(association_name, attributes[:id])
128
+ if nonexistent_id == :create
129
+ new_record = association_klass.new(lookup_key => attributes[:id])
130
+ self.send("#{association_name}=", new_record)
131
+ else
132
+ raise_nested_attributes_record_not_found!(association_name, attributes[:id])
133
+ end
123
134
  end
124
135
  else
125
136
  reflection = self.class._reflect_on_association(association_name)
@@ -1,3 +1,3 @@
1
1
  module NestedAttributeReassignable
2
- VERSION = "0.6.2"
2
+ VERSION = "0.6.4"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nested_attribute_reassignable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.2
4
+ version: 0.6.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lee Richmond
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-09-22 00:00:00.000000000 Z
11
+ date: 2016-09-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord