SimpleRestView v0.1.0 SimpleRestView View Source

Module takes care of creating concise views in Phoenix REST projects.

Module makes it possible to render nested maps, add custom fields and limit which fields to render within the same function.

Link to this section Summary

Functions

Wraps the result with pagination data obtained when using scrivener_ecto library Can limit which pagination fields to render with opt parameter.

Function renders all fields (except for virtual fields and associations) which exist on a given schema into a map that can be converted into json. It supports rendering fields of nested objects if specified with add optional parameter.

Wraps a map passed in as a parameter inside of map having a data field - containing map passed in as parameter

Link to this section Functions

Link to this function

render_paginated_wrapper(data, query_result, opt \\ [])

View Source

Wraps the result with pagination data obtained when using scrivener_ecto library Can limit which pagination fields to render with opt parameter.

Examples

iex> users = [
...>  %{id: 1, username: "joe"},
...>  %{id: 2, username: "mary"}
...> ]
iex> pagination_info = %Scrivener.Page{page_number: 1, page_size: 2, total_pages: 5, entries: ...}
iex> render_paginated_wrapper(users, pagination_info, except: [:total_entries])
%{data: [
      %{id: 1, username: "joe"},
      %{id: 2, username: "mary"}
    ],
  page_number: 1,
  page_size: 2,
  total_pages: 5}
Link to this function

render_schema(schema_ref, schema_map, opt \\ [])

View Source

Function renders all fields (except for virtual fields and associations) which exist on a given schema into a map that can be converted into json. It supports rendering fields of nested objects if specified with add optional parameter.

Optional parameters are:

many - true/false

only - [:field1, :field2]

except - [:field1, :field2]

include_timestamps - true/false

add -

  • [field_name: {SchemaReference, :field_on_schema_map}] OR
  • [field_name: {SchemaReference, :field_on_schema_map, opt}] OR
  • [field_name: (fn schema -> ... end)] OR
  • [field_name: %{custom_field: custom_val, ...}]

Examples

iex> user = %User{id: 1, username: "joe", email: "joe@mail.com", password: "passwordhash123"}
iex> render_schema(User, user)
%{id: 1, username: "joe", email: "joe@mail.com", password: "passwordhash123"}


iex> user = %User{
...>  id: 1,
...>  username: "joe",
...>  email: "joe@mail.com",
...>  password: "passwordhash123",
...>  reviewed: [%Review{...}, %Review{...}]
...> }
iex> render_schema(User, user,
...>    except: [:email, :password],
...>    add: [
...>        avg_rating: (fn user -> get_avg_rating(user.id)),
...>        reviewed: {Review, :reviewed, many: true, only: [:comment]}
...>      ]
...>    )
%{id: 1,
  username: "joe",
  avg_rating: 10,
  reviewed: [%{comment: "..."}, %{comment: "..."}]}


iex> user = %User{
...> id: 1,
...> username: "joe",
...> email: "joe@mail.com",
...> password: "passwordhash123",
...> reviewed: [
...>    %Review{id: 1, comment: "...", ... , reviewer: %User{username: "mary", ...}},
...>    %Review{id: 2, comment: "...", ... , reviewer: %User{username: "johnson", ...}}
...> ]}
iex> render_schema(User, user,
...>    except: [:email, :password],
...>    add: [
...>        avg_rating: (fn user -> get_avg_rating(user.id)),
...>        reviewed: {Review, :reviewed,
...>              many: true,
...>              only: [:comment],
...>              add: [
...>                 reviewed_by: {User, :reviewer, only: [:username]}
...>               ]}
...>      ])
  %{id: 1,
    username: "joe",
    avg_rating: 10,
    reviewed: [
      %{comment: "...", reviewed_by: %{username: "mary"}},
      %{comment: "...", reviewed_by: %{username: "johnson"}}
      ]
    }

Wraps a map passed in as a parameter inside of map having a data field - containing map passed in as parameter

Examples

iex> users = [
...>  %{id: 1, username: "joe"},
...>  %{id: 2, username: "mary"}
...> ]
iex> render_wrapper(users)
%{data: [
  %{id: 1, username: "joe"},
  %{id: 2, username: "mary"}
]}