Skip to content

Commit

Permalink
Merge pull request #376 from kleinernik/master
Browse files Browse the repository at this point in the history
Support Elixir.DateTime, NaiveDateTime and Elixir.Date and Time types
  • Loading branch information
smpallen99 authored Oct 19, 2017
2 parents dc19704 + d64ec11 commit 53429b0
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 3 deletions.
19 changes: 18 additions & 1 deletion lib/ex_admin/form.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1011,6 +1011,14 @@ defmodule ExAdmin.Form do
%{name: model_name, model: resource, id: model_name, class: "form-control"}
|> datetime_select(field_name, Map.get(opts, :options, []))
end
def build_control(DateTime, resource, opts, model_name, field_name, _ext_name) do
%{name: model_name, model: resource, id: model_name, class: "form-control"}
|> datetime_select(field_name, Map.get(opts, :options, []))
end
def build_control(NaiveDateTime, resource, opts, model_name, field_name, _ext_name) do
%{name: model_name, model: resource, id: model_name, class: "form-control"}
|> datetime_select(field_name, Map.get(opts, :options, []))
end
def build_control(Ecto.Date, resource, opts, model_name, field_name, _ext_name) do
%{name: model_name, model: resource, id: model_name, class: "form-control"}
|> date_select(field_name, Map.get(opts, :options, []))
Expand All @@ -1019,6 +1027,14 @@ defmodule ExAdmin.Form do
%{name: model_name, model: resource, id: model_name, class: "form-control"}
|> time_select(field_name, Map.get(opts, :options, []))
end
def build_control(Date, resource, opts, model_name, field_name, _ext_name) do
%{name: model_name, model: resource, id: model_name, class: "form-control"}
|> date_select(field_name, Map.get(opts, :options, []))
end
def build_control(Time, resource, opts, model_name, field_name, _ext_name) do
%{name: model_name, model: resource, id: model_name, class: "form-control"}
|> time_select(field_name, Map.get(opts, :options, []))
end

def build_control(:text, resource, opts, model_name, field_name, ext_name) do
value = Map.get(resource, field_name, "") |> escape_value
Expand Down Expand Up @@ -1221,6 +1237,8 @@ defmodule ExAdmin.Form do
do: %{hour: hour, min: min, sec: Map.get(map, "sec", 0), usec: Map.get(map, "usec", 0)}
defp time_value(%{hour: hour, min: min} = map),
do: %{hour: hour, min: min, sec: Map.get(map, :sec, 0), usec: Map.get(map, :usec, 0)}
defp time_value(%{hour: hour, minute: min} = map),
do: %{hour: hour, min: min, sec: Map.get(map, :second, 0), usec: elem(Map.get(map, :microsecond, {0,0}),0)}

defp time_value({_, {hour, min, sec, usec}}),
do: %{hour: hour, min: min, sec: sec, usec: usec}
Expand All @@ -1230,7 +1248,6 @@ defmodule ExAdmin.Form do
do: %{hour: hour, min: min, sec: sec, usec: nil}
defp time_value({hour, min, sec}),
do: %{hour: hour, min: min, sec: sec, usec: nil}

defp time_value(nil),
do: %{hour: nil, min: nil, sec: nil, usec: nil}
defp time_value(other),
Expand Down
20 changes: 20 additions & 0 deletions lib/ex_admin/table.ex
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,16 @@ defmodule ExAdmin.Table do
text to_string(dt)
end
end
def handle_contents(%DateTime{} = dt, field_name) do
td class: to_class("td-", field_name) do
text to_string(dt)
end
end
def handle_contents(%NaiveDateTime{} = dt, field_name) do
td class: to_class("td-", field_name) do
text to_string(dt)
end
end
def handle_contents(%Ecto.Time{} = dt, field_name) do
td class: to_class("td-", field_name) do
text to_string(dt)
Expand All @@ -206,6 +216,16 @@ defmodule ExAdmin.Table do
text to_string(dt)
end
end
def handle_contents(%Time{} = dt, field_name) do
td class: to_class("td-", field_name) do
text to_string(dt)
end
end
def handle_contents(%Date{} = dt, field_name) do
td class: to_class("td-", field_name) do
text to_string(dt)
end
end
def handle_contents(%{}, _field_name), do: []
def handle_contents(contents, field_name) when is_binary(contents) do
td(to_class(".td-", field_name)) do
Expand Down
2 changes: 1 addition & 1 deletion lib/ex_admin/themes/active_admin/filter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ defmodule ExAdmin.Theme.ActiveAdmin.Filter do
end
end

