Skip to content

Commit 52921b5

Browse files
authored
Merge pull request #56 from naymspace/feature/support-assigns-in-changeset
Pass assigns to changeset function
2 parents ce28396 + e8ad39f commit 52921b5

File tree

3 files changed

+18
-17
lines changed

3 files changed

+18
-17
lines changed

Diff for: lib/backpex/live_components/form_component.ex

+6-6
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ defmodule Backpex.FormComponent do
8080

8181
changeset =
8282
Ecto.Changeset.change(assigns.item_action_types)
83-
|> validate_change(changeset_function, change, target)
83+
|> validate_change(changeset_function, change, assigns, target)
8484

8585
socket = assign(socket, changeset: changeset)
8686

@@ -96,7 +96,7 @@ defmodule Backpex.FormComponent do
9696
changeset =
9797
Ecto.Changeset.change(item)
9898
|> put_assocs(assocs)
99-
|> validate_change(changeset_function, change, target)
99+
|> validate_change(changeset_function, change, assigns, target)
100100

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

@@ -144,7 +144,7 @@ defmodule Backpex.FormComponent do
144144

145145
changeset =
146146
Ecto.Changeset.change(assigns.item_action_types)
147-
|> validate_change(changeset_function, change, nil)
147+
|> validate_change(changeset_function, change, assigns, nil)
148148

149149
socket = assign(socket, changeset: changeset)
150150

@@ -162,7 +162,7 @@ defmodule Backpex.FormComponent do
162162
changeset =
163163
Ecto.Changeset.change(item)
164164
|> put_assocs(assocs)
165-
|> validate_change(changeset_function, change, nil)
165+
|> validate_change(changeset_function, change, assigns, nil)
166166

167167
socket = assign(socket, changeset: changeset)
168168

@@ -193,9 +193,9 @@ defmodule Backpex.FormComponent do
193193
{:noreply, socket}
194194
end
195195

196-
defp validate_change(item, changeset_function, change, target) do
196+
defp validate_change(item, changeset_function, change, assigns, target) do
197197
item
198-
|> LiveResource.call_changeset_function(changeset_function, change, target)
198+
|> LiveResource.call_changeset_function(changeset_function, change, assigns, target)
199199
|> Map.put(:action, :validate)
200200
end
201201

Diff for: lib/backpex/live_resource.ex

+10-9
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ defmodule Backpex.LiveResource do
197197
def can?(_assigns, :my_item_action, item), do: item.role == :admin
198198
199199
def can?(assigns, :my_resource_action, nil), do: assigns.current_user == :admin
200-
200+
201201
> 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.
202202
203203
## Resource Actions
@@ -659,8 +659,8 @@ defmodule Backpex.LiveResource do
659659
* `:layout` - Layout to be used by the LiveResource.
660660
* `:schema` - Schema for the resource.
661661
* `:repo` - Ecto repo that will be used to perform CRUD operations for the given schema.
662-
* `:update_changeset` - Changeset that will be used when updating items. Optionally takes the target as the third parameter.
663-
* `:create_changeset` - Changeset that will be used when creating items. Optionally takes the target as the third parameter.
662+
* `:update_changeset` - Changeset to use when updating items. Optionally takes the target as the third parameter and the assigns as the fourth.
663+
* `:create_changeset` - Changeset to use when creating items. Optionally takes the target as the third parameter and the assigns as the fourth.
664664
* `:pubsub` - PubSub name of the project.
665665
* `:topic` - The topic for PubSub.
666666
* `:event_prefix` - The event prefix for Pubsub, to differentiate between events of different resources when subscribed to multiple resources.
@@ -1008,7 +1008,8 @@ defmodule Backpex.LiveResource do
10081008
Backpex.LiveResource.call_changeset_function(
10091009
item,
10101010
changeset_function,
1011-
default_attrs(live_action, fields, assigns)
1011+
default_attrs(live_action, fields, assigns),
1012+
assigns
10121013
)
10131014

10141015
socket
@@ -1967,13 +1968,13 @@ defmodule Backpex.LiveResource do
19671968
@doc """
19681969
Calls the changeset function with the given change and target.
19691970
"""
1970-
def call_changeset_function(item, changeset_function, change, target \\ nil) do
1971+
def call_changeset_function(item, changeset_function, change, assigns, target \\ nil) do
19711972
arity = :erlang.fun_info(changeset_function)[:arity]
19721973

1973-
if arity == 2 do
1974-
changeset_function.(item, change)
1975-
else
1976-
changeset_function.(item, change, target)
1974+
case arity do
1975+
2 -> changeset_function.(item, change)
1976+
3 -> changeset_function.(item, change, target)
1977+
4 -> changeset_function.(item, change, target, assigns)
19771978
end
19781979
end
19791980

Diff for: lib/backpex/resource.ex

+2-2
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ defmodule Backpex.Resource do
300300

301301
changeset
302302
|> prepare_for_validation()
303-
|> LiveResource.call_changeset_function(changeset_function, change)
303+
|> LiveResource.call_changeset_function(changeset_function, change, assigns)
304304
|> repo.update()
305305
|> broadcast("updated", assigns)
306306
end
@@ -335,7 +335,7 @@ defmodule Backpex.Resource do
335335

336336
changeset
337337
|> prepare_for_validation()
338-
|> LiveResource.call_changeset_function(changeset_function, change)
338+
|> LiveResource.call_changeset_function(changeset_function, change, assigns)
339339
|> repo.insert()
340340
|> broadcast("created", assigns)
341341
end

0 commit comments

Comments
 (0)