Skip to content

Commit

Permalink
Merge pull request #632 from John-Goff/format-date-datetime-function
Browse files Browse the repository at this point in the history
Allow passing a function to format a date or datetime
  • Loading branch information
Flo0807 authored Oct 16, 2024
2 parents 3093bae + 78cd121 commit c23b933
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 12 deletions.
41 changes: 35 additions & 6 deletions lib/backpex/fields/date.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ defmodule Backpex.Fields.Date do
## Options
* `:format` - Defines the date format printed on the index view.
Defaults to `#{@default_format}`.
* `:format` - Format string which will be used to format the date value or function that formats the date.
Defaults to `#{@default_format}`. If a function, must receive a `Date` and return a string.
* `:debounce` - Optional integer timeout value (in milliseconds), "blur" or function that receives the assigns.
* `:throttle` - Optional integer timeout value (in milliseconds) or function that receives the assigns.
## Example
## Examples
@impl Backpex.LiveResource
def fields do
Expand All @@ -25,6 +25,33 @@ defmodule Backpex.Fields.Date do
}
]
end
@impl Backpex.LiveResource
def fields do
[
created_at: %{
module: Backpex.Fields.Date,
label: "Created At",
format: fn date -> # Takes a `Date` and returns a string
# Timex should be installed separately, used as a reference for
# custom formatting logic.
Timex.format!(date, "{relative}", :relative)
end
}
]
end
@impl Backpex.LiveResource
def fields do
[
created_at: %{
module: Backpex.Fields.Date,
label: "Created At",
# If you use the same formatting logic in multiple places
format: &MyApp.Formatters.Dates/1
}
]
end
"""
use BackpexWeb, :field

Expand All @@ -33,9 +60,11 @@ defmodule Backpex.Fields.Date do
format = Map.get(assigns.field_options, :format, @default_format)

value =
if assigns.value,
do: Calendar.strftime(assigns.value, format),
else: HTML.pretty_value(assigns.value)
cond do
is_function(format, 1) -> format.(assigns.value)
assigns.value -> Calendar.strftime(assigns.value, format)
true -> HTML.pretty_value(assigns.value)
end

assigns =
assigns
Expand Down
41 changes: 35 additions & 6 deletions lib/backpex/fields/date_time.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ defmodule Backpex.Fields.DateTime do
## Options
* `:format` - Format string which will be used to format the date time value.
Defaults to `#{@default_format}`
* `:format` - Format string which will be used to format the date time value or function that formats the date time.
Defaults to `#{@default_format}`. If a function, must receive a `DateTime` and return a string.
* `:debounce` - Optional integer timeout value (in milliseconds), "blur" or function that receives the assigns.
* `:throttle` - Optional integer timeout value (in milliseconds) or function that receives the assigns.
## Example
## Examples
@impl Backpex.LiveResource
def fields do
Expand All @@ -25,6 +25,33 @@ defmodule Backpex.Fields.DateTime do
}
]
end
@impl Backpex.LiveResource
def fields do
[
created_at: %{
module: Backpex.Fields.DateTime,
label: "Created At",
format: fn date_time -> # Takes a `DateTime` and returns a string
# Timex should be installed separately, used as a reference for
# custom formatting logic.
Timex.format!(date_time, "{relative}", :relative)
end
}
]
end
@impl Backpex.LiveResource
def fields do
[
created_at: %{
module: Backpex.Fields.Date,
label: "Created At",
# If you use the same formatting logic in multiple places
format: &MyApp.Formatters.Dates/1
}
]
end
"""
use BackpexWeb, :field

Expand All @@ -33,9 +60,11 @@ defmodule Backpex.Fields.DateTime do
format = Map.get(assigns.field_options, :format, @default_format)

value =
if assigns.value,
do: Calendar.strftime(assigns.value, format),
else: HTML.pretty_value(assigns.value)
cond do
is_function(format, 1) -> format.(assigns.value)
assigns.value -> Calendar.strftime(assigns.value, format)
true -> HTML.pretty_value(assigns.value)
end

assigns =
assigns
Expand Down

0 comments on commit c23b933

Please sign in to comment.