def build_field({name, type}, q, defn) when type in [Ecto.DateTime, Ecto.Date, Ecto.Time, Timex.Ecto.DateTime, Timex.Ecto.Date, Timex.Ecto.Time, Timex.Ecto.DateTimeWithTimezone, NaiveDateTime, :naive_datetime] do
def build_field({name, type}, q, defn) when type in [Ecto.DateTime, Ecto.Date, Ecto.Time, Timex.Ecto.DateTime, Timex.Ecto.Date, Timex.Ecto.Time, Timex.Ecto.DateTimeWithTimezone, NaiveDateTime, :naive_datetime, DateTime, :utx_datetime] do
name_label = field_label(name, defn)
gte_value = get_value("#{name}_gte", q)
lte_value = get_value("#{name}_lte", q)
Expand Down
2 changes: 1 addition & 1 deletion lib/ex_admin/themes/admin_lte2/filter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ defmodule ExAdmin.Theme.AdminLte2.Filter do
end
end

def build_field({name, type}, q, defn) when type in [Ecto.DateTime, Ecto.Date, Ecto.Time, Timex.Ecto.DateTime, Timex.Ecto.Date, Timex.Ecto.Time, Timex.Ecto.DateTimeWithTimezone, NaiveDateTime, :naive_datetime] do
def build_field({name, type}, q, defn) when type in [Ecto.DateTime, Ecto.Date, Ecto.Time, Timex.Ecto.DateTime, Timex.Ecto.Date, Timex.Ecto.Time, Timex.Ecto.DateTimeWithTimezone, NaiveDateTime, :naive_datetime, DateTime, :utc_datetime] do
gte_value = get_value("#{name}_gte", q)
lte_value = get_value("#{name}_lte", q)
name_label = field_label(name, defn)
Expand Down
13 changes: 13 additions & 0 deletions test/form_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ defmodule ExAdmin.FormTest do
refute options == []
end

test "build_control NativeDateTime" do
res = ExAdmin.Form.build_control(DateTime, %Simple{inserted_at: DateTime.utc_now}, %{}, "simple", :inserted_at, "simple_inserted_at")
select = Floki.find(res, "select[name='simple[inserted_at][year]']")
refute select == []
options = Floki.find(res, "select[name='simple[inserted_at][year]'] option")
refute options == []

select = Floki.find(res, "select[name='simple[inserted_at][hour]']")
refute select == []
options = Floki.find(res, "select[name='simple[inserted_at][hour]'] option")
refute options == []
end

test "build_control Date" do
res = ExAdmin.Form.build_control(Ecto.Date, %Simple{inserted_at: Ecto.DateTime.utc}, %{}, "simple", :inserted_at, "simple_inserted_at")
select = Floki.find(res, "select[name='simple[inserted_at][year]']")
Expand Down
10 changes: 10 additions & 0 deletions test/themes/filter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ defmodule ExAdmin.ThemeFilterTest do
html = AdminLte2.Filter.build_field({:inserted_at, Ecto.DateTime}, nil, defn)
assert Floki.find(html, "label.label") |> Floki.text == "Inserted At"
end
test "AdminLte2 build_field native datetime" do
defn = %TestExAdmin.ExAdmin.Simple{index_filters: []}
html = AdminLte2.Filter.build_field({:inserted_at, DateTime}, nil, defn)
assert Floki.find(html, "label.label") |> Floki.text == "Inserted At"
end
test "AdminLte2 build_field datetime with label option" do
defn = %TestExAdmin.ExAdmin.Simple{index_filters: [inserted_at: [label: "Created On"]]}
html = AdminLte2.Filter.build_field({:inserted_at, Ecto.DateTime}, nil, defn)
Expand Down Expand Up @@ -110,6 +115,11 @@ defmodule ExAdmin.ThemeFilterTest do
html = ActiveAdmin.Filter.build_field({:inserted_at, Ecto.DateTime}, nil, defn)
assert Floki.find(html, "label.label") |> Floki.text == "Inserted At"
end
test "ActiveAdmin build_field native datetime" do
defn = %TestExAdmin.ExAdmin.Simple{index_filters: []}
html = ActiveAdmin.Filter.build_field({:inserted_at, DateTime}, nil, defn)
assert Floki.find(html, "label.label") |> Floki.text == "Inserted At"
end
test "ActiveAdmin build_field datetime with label option" do
defn = %TestExAdmin.ExAdmin.Simple{index_filters: [inserted_at: [label: "Created On"]]}
html = ActiveAdmin.Filter.build_field({:inserted_at, Ecto.DateTime}, nil, defn)
Expand Down
5 changes: 5 additions & 0 deletions web/ex_admin/errors_helper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ defmodule ExAdmin.ErrorsHelper do

defp flatten_errors(errors_array, assoc_prefixes, prefix \\ nil)
defp flatten_errors(%Ecto.Changeset{changes: changes, errors: errors}, assoc_prefixes, prefix) when errors == [] or is_nil(prefix) do
changes = Enum.reject(changes, fn({_,v}) -> is_struct(v) end)
|> Enum.into(%{})
errors ++ flatten_errors(changes, assoc_prefixes, prefix)
end

Expand Down Expand Up @@ -82,4 +84,7 @@ defmodule ExAdmin.ErrorsHelper do
end
end)
end

defp is_struct(%{__struct__: _}), do: true
defp is_struct(_), do: false
end

0 comments on commit 53429b0

Please sign in to comment.