embedded_associations 4.1.1 → 4.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +2 -2
- data/lib/embedded_associations.rb +7 -4
- data/lib/embedded_associations/version.rb +1 -1
- data/spec/embedded_associations_spec.rb +30 -1
- data/spec/support/app/app/controllers/posts_controller.rb +11 -5
- data/spec/support/app/app/models/tag.rb +2 -0
- data/spec/support/app/config/environments/development.rb +0 -3
- data/spec/support/app/config/environments/test.rb +0 -3
- data/spec/support/serialization_helpers.rb +2 -2
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a3effa8457a2ccc4e44fdefbc2ad1ef44fd4089e
|
4
|
+
data.tar.gz: f9d66cb68fa8531fef3ec954266b6d85a56a09f2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9ea8022101eec7659c39b09fdef7a8cc27192347f97a0c5e91176a1cdc2904846dca0f7c8383d8efb60bbfdf4809adb40358abe6129bd3c1bede626c3c66c649
|
7
|
+
data.tar.gz: 28f019c864eedb7f1580c3d817290cccc04cac5b2a2c7d37383c6f26ed4c8ee73f0fcbdd5aaf959a4201b64bb0495de80f1b26e0c073bf25512e8613ff31009e
|
data/Gemfile
CHANGED
@@ -3,9 +3,9 @@ source 'https://rubygems.org'
|
|
3
3
|
gemspec
|
4
4
|
|
5
5
|
group :test do
|
6
|
-
gem 'rails', '4.
|
6
|
+
gem 'rails', '~> 4.2'
|
7
7
|
gem 'sqlite3'
|
8
|
-
gem 'active_model_serializers'
|
8
|
+
gem 'active_model_serializers', git: 'https://github.com/getoutreach/active_model_serializers.git'
|
9
9
|
|
10
10
|
gem 'rspec'
|
11
11
|
gem 'rspec-rails'
|
@@ -118,10 +118,13 @@ module EmbeddedAssociations
|
|
118
118
|
if id = attrs['id']
|
119
119
|
# can't use current_assoc.find(id), see http://stackoverflow.com/questions/11605120/autosave-ignored-on-has-many-relation-what-am-i-missing
|
120
120
|
r = current_assoc.find{|r| r.id == id.to_i}
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
121
|
+
|
122
|
+
if r
|
123
|
+
attrs = controller.send(:filter_attributes, r.class.name, attrs, :update)
|
124
|
+
handle_resource(child_definition, r, attrs) if child_definition
|
125
|
+
r.assign_attributes(attrs)
|
126
|
+
run_before_update_callbacks(r)
|
127
|
+
end
|
125
128
|
else
|
126
129
|
inheritance_column = parent.class.reflect_on_association(name).klass.inheritance_column
|
127
130
|
# need to pass in inheritance column in build to get correct class
|
@@ -31,6 +31,7 @@ describe PostsController, type: :controller do
|
|
31
31
|
hash[:tags] += [{},{}]
|
32
32
|
json = post :update, :id => resource.id, post: hash
|
33
33
|
|
34
|
+
expect(json.response_code).to eq(200)
|
34
35
|
expect(Post.count).to eq(1)
|
35
36
|
expect(Tag.count).to eq(4)
|
36
37
|
|
@@ -41,6 +42,7 @@ describe PostsController, type: :controller do
|
|
41
42
|
hash[:tags] = hash[:tags].take(1)
|
42
43
|
json = post :update, :id => resource.id, post: hash
|
43
44
|
|
45
|
+
expect(json.response_code).to eq(200)
|
44
46
|
expect(Post.count).to eq(1)
|
45
47
|
expect(Tag.count).to eq(1)
|
46
48
|
|
@@ -51,6 +53,7 @@ describe PostsController, type: :controller do
|
|
51
53
|
hash.delete(:tags)
|
52
54
|
json = post :update, :id => resource.id, post: hash
|
53
55
|
|
56
|
+
expect(json.response_code).to eq(200)
|
54
57
|
expect(Post.count).to eq(1)
|
55
58
|
expect(Tag.count).to eq(2)
|
56
59
|
|
@@ -61,6 +64,7 @@ describe PostsController, type: :controller do
|
|
61
64
|
hash[:tags].first[:name] = 'modified'
|
62
65
|
json = post :update, :id => resource.id, post: hash
|
63
66
|
|
67
|
+
expect(json.response_code).to eq(200)
|
64
68
|
expect(Post.count).to eq(1)
|
65
69
|
expect(Tag.count).to eq(2)
|
66
70
|
|
@@ -69,8 +73,33 @@ describe PostsController, type: :controller do
|
|
69
73
|
Tag.all.each{ |t| expect(t.post).to_not be_nil }
|
70
74
|
end
|
71
75
|
|
72
|
-
|
76
|
+
it "should ignore missing child records" do
|
77
|
+
hash[:tags].first[:name] = "modified"
|
78
|
+
hash[:tags] += [{ id: 0, name: "foo" }]
|
79
|
+
json = post :update, :id => resource.id, post: hash
|
80
|
+
|
81
|
+
expect(json.response_code).to eq(200)
|
82
|
+
expect(Post.count).to eq(1)
|
83
|
+
expect(Tag.count).to eq(2)
|
84
|
+
|
85
|
+
expect(Tag.first.name).to eq("modified")
|
86
|
+
end
|
73
87
|
|
88
|
+
context "with a named tag and a uniqueness constraint" do
|
89
|
+
let(:tags) {[ Tag.create(name: "foo") ]}
|
90
|
+
|
91
|
+
it "should require an ID to supply existing records" do
|
92
|
+
json = post :update, id: resource.id, post: hash.merge(tags: [{ name: "foo" }])
|
93
|
+
expect(json.response_code).to eq(422)
|
94
|
+
|
95
|
+
json = post :update, id: resource.id, post: hash.merge(tags: [{ id: tags[0].id, name: "foo" }])
|
96
|
+
expect(json.response_code).to eq(200)
|
97
|
+
expect(Post.count).to eq(1)
|
98
|
+
expect(Tag.count).to eq(1)
|
99
|
+
expect(Tag.first.name).to eq("foo")
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
74
103
|
end
|
75
104
|
|
76
105
|
describe "embedded belongs_to" do
|
@@ -11,15 +11,21 @@ class PostsController < ApplicationController
|
|
11
11
|
def create
|
12
12
|
params = post_params
|
13
13
|
handle_embedded_associations(resource, params)
|
14
|
-
resource.update_attributes(params)
|
15
|
-
|
14
|
+
if resource.update_attributes(params)
|
15
|
+
render json: resource
|
16
|
+
else
|
17
|
+
render json: {}, status: 422
|
18
|
+
end
|
16
19
|
end
|
17
20
|
|
18
21
|
def update
|
19
22
|
params = post_params
|
20
23
|
handle_embedded_associations(resource, params)
|
21
|
-
resource.update_attributes(params)
|
22
|
-
|
24
|
+
if resource.update_attributes(params)
|
25
|
+
render json: resource
|
26
|
+
else
|
27
|
+
render json: {}, status: 422
|
28
|
+
end
|
23
29
|
end
|
24
30
|
|
25
31
|
def destroy
|
@@ -46,7 +52,7 @@ class PostsController < ApplicationController
|
|
46
52
|
:title,
|
47
53
|
comments: [:content, user: [:name, :email, account: [:note] ]],
|
48
54
|
user: [:type, :name, :email, account: [:note] ],
|
49
|
-
tags: [:name],
|
55
|
+
tags: [:id, :name],
|
50
56
|
category: [:name]
|
51
57
|
)
|
52
58
|
end
|
@@ -22,9 +22,6 @@ App::Application.configure do
|
|
22
22
|
# Only use best-standards-support built into browsers
|
23
23
|
config.action_dispatch.best_standards_support = :builtin
|
24
24
|
|
25
|
-
# Raise exception on mass assignment protection for Active Record models
|
26
|
-
config.active_record.mass_assignment_sanitizer = :strict
|
27
|
-
|
28
25
|
# Log the query plan for queries taking more than this (works
|
29
26
|
# with SQLite, MySQL, and PostgreSQL)
|
30
27
|
config.active_record.auto_explain_threshold_in_seconds = 0.5
|
@@ -29,9 +29,6 @@ App::Application.configure do
|
|
29
29
|
# ActionMailer::Base.deliveries array.
|
30
30
|
config.action_mailer.delivery_method = :test
|
31
31
|
|
32
|
-
# Raise exception on mass assignment protection for Active Record models
|
33
|
-
config.active_record.mass_assignment_sanitizer = :strict
|
34
|
-
|
35
32
|
# Print deprecation notices to the stderr
|
36
33
|
config.active_support.deprecation = :stderr
|
37
34
|
end
|
@@ -6,7 +6,7 @@ module SerializationHelpers
|
|
6
6
|
def serialize(model, root=false)
|
7
7
|
options = {}
|
8
8
|
options[:root] = false unless root
|
9
|
-
params = model.
|
9
|
+
params = ActiveModel::Serializer.serializer_for(model).new(model, options).as_json
|
10
10
|
params[:id] = model.id if root
|
11
11
|
params
|
12
12
|
end
|
@@ -15,4 +15,4 @@ module SerializationHelpers
|
|
15
15
|
arr.map(&method(:serialize))
|
16
16
|
end
|
17
17
|
|
18
|
-
end
|
18
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: embedded_associations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 4.1.
|
4
|
+
version: 4.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gordon L. Hempton
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-05-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: railties
|
@@ -110,7 +110,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
110
110
|
version: '0'
|
111
111
|
requirements: []
|
112
112
|
rubyforge_project:
|
113
|
-
rubygems_version: 2.
|
113
|
+
rubygems_version: 2.5.2
|
114
114
|
signing_key:
|
115
115
|
specification_version: 4
|
116
116
|
summary: ActiveRecord controller-level support for embedded associations
|