Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass metadata to changesets via keyword list #94

Merged
merged 7 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions demo/lib/demo/address.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ defmodule Demo.Address do
@required_fields ~w[street zip city country]a

@doc false
def changeset(address, attrs) do
def changeset(address, attrs, _metadata \\ []) do
address
|> cast(attrs, @required_fields)
|> validate_required(@required_fields)
end

def update_changeset(address, attrs) do
def update_changeset(address, attrs, _metadata \\ []) do
address
|> cast(attrs, @required_fields)
|> validate_required(@required_fields)
end

def create_changeset(address, attrs) do
def create_changeset(address, attrs, _metadata \\ []) do
address
|> cast(attrs, @required_fields)
|> validate_required(@required_fields)
Expand Down
4 changes: 2 additions & 2 deletions demo/lib/demo/category.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ defmodule Demo.Category do
@required_fields ~w[name]a
@optional_fields ~w[]a

def update_changeset(category, attrs) do
def update_changeset(category, attrs, _metadata \\ []) do
category
|> cast(attrs, @required_fields ++ @optional_fields)
|> validate_required(@required_fields)
end

def create_changeset(category, attrs) do
def create_changeset(category, attrs, _metadata \\ []) do
category
|> cast(attrs, @required_fields ++ @optional_fields)
|> validate_required(@required_fields)
Expand Down
4 changes: 2 additions & 2 deletions demo/lib/demo/film_review.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ defmodule Demo.FilmReview do

@required_fields ~w[title overview]a

def update_changeset(film_review, attrs) do
def update_changeset(film_review, attrs, _metadata \\ []) do
film_review
|> cast(attrs, @required_fields)
|> validate_required(@required_fields)
end

def create_changeset(film_review, attrs) do
def create_changeset(film_review, attrs, _metadata \\ []) do
film_review
|> cast(attrs, @required_fields)
|> validate_required(@required_fields)
Expand Down
4 changes: 2 additions & 2 deletions demo/lib/demo/invoice.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ defmodule Demo.Invoice do
@required_fields ~w[company amount]a
@optional_fields ~w[]a

def update_changeset(invoice, attrs) do
def update_changeset(invoice, attrs, _metadata \\ []) do
invoice
|> cast(attrs, @required_fields ++ @optional_fields)
|> validate_required(@required_fields)
end

def create_changeset(invoice, attrs) do
def create_changeset(invoice, attrs, _metadata \\ []) do
invoice
|> cast(attrs, @required_fields ++ @optional_fields)
|> validate_required(@required_fields)
Expand Down
4 changes: 2 additions & 2 deletions demo/lib/demo/post.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ defmodule Demo.Post do
@required_fields ~w[title body published]a
@optional_fields ~w[show_likes user_id category_id likes]a

def update_changeset(post, attrs) do
def update_changeset(post, attrs, _metadata \\ []) do
post
|> cast(attrs, @required_fields ++ @optional_fields)
|> validate_required(@required_fields)
|> maybe_remove_likes()
end

def create_changeset(post, attrs) do
def create_changeset(post, attrs, _metadata \\ []) do
post
|> cast(attrs, @required_fields ++ @optional_fields)
|> validate_required(@required_fields)
Expand Down
2 changes: 1 addition & 1 deletion demo/lib/demo/product.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ defmodule Demo.Product do
@required_fields ~w[name quantity manufacturer price]a
@optional_fields ~w[]a

