Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes #287 add ability for user to add label and type in theme_has_ma… #360

Open
wants to merge 1 commit into
base: phx-1.3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/ex_admin/themes/active_admin/form.ex
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,9 @@ defmodule ExAdmin.Theme.ActiveAdmin.Form do
end

for field <- fields do
f_name = field[:name]
required = if f_name in required_list, do: true, else: false
f_name = field[:opts][:label] || field[:name]
type = field[:opts][:type] || :text
required = if field[:name] in required_list, do: true, else: false
name = "#{base_name}[#{f_name}]"
errors = get_errors(errors, String.to_atom("#{field_field_name}_#{orig_inx}_#{f_name}"))
error = if errors in [nil, [], false], do: "", else: ".error"
Expand Down Expand Up @@ -234,7 +235,7 @@ defmodule ExAdmin.Theme.ActiveAdmin.Form do
required_abbr required
end
val = if res, do: [value: Map.get(res, f_name, "") |> escape_value], else: []
Xain.input([type: :text, maxlength: "255", id: "#{ext_name}_#{f_name}",
Xain.input([type: type, id: "#{ext_name}_#{f_name}",
name: name, required: true] ++ val)
build_errors(errors, field[:opts][:hint])
end
Expand Down
7 changes: 4 additions & 3 deletions lib/ex_admin/themes/admin_lte2/form.ex
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,9 @@ defmodule ExAdmin.Theme.AdminLte2.Form do
end

for field <- fields do
f_name = field[:name]
required = if f_name in required_list, do: true, else: false
f_name = field[:opts][:label] || field[:name]
type = field[:opts][:type] || :text
required = if field[:name] in required_list, do: true, else: false
name = "#{base_name}[#{f_name}]"
errors = get_errors(errors, String.to_atom("#{field_field_name}_#{orig_inx}_#{f_name}"))
error = if errors in [nil, [], false], do: "", else: ".has-error"
Expand Down Expand Up @@ -245,7 +246,7 @@ defmodule ExAdmin.Theme.AdminLte2.Form do
required_abbr required
end
div ".col-sm-10#{error}" do
Xain.input([type: :text, maxlength: "255", id: "#{ext_name}_#{f_name}",
Xain.input([type: type, id: "#{ext_name}_#{f_name}",
class: "form-control", name: name] ++ val)
build_errors(errors, field[:opts][:hint])
end
Expand Down
1 change: 1 addition & 0 deletions test/support/migrations.exs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ defmodule TestExAdmin.Migrations do
create table(:phone_numbers) do
add :number, :string
add :label, :string
add :contacted_on, :date
timestamps()
end

Expand Down
3 changes: 2 additions & 1 deletion test/support/schema.exs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ defmodule TestExAdmin.PhoneNumber do
schema "phone_numbers" do
field :number, :string
field :label, :string
field :contacted_on, :date
has_many :contacts_phone_numbers, TestExAdmin.ContactPhoneNumber
has_many :contacts, through: [:contacts_phone_numbers, :contact]
timestamps()
Expand All @@ -242,7 +243,7 @@ defmodule TestExAdmin.PhoneNumber do

def changeset(model, params \\ %{}) do
model
|> cast(params, @fields)
|> cast(params, @fields ++ [:contacted_on])
|> validate_required(@fields)
end

Expand Down
56 changes: 54 additions & 2 deletions test/themes/form_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,31 @@ defmodule ExAdmin.ThemeFormTest do

end

test "AdminLte2 theme_build_has_many_fieldset with labels", %{conn: conn} do
pn = Repo.insert! PhoneNumber.changeset(%PhoneNumber{}, %{label: "Home Phone", number: "5555555555"})
fields = build_fields(pn, %{label: "Telephone Number"})

{inx, html} = AdminLte2.Form.theme_build_has_many_fieldset(conn, pn, fields, 0, "contact_phone_numbers_attributes_0",
:phone_numbers, "phone_numbers_attributes", "contact", nil)

assert inx == 0

assert Floki.find(html, "div div label") |> Floki.text == "Telephone Number*Label*Remove"
end

test "AdminLte2 theme_build_has_many_fieldset with labels and types", %{conn: conn} do
pn = Repo.insert! PhoneNumber.changeset(%PhoneNumber{}, %{label: "Home Phone", number: "5555555555", contacted_on: Ecto.Date.utc})
fields = build_fields(pn, %{label: "Telephone Number"})
contacted_on = %{name: :contacted_on, opts: %{type: :date}, resource: pn, type: :input}

{inx, html} = AdminLte2.Form.theme_build_has_many_fieldset(conn, pn, [contacted_on | fields], 0, "contact_phone_numbers_attributes_0",
:phone_numbers, "phone_numbers_attributes", "contact", nil)

assert inx == 0

assert Floki.find(html, "div div input") |> Floki.attribute("type") == ["text", "date", "hidden", "checkbox"]
end

test "AdminLte2 theme_build_has_many_fieldset with errors", %{conn: conn} do
pn = %{_destroy: "0", label: "Primary Phone", number: nil}
fields = [%{name: :label,
Expand All @@ -44,6 +69,7 @@ defmodule ExAdmin.ThemeFormTest do

end


test "ActiveAdmin theme_build_has_many_fieldset", %{conn: conn} do
pn = Repo.insert! PhoneNumber.changeset(%PhoneNumber{}, %{label: "Home Phone", number: "5555555555"})
fields = build_fields pn
Expand All @@ -55,18 +81,44 @@ defmodule ExAdmin.ThemeFormTest do

assert Floki.find(html, "fieldset ol h3") |> Floki.text == "Phone Number"
end

test "ActiveAdmin theme_build_has_many_fieldset with labels", %{conn: conn} do
pn = Repo.insert! PhoneNumber.changeset(%PhoneNumber{}, %{label: "Home Phone", number: "5555555555"})
fields = build_fields(pn, %{label: "Telephone Number"})

{inx, html} = ActiveAdmin.Form.theme_build_has_many_fieldset(conn, pn, fields, 0, "contact_phone_numbers_attributes_0",
:phone_numbers, "phone_numbers_attributes", "contact", nil)

assert inx == 0


assert Floki.find(html, "label") |> Floki.text == "RemoveLabel*Telephone Number*"
end

test "ActiveAdmin theme_build_has_many_fieldset with labels and types", %{conn: conn} do
pn = Repo.insert! PhoneNumber.changeset(%PhoneNumber{}, %{label: "Home Phone", number: "5555555555", contacted_on: Ecto.Date.utc})
fields = build_fields(pn, %{label: "Telephone Number"})
contacted_on = %{name: :contacted_on, opts: %{type: :date}, resource: pn, type: :input}

{inx, html} = ActiveAdmin.Form.theme_build_has_many_fieldset(conn, pn, [contacted_on | fields], 0, "contact_phone_numbers_attributes_0",
:phone_numbers, "phone_numbers_attributes", "contact", nil)


assert inx == 0

assert Floki.find(html, "input") |> Floki.attribute("type") == ["hidden", "checkbox", "date", "text"]
end

################
# Helpers

defp build_fields(resource) do
defp build_fields(resource, opts \\ %{}) do
[
%{
name: :label, resource: resource, type: :input,
opts: %{collection: PhoneNumber.labels},
},
%{name: :number, opts: %{}, resource: resource, type: :input}
%{name: :number, opts: opts, resource: resource, type: :input}
]
end
end