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 +4 -4
- data/README.md +14 -0
- data/lib/nested_attribute_reassignable.rb +13 -2
- data/lib/nested_attribute_reassignable/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ca262e237c92c19f98aa14f5130ab052ed640779
|
4
|
+
data.tar.gz: 8de6a7ad83c263f570a0400de578f3bc0502bcdc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
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
|
-
|
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)
|
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.
|
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-
|
11
|
+
date: 2016-09-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|