def changeset(product, attrs) do
def changeset(product, attrs, _metadata \\ []) do
product
|> cast(attrs, @required_fields ++ @optional_fields)
|> cast_assoc(:suppliers,
Expand Down
4 changes: 2 additions & 2 deletions demo/lib/demo/tag.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ defmodule Demo.Tag do

@required_fields ~w[name inserted_at]a

def update_changeset(category, attrs) do
def update_changeset(category, attrs, _metadata \\ []) do
category
|> cast(attrs, @required_fields)
|> validate_required(@required_fields)
end

def create_changeset(category, attrs) do
def create_changeset(category, attrs, metadata) do
category
|> cast(attrs, @required_fields)
|> validate_required(@required_fields)
Expand Down
2 changes: 1 addition & 1 deletion demo/lib/demo/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ defmodule Demo.User do
@optional_fields ~w[avatar deleted_at permissions age]a

@doc false
def changeset(user, attrs) do
def changeset(user, attrs, _metadata \\ []) do
user
|> cast(attrs, @required_fields ++ @optional_fields)
|> cast_assoc(:users_addresses, with: &UsersAddresses.changeset/2)
Expand Down
4 changes: 2 additions & 2 deletions demo/lib/demo_web/live/address_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ defmodule DemoWeb.AddressLive do
layout: {DemoWeb.Layouts, :admin},
schema: Demo.Address,
repo: Demo.Repo,
update_changeset: &Demo.Address.update_changeset/2,
create_changeset: &Demo.Address.create_changeset/2,
update_changeset: &Demo.Address.update_changeset/3,
create_changeset: &Demo.Address.create_changeset/3,
pubsub: Demo.PubSub,
topic: "addresses",
event_prefix: "address_",
Expand Down
4 changes: 2 additions & 2 deletions demo/lib/demo_web/live/category_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ defmodule DemoWeb.CategoryLive do
layout: {DemoWeb.Layouts, :admin},
schema: Demo.Category,
repo: Demo.Repo,
update_changeset: &Demo.Category.update_changeset/2,
create_changeset: &Demo.Category.create_changeset/2,
update_changeset: &Demo.Category.update_changeset/3,
create_changeset: &Demo.Category.create_changeset/3,
pubsub: Demo.PubSub,
topic: "categories",
event_prefix: "category_"
Expand Down
4 changes: 2 additions & 2 deletions demo/lib/demo_web/live/film_review_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ defmodule DemoWeb.FilmReviewLive do
layout: {DemoWeb.Layouts, :admin},
schema: Demo.FilmReview,
repo: Demo.Repo,
update_changeset: &Demo.FilmReview.update_changeset/2,
create_changeset: &Demo.FilmReview.create_changeset/2,
update_changeset: &Demo.FilmReview.update_changeset/3,
create_changeset: &Demo.FilmReview.create_changeset/3,
pubsub: Demo.PubSub,
topic: "film_reviews",
event_prefix: "film_reviews_",
Expand Down
4 changes: 2 additions & 2 deletions demo/lib/demo_web/live/invoice_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ defmodule DemoWeb.InvoiceLive do
layout: {DemoWeb.Layouts, :admin},
schema: Demo.Invoice,
repo: Demo.Repo,
update_changeset: &Demo.Invoice.update_changeset/2,
create_changeset: &Demo.Invoice.create_changeset/2,
update_changeset: &Demo.Invoice.update_changeset/3,
create_changeset: &Demo.Invoice.create_changeset/3,
pubsub: Demo.PubSub,
topic: "invoices",
event_prefix: "invoice_"
Expand Down
4 changes: 2 additions & 2 deletions demo/lib/demo_web/live/post_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ defmodule DemoWeb.PostLive do
layout: {DemoWeb.Layouts, :admin},
schema: Demo.Post,
repo: Demo.Repo,
update_changeset: &Demo.Post.update_changeset/2,
create_changeset: &Demo.Post.create_changeset/2,
update_changeset: &Demo.Post.update_changeset/3,
create_changeset: &Demo.Post.create_changeset/3,
pubsub: Demo.PubSub,
topic: "posts",
event_prefix: "post_",
Expand Down
4 changes: 2 additions & 2 deletions demo/lib/demo_web/live/product_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ defmodule DemoWeb.ProductLive do
layout: {DemoWeb.Layouts, :admin},
schema: Demo.Product,
repo: Demo.Repo,
update_changeset: &Demo.Product.changeset/2,
create_changeset: &Demo.Product.changeset/2,
update_changeset: &Demo.Product.changeset/3,
create_changeset: &Demo.Product.changeset/3,
pubsub: Demo.PubSub,
topic: "products",
event_prefix: "product_"
Expand Down
4 changes: 2 additions & 2 deletions demo/lib/demo_web/live/tag_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ defmodule DemoWeb.TagLive do
layout: {DemoWeb.Layouts, :admin},
schema: Demo.Tag,
repo: Demo.Repo,
update_changeset: &Demo.Tag.update_changeset/2,
create_changeset: &Demo.Tag.create_changeset/2,
update_changeset: &Demo.Tag.update_changeset/3,
create_changeset: &Demo.Tag.create_changeset/3,
pubsub: Demo.PubSub,
topic: "tags",
event_prefix: "tag_"
Expand Down
4 changes: 2 additions & 2 deletions demo/lib/demo_web/live/user_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ defmodule DemoWeb.UserLive do
layout: {DemoWeb.Layouts, :admin},
schema: Demo.User,
repo: Demo.Repo,
update_changeset: &Demo.User.changeset/2,
create_changeset: &Demo.User.changeset/2,
update_changeset: &Demo.User.changeset/3,
create_changeset: &Demo.User.changeset/3,
pubsub: Demo.PubSub,
topic: "users",
event_prefix: "user_"
Expand Down
4 changes: 2 additions & 2 deletions demo/lib/demo_web/resource_actions/email.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ defmodule DemoWeb.ResourceActions.Email do
@required_fields ~w[text users]a

@impl Backpex.ResourceAction
def changeset(change, attrs) do
def changeset(change, attrs, _metadata \\ []) do
change
|> cast(attrs, @required_fields)
|> validate_required(@required_fields)
end

@impl Backpex.ResourceAction
def handle(_socket, _params) do
def handle(_socket, params) do
Flo0807 marked this conversation as resolved.
Show resolved Hide resolved
# Send mail

# We suppose there was no error.
Expand Down
2 changes: 1 addition & 1 deletion demo/lib/demo_web/resource_actions/upload.ex
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ defmodule DemoWeb.ResourceActions.Upload do
@required_fields ~w[description]a

@impl Backpex.ResourceAction
def changeset(change, attrs) do
def changeset(change, attrs, _metadata \\ []) do
change
|> cast(attrs, @required_fields)
|> validate_required(@required_fields)
Expand Down
91 changes: 91 additions & 0 deletions guides/upgrading/v0.2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# Upgrading to v0.2

## Bump Your Deps

Update Backpex to the latest version:

```elixir
defp deps do
[
{:backpex, "~> 0.2.0"}
]
end
```

## Change arity of changeset functions

See [Pull Request](https://github.com/naymspace/backpex/pull/94).

We are introducing a new way to pass metadata to changeset functions. Previously, we supported changeset functions with
different arities to optionally pass the `assigns` and `target` name to changesets.

With this release, we require the arity of all changeset functions you provide to be `3`.

The metadata, previously passed as additional parameters, is now passed as a keyword list to changesets as the third parameter.

Currently we pass the following metadata to changesets:

- `:assigns` - the assigns
- `:target` - the name of the `form` target that triggered the changeset call


When you previously had a LiveResource and Schema that looked like the following:

```elixir
defmodule MyAppWeb.UserLive do
use Backpex.LiveResource,
...
update_changeset: &MyApp.User.changeset/2,
create_changeset: &MyApp.User.changeset/2,
end


defmodule MyApp.User do
...

def changeset(user, attrs) do
...
end
end
```

You have to change it to this:

```elixir
defmodule MyAppWeb.UserLive do
use Backpex.LiveResource,
...
update_changeset: &MyApp.User.changeset/3,
create_changeset: &MyApp.User.changeset/3,
end


defmodule MyApp.User do
...

def changeset(user, attrs, metadata) do
# fetch assigns from metadata
assigns = Keyword.get(metadata, :assigns)

# fetch target from metadata
assigns = Keyword.get(metadata, :target)

...
end
end
```

This change applies to Resource and Item Actions as well.

For example:

```elixir
defmodule MyApp.EmailResourceAction
use Backpex.ResourceAction

@impl Backpex.ResourceAction
def changeset(change, attrs, _metadata \\ []) do
...
end
end
```
15 changes: 11 additions & 4 deletions lib/backpex/item_actions/item_action.ex
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ defmodule Backpex.ItemAction do
@callback fields() :: list()

@doc """
Initial change. The result will be passed to `Backpex.ItemAction.changeset/2` in order to generate a changeset.
Initial change. The result will be passed to `Backpex.ItemAction.changeset/3` in order to generate a changeset.

This function is optional and can be used to use changesets with schemas in item actions. If this function
is not provided a changeset will be generated automatically based on the provided types in `Backpex.ItemAction.fields/0`.
Expand All @@ -144,13 +144,20 @@ defmodule Backpex.ItemAction do

@doc """
The changeset to be used in the resource action. It may be used to validate form inputs.

Additional metadata is passed as a keyword list via the third parameter.

The list of metadata:
- `:assigns` - the assigns
- `:target` - the name of the `form` target that triggered the changeset call. Default to `nil` if the call was not triggered by a form field.
"""
@callback changeset(
change ::
Ecto.Schema.t()
| Ecto.Changeset.t()
| {Ecto.Changeset.data(), Ecto.Changeset.types()},
attrs :: map()
attrs :: map(),
metadata :: keyword()
) :: Ecto.Changeset.t()

@doc """
Expand Down Expand Up @@ -181,7 +188,7 @@ defmodule Backpex.ItemAction do
@callback handle(socket :: Phoenix.LiveView.Socket.t(), items :: list(map()), params :: map()) ::
{:noreply, Phoenix.LiveView.Socket.t()} | {:reply, map(), Phoenix.LiveView.Socket.t()}

@optional_callbacks confirm: 1, confirm_label: 1, cancel_label: 1, changeset: 2, fields: 0
@optional_callbacks confirm: 1, confirm_label: 1, cancel_label: 1, changeset: 3, fields: 0

@doc """
Defines `Backpex.ItemAction` behaviour and provides default implementations.
Expand Down Expand Up @@ -212,7 +219,7 @@ defmodule Backpex.ItemAction do
def fields, do: []

@impl Backpex.ItemAction
def changeset(change, attrs), do: Ecto.Changeset.change(init_change())
def changeset(_change, _attrs, _metadata), do: Ecto.Changeset.change(init_change())
end
end
end
Loading
Loading