In the first case I want to return the associations in the response.
In the calls to render(conn, :index, criteria: criteria) and render(conn, :show, criterion: criterion), I was hoping to catch the difference of the output using when guards:
def data(%Criterion{} = criterion)
when is_map(criterion.program) and is_map(criterion.variant) do
%{
id: criterion.id,
code: criterion.code,
name: criterion.name,
...
program_id: criterion.program_id,
program: CockpitWeb.ProgramJSON.data(criterion.program),
variant_id: criterion.variant_id,
variant: CockpitWeb.VariantJSON.data(criterion.variant)
}
end
def data(%Criterion{} = criterion) do
%{
id: criterion.id,
code: criterion.code,
name: criterion.name,
...
program_id: criterion.program_id,
variant_id: criterion.variant_id
}
end