Ecto Changeset Adds Alert Functionality

I created a repository fork ectoto extend the module Ecto.Changesetwith the ability to add alerts to the changeset. I wanted to have a function add_warnings/4that adds a warning to a change set as a simple list of alerts with this structure warnings: [{atom, {String.t, Keyword.t}}], similarly errors. The difference between the behavior warningsand errorslies in the fact that data is not saved when an error occurs, but when the warning occurs, the data is stored.

Ecto.Changesetstruct extended with keys warningsand warningless?:

defstruct valid?: false, warningless?: false, data: nil, params: nil, changes: %{}, repo: nil,
        errors: [], warnings: [], validations: [], required: [], prepare: [],
        constraints: [], filters: %{}, action: nil, types: nil,
        empty_values: @empty_values

Ecto functions for changing, modifying, processing parameters, etc. Feature add_warnings/4Added:

@spec add_warning(t, atom, String.t, Keyword.t) :: t
def add_warning(%{warnings: warnings} = changeset, key, message, keys \\ []) when is_binary(message) do
  %{changeset | warnings: [{key, {message, keys}}|warnings], warningless?: false}
end 

As a result, I get changesetwith the expected keys:

#Ecto.Changeset<action: nil, changes: %{}, data: #Company.Booking<>, errors: [],
valid?: true, warnings: [], warningless?: true>

, :

#Ecto.Changeset<action: nil,
changes: %{pickup_address: #Ecto.Changeset<action: :update,
changes: %{street_name: nil}, data: #Company.Address<>,
errors: [street_name: {"can't be blank", [validation: :required]}],
valid?: false,
warnings: [phone_number: {"This phone number is not common in Netherlands",
  []}], warningless?: false>}, data: #Company.Booking<>, errors: [],
valid?: false, warnings: [], warningless?: true>

, , . , , , :

#Ecto.Changeset<action: nil,
changes: %{pickup_address: #Ecto.Changeset<action: :update,
changes: %{street_name: "sss"}, data: #Company.Address<>, errors: [],
valid?: true,
warnings: [phone_number: {"This phone number is not common in Netherlands",
  []}], warningless?: false>}, data: #Company.Booking<>, errors: [],
valid?: true, warnings: [], warningless?: true>

, . , , :

#Ecto.Changeset<action: nil, changes: %{}, data: #Company.Booking<>,    errors: [],
valid?: true, warnings: [], warningless?: true>

- , changes, .

: , ?

+6
1

, changeset, , plug , , , __using__, Ecto.

, - - , . , , , warningless, , .

0

All Articles