embedded_associations 0.0.4 → 4.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.travis.yml +2 -2
- data/Gemfile +3 -2
- data/README.md +36 -3
- data/embedded_associations.gemspec +2 -2
- data/lib/embedded_associations.rb +10 -17
- data/lib/embedded_associations/version.rb +1 -1
- data/spec/embedded_associations_spec.rb +10 -6
- data/spec/support/app/app/controllers/posts_controller.rb +16 -2
- data/spec/support/app/app/models/account.rb +0 -1
- data/spec/support/app/app/models/category.rb +0 -1
- data/spec/support/app/app/models/comment.rb +0 -2
- data/spec/support/app/app/models/post.rb +0 -2
- data/spec/support/app/app/models/tag.rb +0 -1
- data/spec/support/app/app/models/user.rb +0 -2
- data/spec/support/app/config/application.rb +0 -6
- metadata +12 -17
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 931459993427be60c6717d86ee0e387148f34b66
|
4
|
+
data.tar.gz: e73417bff91288ec6b76e3fafa61a92ea74e10ec
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 77b9526fdaff3aed1d35ab8a4d7d0e6ea6c2229ec4698c0a41c6fd79bb46d87a400d2c66a1898e177a8705ab94a1e94d79f17449e385b0a38da98f08118daf35
|
7
|
+
data.tar.gz: 38372d705068862c455f2142a459f093719360af9b8771ccf3597bd1e47e34132dd7af80c53e9ad338efee4df55f4031f9418eebc57041db40e6798d0547c567
|
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -30,6 +30,18 @@ class PostsController < ApplicationController
|
|
30
30
|
end
|
31
31
|
```
|
32
32
|
|
33
|
+
Inside of your controllers actions, the `handle_embedded_associations` method should be called:
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
def create
|
37
|
+
params = params.require(:post).permit(:title, tags: [:name], comments: [:title, user: [:username]])
|
38
|
+
post = Post.new
|
39
|
+
handle_embedded_associations(post, params)
|
40
|
+
post.update_attributes(params)
|
41
|
+
render json: post
|
42
|
+
end
|
43
|
+
```
|
44
|
+
|
33
45
|
Behind the scenes, this will make the controller parse out sub-params passed in and perform the necessary ActiveRecord model manipulation. E.g., consider the params hash below:
|
34
46
|
|
35
47
|
```ruby
|
@@ -47,9 +59,7 @@ Based on the declared `embedded_association`s, the controller will manipulate th
|
|
47
59
|
|
48
60
|
### Controller and Model Pre-Requisites
|
49
61
|
|
50
|
-
|
51
|
-
|
52
|
-
When defining the relationships in the model, it is also important to set the `autosave` and `dependent` options on the association:
|
62
|
+
When defining the relationships in the model, it is important to set the `autosave` and `dependent` options on the association:
|
53
63
|
|
54
64
|
```ruby
|
55
65
|
class Post < ActiveRecord::Base
|
@@ -58,6 +68,29 @@ class Post < ActiveRecord::Base
|
|
58
68
|
end
|
59
69
|
```
|
60
70
|
|
71
|
+
### Strong Parameters
|
72
|
+
|
73
|
+
As might be expected, embedded associations must have their attributes permitted via the strong parameters api. For instance, a controller with the following embedded associates configuration:
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
embedded_association :comments => {:user => :account}
|
77
|
+
embedded_association :user => [:account]
|
78
|
+
embedded_association [:tags]
|
79
|
+
embedded_association :category
|
80
|
+
```
|
81
|
+
|
82
|
+
The attributes of the embedded associations must be explicitly permitted:
|
83
|
+
|
84
|
+
```ruby
|
85
|
+
params.require(:post).permit(
|
86
|
+
:title,
|
87
|
+
comments: [:content, user: [:name, :email, account: [:note] ]],
|
88
|
+
user: [:name, :email, account: [:note] ],
|
89
|
+
tags: [:name],
|
90
|
+
category: [:name]
|
91
|
+
)
|
92
|
+
```
|
93
|
+
|
61
94
|
## Contributing
|
62
95
|
|
63
96
|
1. Fork it
|
@@ -10,12 +10,12 @@ Gem::Specification.new do |gem|
|
|
10
10
|
gem.email = ["ghempton@gmail.com"]
|
11
11
|
gem.description = %q{ActiveRecord controller-level support for embedded associations}
|
12
12
|
gem.summary = %q{ActiveRecord controller-level support for embedded associations}
|
13
|
-
gem.homepage = "https://github.com/
|
13
|
+
gem.homepage = "https://github.com/getoutreach/embedded_associations"
|
14
14
|
|
15
15
|
gem.files = `git ls-files`.split($/)
|
16
16
|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
17
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
18
|
gem.require_paths = ["lib"]
|
19
19
|
|
20
|
-
gem.add_dependency "railties", ">
|
20
|
+
gem.add_dependency "railties", "> 4.0.0"
|
21
21
|
end
|
@@ -11,23 +11,14 @@ module EmbeddedAssociations
|
|
11
11
|
def self.embedded_association(definition)
|
12
12
|
unless embedded_associations
|
13
13
|
self.embedded_associations = Definitions.new
|
14
|
-
before_filter :handle_embedded_associations, only: [:update, :create, :destroy]
|
15
14
|
end
|
16
15
|
self.embedded_associations = embedded_associations.add_definition(definition)
|
17
16
|
end
|
18
17
|
end
|
19
18
|
end
|
20
19
|
|
21
|
-
def handle_embedded_associations
|
22
|
-
Processor.new(embedded_associations, self).run
|
23
|
-
end
|
24
|
-
|
25
|
-
def root_resource
|
26
|
-
resource
|
27
|
-
end
|
28
|
-
|
29
|
-
def root_resource_name
|
30
|
-
resource_name
|
20
|
+
def handle_embedded_associations(resource, params)
|
21
|
+
Processor.new(embedded_associations, self, resource, params).run
|
31
22
|
end
|
32
23
|
|
33
24
|
def filter_attributes(name, attrs, action)
|
@@ -67,15 +58,19 @@ module EmbeddedAssociations
|
|
67
58
|
|
68
59
|
attr_reader :definitions
|
69
60
|
attr_reader :controller
|
61
|
+
attr_reader :resource
|
62
|
+
attr_reader :params
|
70
63
|
|
71
|
-
def initialize(definitions, controller)
|
64
|
+
def initialize(definitions, controller, resource, params)
|
72
65
|
@definitions = definitions
|
73
66
|
@controller = controller
|
67
|
+
@params = params
|
68
|
+
@resource = resource
|
74
69
|
end
|
75
70
|
|
76
71
|
def run
|
77
72
|
definitions.each do |definition|
|
78
|
-
handle_resource(definition,
|
73
|
+
handle_resource(definition, resource, params)
|
79
74
|
end
|
80
75
|
end
|
81
76
|
|
@@ -94,7 +89,7 @@ module EmbeddedAssociations
|
|
94
89
|
definition.each do |name, child_definition|
|
95
90
|
reflection = parent.class.reflect_on_association(name)
|
96
91
|
attrs = parent_params && parent_params.delete(name.to_s)
|
97
|
-
|
92
|
+
|
98
93
|
if reflection.collection?
|
99
94
|
attrs ||= []
|
100
95
|
handle_plural_resource parent, name, attrs, child_definition
|
@@ -115,7 +110,6 @@ module EmbeddedAssociations
|
|
115
110
|
end
|
116
111
|
|
117
112
|
attr_array.each do |attrs|
|
118
|
-
attrs = ActionController::Parameters.new(attrs)
|
119
113
|
if id = attrs['id']
|
120
114
|
# can't use current_assoc.find(id), see http://stackoverflow.com/questions/11605120/autosave-ignored-on-has-many-relation-what-am-i-missing
|
121
115
|
r = current_assoc.find{|r| r.id == id.to_i}
|
@@ -135,8 +129,7 @@ module EmbeddedAssociations
|
|
135
129
|
|
136
130
|
def handle_singular_resource(parent, name, attrs, child_definition)
|
137
131
|
current_assoc = parent.send(name)
|
138
|
-
|
139
|
-
|
132
|
+
|
140
133
|
if r = current_assoc
|
141
134
|
if attrs
|
142
135
|
attrs = controller.send(:filter_attributes, r.class.name, attrs, :update)
|
@@ -9,6 +9,7 @@ describe PostsController, type: :controller do
|
|
9
9
|
|
10
10
|
it "should create child records" do
|
11
11
|
json = post :create, post: {
|
12
|
+
title: 'ma post',
|
12
13
|
tags: [{},{}]
|
13
14
|
}
|
14
15
|
|
@@ -23,7 +24,7 @@ describe PostsController, type: :controller do
|
|
23
24
|
context "updating" do
|
24
25
|
|
25
26
|
let(:tags) {[Tag.create, Tag.create]}
|
26
|
-
let(:resource) { Post.create(
|
27
|
+
let(:resource) { Post.create(tags: tags) }
|
27
28
|
let(:hash) { serialize(resource) }
|
28
29
|
|
29
30
|
it "should create new child records" do
|
@@ -68,6 +69,7 @@ describe PostsController, type: :controller do
|
|
68
69
|
|
69
70
|
it "should create child record" do
|
70
71
|
json = post :create, post: {
|
72
|
+
title: 'ma post',
|
71
73
|
category: {name: 'ember-data'}
|
72
74
|
}
|
73
75
|
|
@@ -100,7 +102,7 @@ describe PostsController, type: :controller do
|
|
100
102
|
|
101
103
|
context do
|
102
104
|
|
103
|
-
let(:resource) { Post.create(
|
105
|
+
let(:resource) { Post.create(category: Category.create(name: 'ember')) }
|
104
106
|
|
105
107
|
it "should destroy nil child record" do
|
106
108
|
hash[:category] = nil
|
@@ -134,6 +136,7 @@ describe PostsController, type: :controller do
|
|
134
136
|
|
135
137
|
it "should create hierarchy" do
|
136
138
|
json = post :create, post: {
|
139
|
+
title: 'ma post',
|
137
140
|
user: {name: 'G$', account: {}}
|
138
141
|
}
|
139
142
|
|
@@ -169,7 +172,7 @@ describe PostsController, type: :controller do
|
|
169
172
|
|
170
173
|
context do
|
171
174
|
|
172
|
-
let(:resource) { Post.create({user: User.create({name: 'G$', account: Account.create}
|
175
|
+
let(:resource) { Post.create({user: User.create({name: 'G$', account: Account.create})}) }
|
173
176
|
|
174
177
|
it "should destroy nil child hierarchy" do
|
175
178
|
hash[:user] = nil
|
@@ -227,8 +230,9 @@ describe PostsController, type: :controller do
|
|
227
230
|
|
228
231
|
context "creating" do
|
229
232
|
|
230
|
-
it "should create hierarchy" do
|
233
|
+
it "should create hierarchy", focus: true do
|
231
234
|
json = post :create, post: {
|
235
|
+
title: 'ma post',
|
232
236
|
comments: [{user: {name: 'G$', account: {}}}]
|
233
237
|
}
|
234
238
|
|
@@ -271,7 +275,7 @@ describe PostsController, type: :controller do
|
|
271
275
|
let(:resource) {
|
272
276
|
p = Post.create
|
273
277
|
c = p.comments.create
|
274
|
-
u = c.create_user({account: Account.create}
|
278
|
+
u = c.create_user({account: Account.create})
|
275
279
|
c.save
|
276
280
|
p
|
277
281
|
}
|
@@ -321,4 +325,4 @@ describe PostsController, type: :controller do
|
|
321
325
|
|
322
326
|
end
|
323
327
|
|
324
|
-
end
|
328
|
+
end
|
@@ -9,12 +9,16 @@ class PostsController < ApplicationController
|
|
9
9
|
attr_accessor :resource
|
10
10
|
|
11
11
|
def create
|
12
|
-
|
12
|
+
params = post_params
|
13
|
+
handle_embedded_associations(resource, params)
|
14
|
+
resource.update_attributes(params)
|
13
15
|
render json: resource
|
14
16
|
end
|
15
17
|
|
16
18
|
def update
|
17
|
-
|
19
|
+
params = post_params
|
20
|
+
handle_embedded_associations(resource, params)
|
21
|
+
resource.update_attributes(params)
|
18
22
|
render json: resource
|
19
23
|
end
|
20
24
|
|
@@ -37,4 +41,14 @@ class PostsController < ApplicationController
|
|
37
41
|
'post'
|
38
42
|
end
|
39
43
|
|
44
|
+
def post_params
|
45
|
+
params.require(:post).permit(
|
46
|
+
:title,
|
47
|
+
comments: [:content, user: [:name, :email, account: [:note] ]],
|
48
|
+
user: [:name, :email, account: [:note] ],
|
49
|
+
tags: [:name],
|
50
|
+
category: [:name]
|
51
|
+
)
|
52
|
+
end
|
53
|
+
|
40
54
|
end
|
@@ -47,12 +47,6 @@ module App
|
|
47
47
|
# like if you have constraints or database-specific column types
|
48
48
|
# config.active_record.schema_format = :sql
|
49
49
|
|
50
|
-
# Enforce whitelist mode for mass assignment.
|
51
|
-
# This will create an empty whitelist of attributes available for mass-assignment for all models
|
52
|
-
# in your app. As such, your models will need to explicitly whitelist or blacklist accessible
|
53
|
-
# parameters by using an attr_accessible or attr_protected declaration.
|
54
|
-
config.active_record.whitelist_attributes = true
|
55
|
-
|
56
50
|
# Enable the asset pipeline
|
57
51
|
config.assets.enabled = true
|
58
52
|
|
metadata
CHANGED
@@ -1,32 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: embedded_associations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
5
|
-
prerelease:
|
4
|
+
version: 4.0.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Gordon L. Hempton
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2014-03-11 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: railties
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
|
-
- -
|
17
|
+
- - '>'
|
20
18
|
- !ruby/object:Gem::Version
|
21
|
-
version:
|
19
|
+
version: 4.0.0
|
22
20
|
type: :runtime
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
|
-
- -
|
24
|
+
- - '>'
|
28
25
|
- !ruby/object:Gem::Version
|
29
|
-
version:
|
26
|
+
version: 4.0.0
|
30
27
|
description: ActiveRecord controller-level support for embedded associations
|
31
28
|
email:
|
32
29
|
- ghempton@gmail.com
|
@@ -91,29 +88,28 @@ files:
|
|
91
88
|
- spec/support/app/log/.gitkeep
|
92
89
|
- spec/support/app/script/rails
|
93
90
|
- spec/support/serialization_helpers.rb
|
94
|
-
homepage: https://github.com/
|
91
|
+
homepage: https://github.com/getoutreach/embedded_associations
|
95
92
|
licenses: []
|
93
|
+
metadata: {}
|
96
94
|
post_install_message:
|
97
95
|
rdoc_options: []
|
98
96
|
require_paths:
|
99
97
|
- lib
|
100
98
|
required_ruby_version: !ruby/object:Gem::Requirement
|
101
|
-
none: false
|
102
99
|
requirements:
|
103
|
-
- -
|
100
|
+
- - '>='
|
104
101
|
- !ruby/object:Gem::Version
|
105
102
|
version: '0'
|
106
103
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
107
|
-
none: false
|
108
104
|
requirements:
|
109
|
-
- -
|
105
|
+
- - '>='
|
110
106
|
- !ruby/object:Gem::Version
|
111
107
|
version: '0'
|
112
108
|
requirements: []
|
113
109
|
rubyforge_project:
|
114
|
-
rubygems_version:
|
110
|
+
rubygems_version: 2.0.14
|
115
111
|
signing_key:
|
116
|
-
specification_version:
|
112
|
+
specification_version: 4
|
117
113
|
summary: ActiveRecord controller-level support for embedded associations
|
118
114
|
test_files:
|
119
115
|
- spec/embedded_associations_spec.rb
|
@@ -163,4 +159,3 @@ test_files:
|
|
163
159
|
- spec/support/app/log/.gitkeep
|
164
160
|
- spec/support/app/script/rails
|
165
161
|
- spec/support/serialization_helpers.rb
|
166
|
-
has_rdoc:
|