ja_serializer v0.18.1 JaSerializer.Params View Source

Functions to help when working with json api params.

Link to this section Summary

Functions

Takes the entire params passed in, and merges relationships and attributes.

Link to this section Functions

Takes the entire params passed in, and merges relationships and attributes.

Note, this expects to receive the json api "data" param.

Example functionality:

JaSerializer.Params.to_attributes(%{
  "type" => "person",
  "attributes" => %{"first" => "Jane", "last" => "Doe", "type" => "anon"},
  "relationships" => %{"user" => %{"data" => %{"id" => 1}}}
})
%{
  "first" => "Jane",
  "last" => "Doe",
  "type" => "anon",
  "user_id" => 1
}

Example usage:

def create(conn, %{"data" => data}) do
  %Comment{}
  |> Comment.changeset(create_params(data))
  |> Repo.insert
  |> case do
    {:ok, my_model} ->
      # etc
    {:error, changeset} -> etc
      # etc
  end
end

defp create_params(data) do
  data
  |> JaSerializer.Params.to_attributes
  |> Map.take(["name", "body", "post_id"])
end