Haytni.Helpers (Haytni v0.7.0) View Source

Regroup various helper functions that are not directly tied to the web part (mostly for plugins use but not in controllers, views or templates).

Link to this section Summary

Functions

Helper to add a global error under the :base key as Ruby on Rails does.

Helper to add a global error under the :base key as Ruby on Rails does and right after apply (see Ecto.Changeset.apply_action/2) an action. Meaning the changeset is not meant to be reuse (you had better to use add_base_error/2 instead), it is an immediate error.

Concat to message the translated note to invite the user to also check its spam folder

Convert a duration of the form {number, unit} to seconds.

Helper for plugins to associate a mismatch error to fields given as keys of changeset.

Add the given error to all keys fields in changeset and returns the resulting as an error tuple ({:error, %Ecto.Changeset{}})

Helper intended for plugins for the implementation of their build_config/1 callback. It merges the values of params into struct, their default configuration and automatically converting duration_keys from Haytni.duration to seconds.

The translated string set as error when a key/field doesn't match any account

Helper to return the current UTC datetime as expected by :utc_datetime type of Ecto (meaning a %DateTime{} without microseconds).

Generates a module name by appending suffix to module

Casts the parameters received by a controller (a map of strings - as both keys and values) to a %Ecto.Changeset{}. This transformation is done by casting all values for keys to string and optionaly requiring (validation) the presence of required_keys.

Link to this section Functions

Link to this function

add_base_error(changeset, message)

View Source

Specs

add_base_error(changeset :: Ecto.Changeset.t(), message :: String.t()) ::
  Ecto.Changeset.t()

Helper to add a global error under the :base key as Ruby on Rails does.

Link to this function

apply_base_error(changeset, message)

View Source

Specs

apply_base_error(changeset :: Ecto.Changeset.t(), message :: String.t()) ::
  {:error, Ecto.Changeset.t()}

Helper to add a global error under the :base key as Ruby on Rails does and right after apply (see Ecto.Changeset.apply_action/2) an action. Meaning the changeset is not meant to be reuse (you had better to use add_base_error/2 instead), it is an immediate error.

Link to this function

concat_spam_check_hint_message(message)

View Source

Specs

concat_spam_check_hint_message(message :: String.t()) :: String.t()

Concat to message the translated note to invite the user to also check its spam folder

Specs

duration(duration :: pos_integer()) :: pos_integer()
duration({pos_integer(), unit :: Haytni.duration_unit()}) :: pos_integer()

Convert a duration of the form {number, unit} to seconds.

unit can be one of the following:

  • :second
  • :minute
  • :hour
  • :day
  • :week
  • :month
  • :year
Link to this function

mark_changeset_keys_as_unmatched(changeset, keys)

View Source

Specs

mark_changeset_keys_as_unmatched(
  changeset :: Ecto.Changeset.t(),
  keys :: [atom()]
) :: {:error, Ecto.Changeset.t()}

Helper for plugins to associate a mismatch error to fields given as keys of changeset.

Returns an Ecto.Changeset.t with proper errors set.

Link to this function

mark_changeset_keys_with_error(changeset, keys, error)

View Source

Specs

mark_changeset_keys_with_error(
  changeset :: Ecto.Changeset.t(),
  keys :: [atom()],
  error :: String.t()
) :: {:error, Ecto.Changeset.t()}

Add the given error to all keys fields in changeset and returns the resulting as an error tuple ({:error, %Ecto.Changeset{}})

Link to this function

merge_config(struct, params, duration_keys \\ [])

View Source

Specs

merge_config(
  struct :: struct(),
  params :: %{optional(atom()) => any()} | Keyword.t(),
  duration_keys :: [atom()]
) :: struct()

Helper intended for plugins for the implementation of their build_config/1 callback. It merges the values of params into struct, their default configuration and automatically converting duration_keys from Haytni.duration to seconds.

Link to this macro

multi_to_regular_result(result, name)

View Source (macro)

Specs

no_match_message() :: String.t()

The translated string set as error when a key/field doesn't match any account

Specs

now() :: DateTime.t()

Helper to return the current UTC datetime as expected by :utc_datetime type of Ecto (meaning a %DateTime{} without microseconds).

Link to this function

scope_module(module, suffix)

View Source

Specs

scope_module(module :: module(), suffix :: String.t()) :: atom()

Generates a module name by appending suffix to module

iex> Elixir.Haytni.Helpers.scope_module(YourApp.User, "Connection")
YourApp.UserConnection
Link to this function

to_changeset(params, schema, keys, required_keys \\ nil)

View Source

Specs

to_changeset(
  params :: Haytni.params(),
  schema :: nil | module(),
  keys :: [atom(), ...],
  required_keys :: nil | [atom()]
) :: Ecto.Changeset.t()

Casts the parameters received by a controller (a map of strings - as both keys and values) to a %Ecto.Changeset{}. This transformation is done by casting all values for keys to string and optionaly requiring (validation) the presence of required_keys.

If required_keys is nil, all keys are mandatory.