Skip to content

Commit

Permalink
Merge pull request #56 from naymspace/feature/support-assigns-in-chan…
Browse files Browse the repository at this point in the history
…geset

Pass assigns to changeset function
  • Loading branch information
pehbehbeh authored Jan 11, 2024
2 parents ce28396 + e8ad39f commit 52921b5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
12 changes: 6 additions & 6 deletions lib/backpex/live_components/form_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ defmodule Backpex.FormComponent do

changeset =
Ecto.Changeset.change(assigns.item_action_types)
|> validate_change(changeset_function, change, target)
|> validate_change(changeset_function, change, assigns, target)

socket = assign(socket, changeset: changeset)

Expand All @@ -96,7 +96,7 @@ defmodule Backpex.FormComponent do
changeset =
Ecto.Changeset.change(item)
|> put_assocs(assocs)
|> validate_change(changeset_function, change, target)
|> validate_change(changeset_function, change, assigns, target)

send(self(), {:update_changeset, changeset})

Expand Down Expand Up @@ -144,7 +144,7 @@ defmodule Backpex.FormComponent do

changeset =
Ecto.Changeset.change(assigns.item_action_types)
|> validate_change(changeset_function, change, nil)
|> validate_change(changeset_function, change, assigns, nil)

socket = assign(socket, changeset: changeset)

Expand All @@ -162,7 +162,7 @@ defmodule Backpex.FormComponent do
changeset =
Ecto.Changeset.change(item)
|> put_assocs(assocs)
|> validate_change(changeset_function, change, nil)
|> validate_change(changeset_function, change, assigns, nil)

socket = assign(socket, changeset: changeset)

Expand Down Expand Up @@ -193,9 +193,9 @@ defmodule Backpex.FormComponent do
{:noreply, socket}
end

defp validate_change(item, changeset_function, change, target) do
defp validate_change(item, changeset_function, change, assigns, target) do
item
|> LiveResource.call_changeset_function(changeset_function, change, target)
|> LiveResource.call_changeset_function(changeset_function, change, assigns, target)
|> Map.put(:action, :validate)
end

Expand Down
19 changes: 10 additions & 9 deletions lib/backpex/live_resource.ex
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ defmodule Backpex.LiveResource do
def can?(_assigns, :my_item_action, item), do: item.role == :admin
def can?(assigns, :my_resource_action, nil), do: assigns.current_user == :admin
> Note that item actions are always displayed if they are defined. If you want to remove item actions completely, you must restrict access to them with `can?/3` and remove the action with the `item_actions/1` function.
## Resource Actions
Expand Down Expand Up @@ -659,8 +659,8 @@ defmodule Backpex.LiveResource do
* `:layout` - Layout to be used by the LiveResource.
* `:schema` - Schema for the resource.
* `:repo` - Ecto repo that will be used to perform CRUD operations for the given schema.
* `:update_changeset` - Changeset that will be used when updating items. Optionally takes the target as the third parameter.
* `:create_changeset` - Changeset that will be used when creating items. Optionally takes the target as the third parameter.
* `:update_changeset` - Changeset to use when updating items. Optionally takes the target as the third parameter and the assigns as the fourth.
* `:create_changeset` - Changeset to use when creating items. Optionally takes the target as the third parameter and the assigns as the fourth.
* `:pubsub` - PubSub name of the project.
* `:topic` - The topic for PubSub.
* `:event_prefix` - The event prefix for Pubsub, to differentiate between events of different resources when subscribed to multiple resources.
Expand Down Expand Up @@ -1008,7 +1008,8 @@ defmodule Backpex.LiveResource do
Backpex.LiveResource.call_changeset_function(
item,
changeset_function,
default_attrs(live_action, fields, assigns)
default_attrs(live_action, fields, assigns),
assigns
)

socket
Expand Down Expand Up @@ -1967,13 +1968,13 @@ defmodule Backpex.LiveResource do
@doc """
Calls the changeset function with the given change and target.
"""
def call_changeset_function(item, changeset_function, change, target \\ nil) do
def call_changeset_function(item, changeset_function, change, assigns, target \\ nil) do
arity = :erlang.fun_info(changeset_function)[:arity]

if arity == 2 do
changeset_function.(item, change)
else
changeset_function.(item, change, target)
case arity do
2 -> changeset_function.(item, change)
3 -> changeset_function.(item, change, target)
4 -> changeset_function.(item, change, target, assigns)
end
end

Expand Down
4 changes: 2 additions & 2 deletions lib/backpex/resource.ex
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ defmodule Backpex.Resource do

changeset
|> prepare_for_validation()
|> LiveResource.call_changeset_function(changeset_function, change)
|> LiveResource.call_changeset_function(changeset_function, change, assigns)
|> repo.update()
|> broadcast("updated", assigns)
end
Expand Down Expand Up @@ -335,7 +335,7 @@ defmodule Backpex.Resource do

changeset
|> prepare_for_validation()
|> LiveResource.call_changeset_function(changeset_function, change)
|> LiveResource.call_changeset_function(changeset_function, change, assigns)
|> repo.insert()
|> broadcast("created", assigns)
end
Expand Down

0 comments on commit 52921b5

Please sign in to comment.