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

Link to this function

actual_deletion_date(grace_period, cron_expression \\ "4 2 * * *", unit \\ :days)

View Source
Link to this function

coerce_json_field(attrs, field)

View Source
@spec coerce_json_field(map(), Map.key()) :: map()

Changes a given maps field from a json string to a map. If it cannot be converted, it leaves the original value

Link to this function

copy_error(changeset, original_key, new_key, opts \\ [overwrite: true])

View Source

Copies 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. Supports overwrite, which is a boolean indicating whether to overwrite the new_key error if it already exists. Defaults to true.

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.

Link to this function

format_date(date, formatter \\ "%a %d/%m/%Y at %H:%M:%S")

View Source

Recursively ensures a given map is safe to convert to JSON, where all keys are strings and all values are json safe (primitive values).

Link to this function

ms_to_human(milliseconds)

View Source
@spec ms_to_human(integer()) :: String.t() | {:error, :invalid_duration}

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().

@spec url_safe_name(String.t() | nil) :: String.t()

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. If nil is passed, it returns an empty string.

Examples

iex> url_safe_name("My Project!!")
"my-project"

iex> url_safe_name(nil)
""