View Source Lightning.Helpers (Lightning v2.10.4)
Common functions for the context
Summary
Functions
Changes a given maps field from a json string to a map. If it cannot be converted, it leaves the original value
Copies an error from one key to another in the given changeset.
Recursively ensures a given map is safe to convert to JSON, where all keys are strings and all values are json safe (primitive values).
Converts milliseconds (integer) to a human duration, such as "1 minute" or
"45 years, 6 months, 5 days, 21 hours, 12 minutes, 34 seconds" using
Timex.Format.Duration.Formatters.Humanized.format()
.
Converts a string into a URL-safe format by converting it to lowercase, replacing unwanted characters with hyphens, and trimming leading/trailing hyphens.
Functions
actual_deletion_date(grace_period, cron_expression \\ "4 2 * * *", unit \\ :days)
View SourceChanges a given maps field from a json string to a map. If it cannot be converted, it leaves the original value
copy_error(changeset, original_key, new_key, opts \\ [overwrite: true])
View SourceCopies an error from one key to another in the given changeset.
Parameters
changeset
: The changeset to modify.original_key
: The key where the error currently exists.new_key
: The key where the error should be duplicated.opts
: A keyword list of options. Supportsoverwrite
, which is a boolean indicating whether to overwrite thenew_key
error if it already exists. Defaults totrue
.
Example
iex> changeset = %Ecto.Changeset{errors: [name: {"has already been taken", []}]}
iex> updated_changeset = Lightning.Helpers.copy_error(changeset, :name, :raw_name)
iex> updated_changeset.errors
[name: {"has already been taken", []}, raw_name: {"has already been taken", []}]
If the original_key
doesn't exist in the errors, or if the new_key
already exists and overwrite
is set to false
, the changeset is returned unchanged.
Recursively ensures a given map is safe to convert to JSON, where all keys are strings and all values are json safe (primitive values).
Converts milliseconds (integer) to a human duration, such as "1 minute" or
"45 years, 6 months, 5 days, 21 hours, 12 minutes, 34 seconds" using
Timex.Format.Duration.Formatters.Humanized.format()
.
Converts a string into a URL-safe format by converting it to lowercase, replacing unwanted characters with hyphens, and trimming leading/trailing hyphens.
This function allows international characters, which will be automatically percent-encoded in URLs by browsers.
Parameters
name
: The string to convert. Ifnil
is passed, it returns an empty string.
Examples
iex> url_safe_name("My Project!!")
"my-project"
iex> url_safe_name(nil)
""