embedded_associations 4.1.0 → 4.1.1

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: 76c8b0e4ae9164c343480eb95de82cd07a83c8fe
4
- data.tar.gz: 04934eb299a9eb501f8f77a8c803bc7715d4925c
3
+ metadata.gz: f6d41192bb48a2704dbc2356512872935acac014
4
+ data.tar.gz: eee1b040f3c264e88ab493fca6f18a8c63231857
5
5
  SHA512:
6
- metadata.gz: 8fad90037a4e1e2528772398f851f55230a4f8559eb02d621566ac092c6c9a87509581e71d40756deef82dbc3183146c77095208622d7a75f26e49e630653eeb
7
- data.tar.gz: 074f845b30f457745ae7976bf8fccadea23e231d0006e8d325daa19418fece68ecdaa033e10e07f4ab875d2ee32438eff88e22849c700c08e6cae12a07f52413
6
+ metadata.gz: 3cc1b91d346d294d8368ba4a2e78724c47294072613896f0e5e4f60c571bcaf6520641bbd188c6d3ae80930ee9909d6e5e9136727d7bdccfbcb1fac37092eba1
7
+ data.tar.gz: 595ab5ab518d466eb4227d28c0d47781e588ee2b377fdd89c61d558dfa7c8f5d41095745902407d92c4cabbebe75e093aea4fb1565ff1d429d4cef6e8e038a85
@@ -123,7 +123,13 @@ module EmbeddedAssociations
123
123
  r.assign_attributes(attrs)
124
124
  run_before_update_callbacks(r)
125
125
  else
126
- r = current_assoc.build()
126
+ inheritance_column = parent.class.reflect_on_association(name).klass.inheritance_column
127
+ # need to pass in inheritance column in build to get correct class
128
+ r = if inheritance_column
129
+ current_assoc.build(attrs.slice(inheritance_column))
130
+ else
131
+ current_assoc.build()
132
+ end
127
133
  attrs = controller.send(:filter_attributes, r.class.name, attrs, :create)
128
134
  handle_resource(child_definition, r, attrs) if child_definition
129
135
  r.assign_attributes(attrs)
@@ -147,7 +153,13 @@ module EmbeddedAssociations
147
153
  r.mark_for_destruction
148
154
  end
149
155
  elsif attrs && attrs != ''
150
- r = parent.send("build_#{name}")
156
+ inheritance_column = parent.class.reflect_on_association(name).klass.inheritance_column
157
+ # need to pass in inheritance column in build to get correct class
158
+ r = if inheritance_column
159
+ parent.send("build_#{name}", attrs.slice(inheritance_column))
160
+ else
161
+ parent.send("build_#{name}")
162
+ end
151
163
  attrs = controller.send(:filter_attributes, r.class.name, attrs, :create)
152
164
  handle_resource(child_definition, r, attrs) if child_definition
153
165
  r.assign_attributes(attrs)
@@ -1,3 +1,3 @@
1
1
  module EmbeddedAssociations
2
- VERSION = "4.1.0"
2
+ VERSION = "4.1.1"
3
3
  end
@@ -169,6 +169,29 @@ describe PostsController, type: :controller do
169
169
  end
170
170
 
171
171
  end
172
+
173
+ context "creating with sti belongs_to" do
174
+
175
+ it "should create hierarchy" do
176
+ json = post :create, post: {
177
+ title: 'ma post',
178
+ user: {type: 'Admin', name: 'G$', account: {}}
179
+ }
180
+
181
+ parsed_body = JSON.parse(response.body)
182
+ expect(parsed_body['post']['user']['type']).to eq('Admin')
183
+
184
+ expect(Post.count).to eq(1)
185
+ expect(User.count).to eq(1)
186
+ expect(Account.count).to eq(1)
187
+
188
+ resource = Post.first
189
+
190
+ expect(resource.user).to_not be_nil
191
+ expect(resource.user.account).to_not be_nil
192
+ end
193
+
194
+ end
172
195
 
173
196
  context "updating" do
174
197
 
@@ -45,7 +45,7 @@ class PostsController < ApplicationController
45
45
  params.require(:post).permit(
46
46
  :title,
47
47
  comments: [:content, user: [:name, :email, account: [:note] ]],
48
- user: [:name, :email, account: [:note] ],
48
+ user: [:type, :name, :email, account: [:note] ],
49
49
  tags: [:name],
50
50
  category: [:name]
51
51
  )
@@ -0,0 +1,2 @@
1
+ class Admin < User
2
+ end
@@ -0,0 +1,3 @@
1
+ class AdminSerializer < UserSerializer
2
+ attributes :type
3
+ end
@@ -0,0 +1,5 @@
1
+ class CreateAdmins < ActiveRecord::Migration
2
+ def change
3
+ add_column :users, :type, :string
4
+ end
5
+ end
@@ -9,51 +9,52 @@
9
9
  # from scratch. The latter is a flawed and unsustainable approach (the more migrations
10
10
  # you'll amass, the slower it'll run and the greater likelihood for issues).
11
11
  #
12
- # It's strongly recommended to check this file into your version control system.
12
+ # It's strongly recommended that you check this file into your version control system.
13
13
 
14
- ActiveRecord::Schema.define(:version => 20130226001639) do
14
+ ActiveRecord::Schema.define(version: 20141022213956) do
15
15
 
