Skip to content

Commit

Permalink
Merge pull request #37 from dwyl/change-submit-button-txt
Browse files Browse the repository at this point in the history
Change submit button txt
  • Loading branch information
nelsonic authored Dec 15, 2018
2 parents 0fbc998 + 9efc08c commit 521a35d
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 6 deletions.
9 changes: 9 additions & 0 deletions lib/autoform.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ defmodule Autoform do
* `: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
* `:btn_txt` - A string that will be used in the form submission button
"""
@spec render_autoform(
Expand Down Expand Up @@ -74,6 +75,7 @@ defmodule Autoform do

assigns =
assigns
|> submit_btn_txt(options)
|> Enum.into(%{})
|> Map.put_new(:changeset, schema.changeset(struct(schema), %{}))
|> Map.put(:action, path(conn, action, schema, options))
Expand Down Expand Up @@ -115,6 +117,7 @@ defmodule Autoform do
* `: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
* `:path` - The endpoint you want your form to post to. Defaults using the calling modules schema name plus :update_field. If you are rendering multiple schemas, you will almost certainly need to use this field
* `:btn_txt` - A string that will be used in the form submission button
"""
@spec custom_render_autoform(
Expand Down Expand Up @@ -180,6 +183,7 @@ defmodule Autoform do
Autoform.CustomView,
"custom.html",
Keyword.get(options, :assigns, [])
|> submit_btn_txt(options)
|> Map.new()
|> Map.put_new(:changeset, first_schema.changeset(struct(first_schema), %{}))
|> (fn m ->
Expand Down Expand Up @@ -276,6 +280,11 @@ defmodule Autoform do
defp schema_name(schema) do
schema |> to_string() |> String.downcase() |> String.split(".") |> List.last()
end

defp submit_btn_txt(assigns, options) do
btn_txt_str = Keyword.get(options, :btn_txt, "Save")
Keyword.put_new(assigns, :btn_txt, btn_txt_str)
end
end
end
end
8 changes: 4 additions & 4 deletions lib/templates/autoform/form.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
<div class="form-group <%= assoc[:name] %>-group">
<%= label f, assoc[:name], class: "control-label #{if assoc[:name] in @required do "required" end}" %>
<%= for a <- assoc[:associations] do %>
<input
type="checkbox"
<input
type="checkbox"
id="<%=@schema_name%>_<%=assoc[:name]%>_<%=String.split(Map.get(a, :display)) |> Enum.join |> Macro.underscore()%>"
name="<%=@schema_name%>[<%=assoc[:name]%>][<%=String.to_atom(Map.get(a, :display))%>]"
name="<%=@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%>
>
Expand All @@ -29,6 +29,6 @@
<% end %>

<div class="form-group">
<%= submit "Save", class: "btn btn-primary" %>
<%= submit @btn_txt, class: "btn btn-primary" %>
</div>
<% end %>
2 changes: 1 addition & 1 deletion lib/templates/custom/custom.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@
<% end %>

<div class="form-group">
<%= submit "Save", class: "btn btn-primary" %>
<%= submit @btn_txt, class: "btn btn-primary" %>
</div>
<% end %>
6 changes: 6 additions & 0 deletions test/autoform_view_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,11 @@ defmodule AutoformViewTest do
refute h =~ "for=\"address_line_1\""
assert t =~ "for=\"address_line_1\""
end

test "Submit button text says Create", %{conn: conn} do
response = conn |> get(address_path(conn, :new)) |> html_response(200)

assert response =~ "Create"
end
end
end
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<h1>Addresses</h1>

<%= render_autoform @conn, :create, TestAutoform.Address, assigns: [changeset: @changeset], exclude: [:line_1] %>
<%= render_autoform @conn, :create, TestAutoform.Address, assigns: [changeset: @changeset], exclude: [:line_1], btn_txt: "Create" %>

0 comments on commit 521a35d

Please sign in to comment.