Skip to content

Commit

Permalink
Merge pull request #29 from dwyl/dropdown
Browse files Browse the repository at this point in the history
Dropdown
  • Loading branch information
nelsonic authored Nov 27, 2018
2 parents a01b9a9 + 14a0ec2 commit acee0e7
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 24 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,11 @@ custom_render_autoform(conn, action, [{schema, opts}], options)
- `:exclude` - A list of any fields in your schema you don't want to display on the form
- `:update_field` - The field from your schema you want to use in your update path (/users/some-id), defaults to `id`
- `:assoc_query` - An ecto query you want to use when loading your associations

### Associations

`:many_to_many` associations are rendered as checkboxes.

`:belongs_to` associations are rendered as a `select` element.

If you don't want the associations to be rendered in your form, you can add them to your exclude list (see options `above`).
12 changes: 9 additions & 3 deletions lib/autoform.ex
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,11 @@ defmodule Autoform do
associations(
conn,
schema,
schema.changeset(struct(schema), %{}),
Keyword.get(
Keyword.get(options, :assigns, []),
:changeset,
schema.changeset(struct(schema), %{})
),
Keyword.update(options, :exclude, [], fn v -> v ++ excludes end)
),
schema_name: schema_name(schema),
Expand All @@ -174,7 +178,7 @@ defmodule Autoform do
Phoenix.View.render(
Autoform.CustomView,
"custom.html",
Keyword.get(options, :assigns, %{})
Keyword.get(options, :assigns, [])
|> Map.new()
|> Map.put_new(:changeset, first_schema.changeset(struct(first_schema), %{}))
|> Map.merge(%{
Expand Down Expand Up @@ -219,7 +223,8 @@ defmodule Autoform do
defp fields(schema, options) do
excludes = Keyword.get(options, :exclude, []) ++ unquote(@excludes)

schema.__schema__(:fields) |> Enum.reject(&(&1 in excludes))
schema.__schema__(:fields)
|> Enum.reject(&(&1 in excludes))
end

defp associations(conn, schema, changeset, options) do
Expand Down Expand Up @@ -247,6 +252,7 @@ defmodule Autoform do
end

%{
cardinality: Map.get(schema.__schema__(:association, a), :cardinality),
name: a,
associations:
Enum.map(
Expand Down
3 changes: 2 additions & 1 deletion lib/templates/autoform/form.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<% end %>
<%= for assoc <- @associations do %>
<div class="form-group <%= assoc[:name] %>-group">
<%= label f, assoc[:name], class: "control-label" %>
<%= label f, assoc[:name], class: "control-label #{if assoc[:name] in @required do "required" end}" %>
<%= for a <- assoc[:associations] do %>
<input
type="checkbox"
Expand All @@ -24,6 +24,7 @@
>
<label for="<%=@schema_name%>_<%=assoc[:name]%>_<%=String.to_atom(Map.get(a, :display))%>"><%= Map.get(a, :display) |> String.capitalize() %></label>
<% end %>
<%= error_tag f, assoc[:name] %>
</div>
<% end %>

Expand Down
47 changes: 27 additions & 20 deletions lib/templates/custom/custom.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,42 @@
<%= for field <- element.fields do %>
<div class="form-group">
<%= if Map.has_key?(element, :input_first) && Enum.any?(element.input_first, &(&1 == field)) do %>
<%= input f, field, element.schema_name, class: "form-control", required: (field in element.required) %>
<%= input f, field, element.schema_name, class: "form-control", required: (field in element.required), value: Map.get(@changeset.data, field) %>
<%= error_tag f, field %>
<%= label String.to_existing_atom(element.schema_name), field, class: "control-label #{if field in element.required do "required" end}" do %>
<% custom_label = Map.get(element.custom_labels, field) %>
<%= custom_label || humanize(field) %>
<% end %>
<%= label String.to_existing_atom(element.schema_name), field, class: "control-label #{if field in element.required do "required" end}" do %><%=Map.get(element.custom_labels, field, humanize(field))%><% end %>
<% else %>
<%= label String.to_existing_atom(element.schema_name), field, class: "control-label #{if field in element.required do "required" end}" do %>
<% custom_label = Map.get(element.custom_labels, field) %>
<%= custom_label || humanize(field) %>
<% end %>
<%= input f, field, element.schema_name, class: "form-control", required: (field in element.required) %>
<%= label String.to_existing_atom(element.schema_name), field, class: "control-label #{if field in element.required do "required" end}" do %><%=Map.get(element.custom_labels, field, humanize(field))%><% end %>
<%= input f, field, element.schema_name, class: "form-control", required: (field in element.required), value: Map.get(@changeset.data, field) %>
<%= error_tag f, field %>
<% end %>
</div>
<% end %>
<%= for assoc <- element.associations do %>
<div class="form-group <%= assoc[:name] %>-group">
<%= label String.to_existing_atom(element.schema_name), assoc[:name], class: "control-label" %>
<%= for a <- assoc[:associations] do %>
<input
type="checkbox"
id="<%=element.schema_name%>_<%=assoc[:name]%>_<%=String.split(Map.get(a, :display)) |> Enum.join |> Macro.underscore()%>"
name="<%=element.schema_name%>[<%=assoc[:name]%>][<%=String.to_atom(Map.get(a, :display))%>]"
class="form-control"
<%= if (Map.get(assoc.loaded_associations, assoc[:name]) || []) |> Enum.find(fn l -> Map.get(l, :name) == Map.get(a, :display) || Map.get(l, :type) == Map.get(a, :display) end) do "checked" end%>
>
<label for="<%=element.schema_name%>_<%=assoc[:name]%>_<%=String.to_atom(Map.get(a, :display))%>"><%= Map.get(a, :display) |> String.capitalize() %></label>
<%= label String.to_existing_atom(element.schema_name), assoc[:name], class: "control-label #{if assoc[:name] in element.required do "required" end}" %>
<%= if assoc[:cardinality] == :one do %>
<select name="<%=element.schema_name%>[<%=assoc[:name]%>]">
<%= for a <- assoc[:associations] do %>
<option value="<%=String.to_atom(Map.get(a, :display))%>"
<%= if (Map.get(assoc.loaded_associations, assoc[:name]) || %{}) |> Map.get(:name) == Map.get(a, :display) do "selected" end%>
>
<%= Map.get(a, :display) |> String.capitalize() %>
</option>
<% end %>
</select>
<%= else %>
<%= for a <- assoc[:associations] do %>
<input
type="checkbox"
id="<%=element.schema_name%>_<%=assoc[:name]%>_<%=String.split(Map.get(a, :display)) |> Enum.join |> Macro.underscore()%>"
name="<%=element.schema_name%>[<%=assoc[:name]%>][<%=String.to_atom(Map.get(a, :display))%>]"
class="form-control"
<%= if (Map.get(assoc.loaded_associations, assoc[:name]) || []) |> Enum.find(fn l -> Map.get(l, :name) == Map.get(a, :display) || Map.get(l, :type) == Map.get(a, :display) end) do "checked" end%>
>
<label for="<%=element.schema_name%>_<%=assoc[:name]%>_<%=String.to_atom(Map.get(a, :display))%>"><%= Map.get(a, :display) |> String.capitalize() %></label>
<% end %>
<% end %>
<%= error_tag f, assoc[:name] %>
</div>
<% end %>
<% end %>
Expand Down

0 comments on commit acee0e7

Please sign in to comment.