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
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.
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.
Specs
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
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.
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{}}
)
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.
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).
Specs
Generates a module name by appending suffix to module
iex> Elixir.Haytni.Helpers.scope_module(YourApp.User, "Connection")
YourApp.UserConnection
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.