16
- create_table "accounts", :force => true do |t|
16
+ create_table "accounts", force: true do |t|
17
17
  t.integer "user_id"
18
18
  t.string "note"
19
- t.datetime "created_at", :null => false
20
- t.datetime "updated_at", :null => false
19
+ t.datetime "created_at"
20
+ t.datetime "updated_at"
21
21
  end
22
22
 
23
- create_table "categories", :force => true do |t|
23
+ create_table "categories", force: true do |t|
24
24
  t.string "name"
25
- t.datetime "created_at", :null => false
26
- t.datetime "updated_at", :null => false
25
+ t.datetime "created_at"
26
+ t.datetime "updated_at"
27
27
  end
28
28
 
29
- create_table "comments", :force => true do |t|
29
+ create_table "comments", force: true do |t|
30
30
  t.integer "post_id"
31
31
  t.integer "user_id"
32
32
  t.string "content"
33
- t.datetime "created_at", :null => false
34
- t.datetime "updated_at", :null => false
33
+ t.datetime "created_at"
34
+ t.datetime "updated_at"
35
35
  end
36
36
 
37
- create_table "posts", :force => true do |t|
37
+ create_table "posts", force: true do |t|
38
38
  t.string "title"
39
39
  t.integer "user_id"
40
40
  t.integer "category_id"
41
- t.datetime "created_at", :null => false
42
- t.datetime "updated_at", :null => false
41
+ t.datetime "created_at"
42
+ t.datetime "updated_at"
43
43
  end
44
44
 
45
- create_table "tags", :force => true do |t|
45
+ create_table "tags", force: true do |t|
46
46
  t.integer "post_id"
47
47
  t.string "name"
48
- t.datetime "created_at", :null => false
49
- t.datetime "updated_at", :null => false
48
+ t.datetime "created_at"
49
+ t.datetime "updated_at"
50
50
  end
51
51
 
52
- create_table "users", :force => true do |t|
52
+ create_table "users", force: true do |t|
53
53
  t.string "name"
54
54
  t.string "email"
55
- t.datetime "created_at", :null => false
56
- t.datetime "updated_at", :null => false
55
+ t.datetime "created_at"
56
+ t.datetime "updated_at"
57
+ t.string "type"
57
58
  end
58
59
 
59
60
  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.0
4
+ version: 4.1.1
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: 2014-08-29 00:00:00.000000000 Z
11
+ date: 2014-10-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties
@@ -48,12 +48,14 @@ files:
48
48
  - spec/support/app/app/controllers/application_controller.rb
49
49
  - spec/support/app/app/controllers/posts_controller.rb
50
50
  - spec/support/app/app/models/account.rb
51
+ - spec/support/app/app/models/admin.rb
51
52
  - spec/support/app/app/models/category.rb
52
53
  - spec/support/app/app/models/comment.rb
53
54
  - spec/support/app/app/models/post.rb
54
55
  - spec/support/app/app/models/tag.rb
55
56
  - spec/support/app/app/models/user.rb
56
57
  - spec/support/app/app/serializers/account_serializer.rb
58
+ - spec/support/app/app/serializers/admin_serializer.rb
57
59
  - spec/support/app/app/serializers/category_serializer.rb
58
60
  - spec/support/app/app/serializers/comment_serializer.rb
59
61
  - spec/support/app/app/serializers/post_serializer.rb
@@ -81,6 +83,7 @@ files:
81
83
  - spec/support/app/db/migrate/20130225173717_create_accounts.rb
82
84
  - spec/support/app/db/migrate/20130226001629_create_tags.rb
83
85
  - spec/support/app/db/migrate/20130226001639_create_categories.rb
86
+ - spec/support/app/db/migrate/20141022213956_create_admins.rb
84
87
  - spec/support/app/db/schema.rb
85
88
  - spec/support/app/db/seeds.rb
86
89
  - spec/support/app/lib/assets/.gitkeep
@@ -119,12 +122,14 @@ test_files:
119
122
  - spec/support/app/app/controllers/application_controller.rb
120
123
  - spec/support/app/app/controllers/posts_controller.rb
121
124
  - spec/support/app/app/models/account.rb
125
+ - spec/support/app/app/models/admin.rb
122
126
  - spec/support/app/app/models/category.rb
123
127
  - spec/support/app/app/models/comment.rb
124
128
  - spec/support/app/app/models/post.rb
125
129
  - spec/support/app/app/models/tag.rb
126
130
  - spec/support/app/app/models/user.rb
127
131
  - spec/support/app/app/serializers/account_serializer.rb
132
+ - spec/support/app/app/serializers/admin_serializer.rb
128
133
  - spec/support/app/app/serializers/category_serializer.rb
129
134
  - spec/support/app/app/serializers/comment_serializer.rb
130
135
  - spec/support/app/app/serializers/post_serializer.rb
@@ -152,6 +157,7 @@ test_files:
152
157
  - spec/support/app/db/migrate/20130225173717_create_accounts.rb
153
158
  - spec/support/app/db/migrate/20130226001629_create_tags.rb
154
159
  - spec/support/app/db/migrate/20130226001639_create_categories.rb
160
+ - spec/support/app/db/migrate/20141022213956_create_admins.rb
155
161
  - spec/support/app/db/schema.rb
156
162
  - spec/support/app/db/seeds.rb
157
163
  - spec/support/app/lib/assets/.gitkeep