Skip to content

Commit

Permalink
Merge pull request #134 from mojotech/cpj/filter_date_select
Browse files Browse the repository at this point in the history
Add new date filter options and input
  • Loading branch information
cpjolicoeur authored Feb 7, 2020
2 parents 821edbc + 5df834e commit b6a2aaa
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 6 deletions.
10 changes: 8 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@ cache:
elixir:
- 1.8.1
- 1.9.1
- 1.10.0
otp_release:
- 20.3
- 21.3
- 22.0
- 22.2
jobs:
include:
- elixir: '1.8.1'
otp_release: '20.3'
- elixir: '1.9.1'
otp_release: '20.3'
script:
- bin/setup
before_script:
Expand Down
67 changes: 65 additions & 2 deletions lib/torch/views/filter_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ defmodule Torch.FilterView do

@type prefix :: atom | String.t()
@type field :: atom | String.t()
@type input_type :: atom | String.t()

@doc """
Generates a select box for a `belongs_to` association.
Expand Down Expand Up @@ -53,6 +54,29 @@ defmodule Torch.FilterView do
select(:filters, "", opts, class: "filter-type", value: "#{prefix}[#{selected}]")
end

@doc """
Generates a "before/after" filter type select box for a given `date` or
`datetime` field.
## Example
iex> params = %{"post" => %{"updated_at_after" => "01/01/2019"}}
...> filter_date_select(:post, :updated_at, params) |> safe_to_string()
"<select class=\\"filter-type\\" id=\\"filters_\\" name=\\"filters[]\\"><option value=\\"post[updated_at_before]\\">Before</option><option value=\\"post[updated_at_after]\\" selected>After</option></select>"
"""
@spec filter_date_select(prefix, field, map) :: Phoenix.HTML.safe()
def filter_date_select(prefix, field, params) do
prefix_str = to_string(prefix)
{selected, _value} = find_param(params[prefix_str], field)

opts = [
{dgettext("default", "Before"), "#{prefix}[#{field}_before]"},
{dgettext("default", "After"), "#{prefix}[#{field}_after]"}
]

select(:filters, "", opts, class: "filter-type", value: "#{prefix}[#{selected}]")
end

@doc """
Generates a number filter type select box for a given `number` field.
Expand Down Expand Up @@ -126,9 +150,23 @@ defmodule Torch.FilterView do
iex> params = %{"post" => %{"inserted_at_between" => %{"start" => "01/01/2018", "end" => "01/31/2018"}}}
...> filter_date_input(:post, :inserted_at, params) |> safe_to_string()
"<input class=\\"datepicker start\\" name=\\"post[inserted_at_between][start]\\" placeholder=\\"Select Start Date\\" type=\\"text\\" value=\\"01/01/2018\\"><input class=\\"datepicker end\\" name=\\"post[inserted_at_between][end]\\" placeholder=\\"Select End Date\\" type=\\"text\\" value=\\"01/31/2018\\">"
iex> params = %{"post" => %{"inserted_at_between" => %{"start" => "01/01/2018", "end" => "01/31/2018"}}}
...> filter_date_input(:post, :inserted_at, params, :range) |> safe_to_string()
"<input class=\\"datepicker start\\" name=\\"post[inserted_at_between][start]\\" placeholder=\\"Select Start Date\\" type=\\"text\\" value=\\"01/01/2018\\"><input class=\\"datepicker end\\" name=\\"post[inserted_at_between][end]\\" placeholder=\\"Select End Date\\" type=\\"text\\" value=\\"01/31/2018\\">"
iex> params = %{"post" => %{"inserted_at_before" => "01/01/2018"}}
...> filter_date_input(:post, :inserted_at, params, :select) |> safe_to_string()
"<input class=\\"datepicker\\" name=\\"post[inserted_at_before]\\" placeholder=\\"Select Date\\" type=\\"text\\" value=\\"01/01/2018\\">"
iex> params = %{"post" => %{"inserted_at_after" => "01/01/2018"}}
...> filter_date_input(:post, :inserted_at, params, :select) |> safe_to_string()
"<input class=\\"datepicker\\" name=\\"post[inserted_at_after]\\" placeholder=\\"Select Date\\" type=\\"text\\" value=\\"01/01/2018\\">"
"""
@spec filter_date_input(prefix, field, map) :: Phoenix.HTML.safe()
def filter_date_input(prefix, field, params) do
@spec filter_date_input(prefix, field, map, input_type) :: Phoenix.HTML.safe()
def filter_date_input(prefix, field, params, input_type \\ :range)

def filter_date_input(prefix, field, params, :range) do
prefix = to_string(prefix)
field = to_string(field)

Expand All @@ -149,6 +187,19 @@ defmodule Torch.FilterView do
raw(start ++ ending)
end

def filter_date_input(prefix, field, params, :select) do
prefix_str = to_string(prefix)
{name, value} = find_param(params[prefix_str], field, :date)

{:safe, date_input} =
torch_date_input(
"#{prefix}[#{name}]",
value
)

raw(date_input)
end

@doc """
Generates a filter select box for a boolean field.
Expand All @@ -169,6 +220,17 @@ defmodule Torch.FilterView do
checkbox(prefix, :"#{field}_equals", value: value)
end

defp torch_date_input(name, value) do
tag(
:input,
type: "text",
class: "datepicker",
name: name,
value: value,
placeholder: dgettext("default", "Select Date")
)
end

defp torch_date_input(name, value, "start") do
tag(
:input,
Expand Down Expand Up @@ -206,6 +268,7 @@ defmodule Torch.FilterView do
cond do
result == nil && type == :string -> {"#{pattern}_contains", nil}
result == nil && type == :number -> {"#{pattern}_equals", nil}
result == nil && type == :date -> {"#{pattern}_before", nil}
result != nil -> result
end
end
Expand Down
4 changes: 2 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ defmodule Torch.MixProject do
def project do
[
app: :torch,
version: "2.1.0",
elixir: "~> 1.5",
version: "3.0.0",
elixir: "~> 1.8",
start_permanent: Mix.env() == :prod,
compilers: [:phoenix, :gettext] ++ Mix.compilers(),
name: "Torch",
Expand Down

0 comments on commit b6a2aaa

Please sign in to comment.