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

Resource Identifier metadata #206

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 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
20 changes: 19 additions & 1 deletion lib/ja_serializer/builder/resource_identifier.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ defmodule JaSerializer.Builder.ResourceIdentifier do
defp do_build(data, context, definition) do
%__MODULE__{
type: find_type(data, context, definition),
id: find_id(data, context, definition)
id: find_id(data, context, definition),
meta: get_meta(data, context, definition)
}
end

Expand All @@ -48,4 +49,21 @@ defmodule JaSerializer.Builder.ResourceIdentifier do
end
end

defp get_meta(
destination_data,
%{data: source_data, serializer: serializer, conn: conn},
%{identifier_meta: identifier_metadata_name})
when is_atom(identifier_metadata_name) do
apply(serializer, identifier_metadata_name, [source_data, destination_data, conn])
end

defp get_meta(
destination_data,
%{data: source_data, conn: conn},
%{identifer_meta: identifier_metadata_func})
when is_function(identifier_metadata_func) do
identifier_metadata_func.(source_data, destination_data, conn)
Copy link
Contributor

@alanpeabody alanpeabody Dec 19, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, does this work? I have had some trouble getting functions working when passed to the DSL.

end

defp get_meta(_destination_data, _context, _definition), do: nil
end
7 changes: 6 additions & 1 deletion lib/ja_serializer/formatter/resource_identifier.ex
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
defimpl JaSerializer.Formatter, for: JaSerializer.Builder.ResourceIdentifier do
def format(resource) do
%{
required_attributes = %{
"id" => to_string(resource.id),
"type" => resource.type
}

add_meta(required_attributes, resource)
end

defp add_meta(resource, %{meta: nil}), do: resource
defp add_meta(resource, %{meta: meta}), do: Map.put(resource, "meta", meta)
end
90 changes: 49 additions & 41 deletions lib/ja_serializer/relationship.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,15 @@ defmodule JaSerializer.Relationship do

The fields are:

* `serializer` - A Serializer (often a PhoenixView) implementing the JaSerializer.Serializer behaviour.
* `include` - Should this relationship be included (sideloaded) by default. Overriden by `include` opt to JaSerializer.format/4
* `data` - A list of structs representing the data.
* `identifiers` - Should "resource identifiers be included, options are `:when_included` and `:always`. Defaults to `:when_included`
* `links` - A keyword list of links, `self` and `related` are most common.
* `name` - Name of the relationship, automatically set.
* `serializer` - A Serializer (often a PhoenixView) implementing the JaSerializer.Serializer behaviour.
* `include` - Should this relationship be included (sideloaded) by default. Overriden by `include` opt to JaSerializer.format/4
* `data` - A list of structs representing the data.
* `identifiers` - Should "resource identifiers be included, options are `:when_included` and `:always`. Defaults to `:when_included`
* `links` - A keyword list of links, `self` and `related` are most common.
* `name` - Name of the relationship, automatically set.
* `identifier_meta` - An atom referring to a function on the serializer or a function literal with arity 3.
If specified, it is called with the source record, the destination record, and the conn.


Used when defining relationships without the DSL using the
JaSerializer.relationships/2 callback. For example:
Expand All @@ -57,25 +60,27 @@ defmodule JaSerializer.Relationship do
of relationships.
"""
defstruct [
links: [],
type: nil,
serializer: nil,
include: false,
data: nil,
identifiers: :when_included,
name: nil
links: [],
type: nil,
serializer: nil,
include: false,
data: nil,
identifiers: :when_included,
name: nil,
identifier_meta: nil
]

@doc false
def from_dsl(name, dsl_opts) do
%__MODULE__{
links: dsl_opts[:links] || [],
type: dsl_opts[:type],
serializer: dsl_opts[:serializer],
include: dsl_opts[:include],
data: dsl_opts[:data] || name,
identifiers: dsl_opts[:identifiers] || :when_included,
name: name
links: dsl_opts[:links] || [],
type: dsl_opts[:type],
serializer: dsl_opts[:serializer],
include: dsl_opts[:include],
data: dsl_opts[:data] || name,
identifiers: dsl_opts[:identifiers] || :when_included,
identifier_meta: dsl_opts[:identifier_meta] || nil,
name: name
}
end
end
Expand All @@ -86,13 +91,14 @@ defmodule JaSerializer.Relationship do

The fields are:

* `serializer` - A Serializer (often a PhoenixView) implementing the JaSerializer.Serializer behaviour.
* `include` - Should this relationship be included (sideloaded) by default. Overriden by `include` opt to JaSerializer.format/4
* `data` - A struct representing the data for serialization.
* `identifiers` - Should "resource identifiers be included, options are `:when_included` and `:always`. Defaults to `:when_included`
* `links` - A keyword list of links, `self` and `related` are most common.
* `name` - Name of the relationship, automatically set.

* `serializer` - A Serializer (often a PhoenixView) implementing the JaSerializer.Serializer behaviour.
* `include` - Should this relationship be included (sideloaded) by default. Overriden by `include` opt to JaSerializer.format/4
* `data` - A struct representing the data for serialization.
* `identifiers` - Should "resource identifiers be included, options are `:when_included` and `:always`. Defaults to `:when_included`
* `links` - A keyword list of links, `self` and `related` are most common.
* `name` - Name of the relationship, automatically set.
* `identifier_meta` - An atom referring to a function on the serializer or a function literal with arity 3.
If specified, it is called with the source record, the destination record, and the conn.

Used when defining relationships without the DSL using the
JaSerializer.relationships/2 callback. For example:
Expand All @@ -111,25 +117,27 @@ defmodule JaSerializer.Relationship do
of relationships.
"""
defstruct [
links: [],
type: nil,
serializer: nil,
include: false,
data: nil,
identifiers: :always,
name: nil
links: [],
type: nil,
serializer: nil,
include: false,
data: nil,
identifiers: :always,
name: nil,
identifier_meta: nil
]

@doc false
def from_dsl(name, dsl_opts) do
%__MODULE__{
links: dsl_opts[:links] || [],
type: dsl_opts[:type],
serializer: dsl_opts[:serializer],
include: dsl_opts[:include],
data: dsl_opts[:data] || name,
identifiers: dsl_opts[:identifiers] || :always,
name: name
links: dsl_opts[:links] || [],
type: dsl_opts[:type],
serializer: dsl_opts[:serializer],
include: dsl_opts[:include],
data: dsl_opts[:data] || name,
identifiers: dsl_opts[:identifiers] || :always,
identifier_meta: dsl_opts[:identifier_meta] || nil,
name: name
}
end
end
Expand Down