Skip to content

Relationship serializer class option #94

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

Closed
wants to merge 3 commits into from
Closed
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
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ This library is up-to-date with the finalized v1 JSON API spec.
* [Compound documents and includes](#compound-documents-and-includes)
* [Relationship path handling](#relationship-path-handling)
* [Control links and data in relationships](#control-links-and-data-in-relationships)
* [Relationship serializer](#relationship-serializer)
* [Rails example](#rails-example)
* [Sinatra example](#sinatra-example)
* [Unfinished business](#unfinished-business)
Expand Down Expand Up @@ -641,6 +642,16 @@ Notice that linkage data is now included for the `author` relationship:
}
```

### Relationship serializer

By default the relationship serializer class will be looked up in the same namespace.
To specify a custom serializer outside that namespace you can add a `serializer` option
to the declaration

```ruby
has_one :author, serializer: MyOtherNamespace::Author
```

## Rails example

```ruby
Expand Down
11 changes: 7 additions & 4 deletions lib/jsonapi-serializers/attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ module ClassMethods
attr_accessor :to_one_associations
attr_accessor :to_many_associations

RELATIONSHIP_DEFAULT_OPTIONS = {
include_links: true,
include_data: false,
}.freeze

def attribute(name, options = {}, &block)
add_attribute(name, options, &block)
end
Expand Down Expand Up @@ -52,8 +57,7 @@ def add_attribute(name, options = {}, &block)
private :add_attribute

def add_to_one_association(name, options = {}, &block)
options[:include_links] = options.fetch(:include_links, true)
options[:include_data] = options.fetch(:include_data, false)
options = RELATIONSHIP_DEFAULT_OPTIONS.merge(options)
@to_one_associations ||= {}
@to_one_associations[name] = {
attr_or_block: block_given? ? block : name,
Expand All @@ -63,8 +67,7 @@ def add_to_one_association(name, options = {}, &block)
private :add_to_one_association

def add_to_many_association(name, options = {}, &block)
options[:include_links] = options.fetch(:include_links, true)
options[:include_data] = options.fetch(:include_data, false)
options = RELATIONSHIP_DEFAULT_OPTIONS.merge(options)
@to_many_associations ||= {}
@to_many_associations[name] = {
attr_or_block: block_given? ? block : name,
Expand Down
6 changes: 4 additions & 2 deletions lib/jsonapi-serializers/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ def relationships
# http://jsonapi.org/format/#document-structure-resource-relationships
data[formatted_attribute_name]['data'] = nil
else
related_object_serializer = JSONAPI::Serializer.find_serializer(object, @options)
related_object_serializer = attr_data[:options][:serializer] ||
JSONAPI::Serializer.find_serializer(object, @options)
data[formatted_attribute_name]['data'] = {
'type' => related_object_serializer.type.to_s,
'id' => related_object_serializer.id.to_s,
Expand Down Expand Up @@ -151,7 +152,8 @@ def relationships
data[formatted_attribute_name]['data'] = []
objects = has_many_relationship(attribute_name, attr_data) || []
objects.each do |obj|
related_object_serializer = JSONAPI::Serializer.find_serializer(obj, @options)
related_object_serializer = attr_data[:options][:serializer] ||
JSONAPI::Serializer.find_serializer(obj, @options)
data[formatted_attribute_name]['data'] << {
'type' => related_object_serializer.type.to_s,
'id' => related_object_serializer.id.to_s,
Expand Down
24 changes: 24 additions & 0 deletions spec/serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,30 @@ def serialize_primary(object, options = {})
}
})
end

it 'allows to set custom serializer for relationships' do
long_comments = create_list(:long_comment, 2)
post = create(:post, :with_author, long_comments: long_comments)
primary_data = serialize_primary(post, { serializer: MyApp::PostSerializerWithCustomRelationshipSerializer })
expect(primary_data).to eq({
'id' => '1',
'type' => 'posts',
'attributes' => {
'title' => 'Title for Post 1'
},
'links' => {
'self' => '/posts/1',
},
'relationships' => {
'author' => {
'links' => {
'self' => '/posts/1/relationships/author',
'related' => '/posts/1/author'
},
},
}
})
end
end

# The members data and errors MUST NOT coexist in the same document.
Expand Down
14 changes: 14 additions & 0 deletions spec/support/serializers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,20 @@ def type
end
end

module MyAppOtherNamespace
class AuthorSerializer
include JSONAPI::Serializer
end
end

class PostSerializerWithCustomRelationshipSerializer
include JSONAPI::Serializer

attribute :title

has_one :author, serializer: MyAppOtherNamespace::AuthorSerializer
end

class PostSerializerWithContext < PostSerializer
attribute :body, if: :show_body?, unless: :hide_body?

Expand Down