-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor JsonApi adapter in order to avoid recomputation of hashes fo…
…r included resources.
- Loading branch information
Showing
5 changed files
with
210 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
lib/active_model/serializer/adapter/json_api/json_api_object.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
module ActiveModel | ||
class Serializer | ||
module Adapter | ||
class JsonApi | ||
module JsonApiObject | ||
ActiveModel::Serializer.config.jsonapi_version = '1.0' | ||
ActiveModel::Serializer.config.jsonapi_toplevel_meta = {} | ||
# Make JSON API top-level jsonapi member opt-in | ||
# ref: http://jsonapi.org/format/#document-top-level | ||
ActiveModel::Serializer.config.jsonapi_include_toplevel_object = false | ||
|
||
module_function | ||
|
||
def add!(hash) | ||
hash.merge!(object) if include_object? | ||
end | ||
|
||
def include_object? | ||
ActiveModel::Serializer.config.jsonapi_include_toplevel_object | ||
end | ||
|
||
# TODO: see if we can cache this | ||
def object | ||
object = { | ||
jsonapi: { | ||
version: ActiveModel::Serializer.config.jsonapi_version, | ||
meta: ActiveModel::Serializer.config.jsonapi_toplevel_meta | ||
} | ||
} | ||
object[:jsonapi].reject! { |_, v| v.blank? } | ||
|
||
object | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
27 changes: 27 additions & 0 deletions
27
lib/active_model/serializer/adapter/json_api/relationship.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
module ActiveModel | ||
class Serializer | ||
module Adapter | ||
class JsonApi | ||
class Relationship | ||
# NOTE(beauby): Currently only `data` is used. | ||
attr_accessor :data, :meta, :links | ||
|
||
def initialize(hash = {}) | ||
hash.each { |k, v| send("#{k}=", v) } | ||
end | ||
|
||
def to_h | ||
data_hash = | ||
if data.is_a?(Array) | ||
data.map { |ri| ri.respond_to?(:to_h) ? ri.to_h : ri } | ||
elsif data | ||
data.respond_to?(:to_h) ? data.to_h : data | ||
end | ||
|
||
{ data: data_hash } | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
module ActiveModel | ||
class Serializer | ||
module Adapter | ||
class JsonApi | ||
Resource = Struct.new(:identifier, :attributes, :relationships, :links) do | ||
def to_h | ||
hash = identifier.to_h | ||
hash[:attributes] = attributes if attributes.any? | ||
hash[:relationships] = Hash[relationships.map { |k, v| [k, v.to_h] }] if relationships.any? | ||
hash[:links] = links if links.any? | ||
|
||
hash | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.