Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 0.10.6 regression; make using belongs_to on self opt-in #2218

Merged
merged 2 commits into from
Nov 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions docs/general/configuration_options.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ still prefer the render option `:key_transform` over this setting.
application, setting `config.key_transform` to `:unaltered` will provide a performance boost.*

##### default_includes

What relationships to serialize by default. Default: `'*'`, which includes one level of related
objects. See [includes](adapters.md#included) for more info.

Expand Down Expand Up @@ -162,6 +163,21 @@ Default: `{}`.

*Used when `jsonapi_include_toplevel_object` is `true`*

##### jsonapi_use_foreign_key_on_belongs_to_relationship

When true, the relationship will determine its resource object identifier
without calling the association or its serializer. This can be useful when calling
the association object is triggering unnecessary queries.

For example, if a `comment` belongs to a `post`, and the comment
uses the foreign key `post_id`, we can determine the resource object
identifier `id` as `comment.post_id` and the `type` from the association options.
Or quite simply, it behaves as `belongs_to :post, type: :posts, foreign_key: :post_id`.

Note: This option has *no effect* on polymorphic associations as we cannot reliably
determine the associated object's type without instantiating it.

Default: `false`.

## Hooks

Expand Down
1 change: 1 addition & 0 deletions lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ def config.array_serializer
# Make JSON API top-level jsonapi member opt-in
# ref: http://jsonapi.org/format/#document-top-level
config.jsonapi_include_toplevel_object = false
config.jsonapi_use_foreign_key_on_belongs_to_relationship = false
config.include_data_default = true

# For configuring how serializers are found.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ def meta_for(association)
end

def belongs_to_id_on_self?(association)
association.belongs_to? &&
parent_serializer.config.jsonapi_use_foreign_key_on_belongs_to_relationship &&
association.belongs_to? &&
parent_serializer.object.respond_to?(association.reflection.foreign_key)
end
end
Expand Down
18 changes: 16 additions & 2 deletions test/serializers/associations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,14 @@ def blog_id
end
end

actual = serializable(post, adapter: :json_api, serializer: BelongsToBlogModelSerializer).as_json
actual =
begin
original_option = BelongsToBlogModelSerializer.config.jsonapi_use_foreign_key_on_belongs_to_relationship
BelongsToBlogModelSerializer.config.jsonapi_use_foreign_key_on_belongs_to_relationship = true
serializable(post, adapter: :json_api, serializer: BelongsToBlogModelSerializer).as_json
ensure
BelongsToBlogModelSerializer.config.jsonapi_use_foreign_key_on_belongs_to_relationship = original_option
end
expected = { data: { id: '1', type: 'posts', relationships: { blog: { data: { id: '5', type: 'blogs' } } } } }

assert_equal expected, actual
Expand Down Expand Up @@ -189,7 +196,14 @@ def test_belongs_to_allows_id_overwriting
}
post = BelongsToExternalBlogModel.new(attributes)

actual = serializable(post, adapter: :json_api, serializer: BelongsToExternalBlogModelSerializer).as_json
actual =
begin
original_option = BelongsToExternalBlogModelSerializer.config.jsonapi_use_foreign_key_on_belongs_to_relationship
BelongsToExternalBlogModelSerializer.config.jsonapi_use_foreign_key_on_belongs_to_relationship = true
serializable(post, adapter: :json_api, serializer: BelongsToExternalBlogModelSerializer).as_json
ensure
BelongsToExternalBlogModelSerializer.config.jsonapi_use_foreign_key_on_belongs_to_relationship = original_option
end
expected = { data: { id: '1', type: 'posts', relationships: { :'external-blog' => { data: { id: '6', type: 'external-blogs' } } } } }

assert_equal expected, actual
Expand Down