Skip to content

Commit 1873e89

Browse files
authored
Merge pull request #1271 from naymspace/feature/use-filtered-fields-in-ecto-adapter
Refactor fields handling
2 parents 4628474 + 0447cdd commit 1873e89

File tree

22 files changed

+202
-158
lines changed

22 files changed

+202
-158
lines changed

demo/lib/demo_web/live/post_live.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ defmodule DemoWeb.PostLive do
110110
module: Backpex.Fields.Textarea,
111111
label: "Body",
112112
rows: 10,
113-
except: [:index, :resource_action],
113+
except: [:index],
114114
align_label: :center
115115
},
116116
published: %{
@@ -123,7 +123,7 @@ defmodule DemoWeb.PostLive do
123123
module: Backpex.Fields.Boolean,
124124
label: "Show likes",
125125
select: dynamic([post: p], fragment("? > 0", p.likes)),
126-
except: [:index, :resource_action, :show]
126+
except: [:index, :show]
127127
},
128128
likes: %{
129129
module: Backpex.Fields.Number,

demo/lib/demo_web/live/product_live.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ defmodule DemoWeb.ProductLive do
5151
assigns ->
5252
~H"<p>{Backpex.HTML.pretty_value(@value)}</p>"
5353
end,
54-
except: [:index, :resource_action]
54+
except: [:index]
5555
},
5656
name: %{
5757
module: Backpex.Fields.Text,

demo/lib/demo_web/live/short_link_live.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ defmodule DemoWeb.ShortLinkLive do
2222
def can?(_assigns, :delete, _item), do: false
2323
def can?(_assigns, _action, _item), do: true
2424

25+
@impl Backpex.LiveResource
2526
def return_to(_socket, _assigns, :edit, _form_action, _item) do
2627
# since the primary key might be updated, we go to the index page
2728
~p"/admin/short-links"

guides/fields/visibility.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ You can change the visibility of fields in certain views.
66

77
You can use the `only` and `except` options to define the views where a field should be visible. The `only` option will show the field only in the specified views, while the `except` option will show the field in all views except the specified ones. The options have to be a list of view names.
88

9-
The following values are supported: `:new`, `:edit`, `:show`, `:index` and `:resource_action`.
9+
The following values are supported: `:new`, `:edit`, `:show` and `:index`.
1010

1111
```elixir
1212
# in your resource configuration file

guides/filter/how-to-add-a-filter.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ defmodule MyAppWeb.Filters.PostCategorySelect do
2727
def prompt, do: "Select category ..."
2828

2929
@impl Backpex.Filters.Select
30-
def options do
30+
def options(_assigns) do
3131
query =
3232
from p in Post,
3333
join: c in Category,

guides/live_resource/navigation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@ When working with forms (in `:new` or `:edit` live actions), the following form
3434
- `:save` - When a form is successfully submitted aka the "Save" button was clicked
3535
- `:cancel` - When a form submission is canceled aka the "Cancel" button was clicked
3636

37-
For all other live actions, the form_action will be `nil`.
37+
For all other live actions, the form_action will be `nil`.

guides/upgrading/v0.15.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,34 @@ Note that it is now also possible to configure the layout as a function:
5454
use Backpex.LiveResource,
5555
layout: &MyAppWeb.admin/1
5656
```
57+
58+
## Resource and adapter functions have been updated
59+
60+
We've updated some functions in `Backpex.Resource` and the adapter modules (`Backpex.Adapters.Ecto` and `Backpex.Adapters.Ash`) to include the fields as an additional parameter.
61+
62+
The following functions are affected:
63+
64+
`Backpex.Resource`:
65+
- `list/3` -> `Backpex.Resource.list/4`
66+
- `count/3` -> `Backpex.Resource.count/4`
67+
- `get/3` -> `Backpex.Resource.get/4`
68+
- `get!/3` -> `Backpex.Resource.get!/4`
69+
70+
`Backpex.Adapter` (including `Backpex.Adapters.Ecto` and `Backpex.Adapters.Ash`):
71+
- `list/3` -> `c:Backpex.Adapter.list/4`
72+
- `count/3` -> `c:Backpex.Adapter.count/4`
73+
- `get/3` -> `c:Backpex.Adapter.get/4`
74+
75+
For example:
76+
77+
```elixir
78+
# before
79+
Resource.get(primary_value, socket.assigns, live_resource)
80+
# after
81+
Resource.get(primary_value, fields, socket.assigns, live_resource)
82+
```
83+
84+
## `:only`/`:except` field option changes
85+
86+
You no longer need to pass `:resource_action` in addition to `:index` to the fields `:only`/`:except` option.
87+
Before, it was needed to make fields visible behind the backdrop of the resource action modal.

guides/upgrading/v0.7.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ end
3939
We have updated certain functions in `Backpex.Resource`.
4040

4141
The following functions are affected:
42-
- `Backpex.Resource.update/6` (`update/5` before)
43-
- [`Backpex.Resource.insert/6`]() (`insert/5` before)
44-
- [`Backpex.Resource.change/7`]()
45-
- [`Backpex.Resource.put_assocs/2`]() (has been removed)
42+
- `update/6` (`update/5` before)
43+
- `insert/6` (`insert/5` before)
44+
- `change/7`
45+
- `put_assocs/2` (has been removed)
4646

4747
If you call one of these functions in your application, you will probably need to update the function call.
4848

@@ -85,4 +85,4 @@ def label(_assigns, _item) do
8585
end
8686
```
8787

88-
Read more about the new `item` parameter in [the item action guide](/guides/actions/item-actions.md#implementing-an-item-action).
88+
Read more about the new `item` parameter in [the item action guide](/guides/actions/item-actions.md#implementing-an-item-action).

lib/backpex/adapter.ex

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,41 +27,51 @@ defmodule Backpex.Adapter do
2727
2828
Should return `nil` if no result was found.
2929
"""
30-
@callback get(term(), map(), module()) :: {:ok, struct() | nil} | {:error, term()}
30+
@callback get(primary_value :: term(), fields :: list(), assigns :: map(), live_resource :: module()) ::
31+
{:ok, struct() | nil} | {:error, term()}
3132

3233
@doc """
3334
Returns a list of items by given criteria.
3435
"""
35-
@callback list(keyword(), map(), module()) :: {:ok, list()}
36+
@callback list(criteria :: keyword(), fields :: list(), assigns :: map(), live_resource :: module()) :: {:ok, list()}
3637

3738
@doc """
3839
Gets the total count of the current live_resource.
3940
Possibly being constrained the item query and the search- and filter options.
4041
"""
41-
@callback count(keyword(), map(), module()) :: {:ok, non_neg_integer()}
42+
@callback count(criteria :: keyword(), fields :: list(), assigns :: map(), live_resource :: module()) ::
43+
{:ok, non_neg_integer()}
4244

4345
@doc """
4446
Inserts given item.
4547
"""
46-
@callback insert(struct(), module()) :: {:ok, struct()} | {:error, term()}
48+
@callback insert(item :: struct(), live_resource :: module()) :: {:ok, struct()} | {:error, term()}
4749

4850
@doc """
4951
Updates given item.
5052
"""
51-
@callback update(struct(), module()) :: {:ok, struct()} | {:error, term()}
53+
@callback update(item :: struct(), live_resource :: module()) :: {:ok, struct()} | {:error, term()}
5254

5355
@doc """
5456
Updates given items.
5557
"""
56-
@callback update_all(list(struct()), keyword(), module()) :: {:ok, non_neg_integer()}
58+
@callback update_all(items :: list(struct()), updates :: keyword(), live_resource :: module()) ::
59+
{:ok, non_neg_integer()}
5760

5861
@doc """
5962
Applies a change to a given item.
6063
"""
61-
@callback change(struct(), map(), term(), list(), module(), keyword()) :: Ecto.Changeset.t()
64+
@callback change(
65+
item :: struct(),
66+
attrs :: map(),
67+
fields :: term(),
68+
assigns :: list(),
69+
live_resource :: module(),
70+
opts :: keyword()
71+
) :: Ecto.Changeset.t()
6272

6373
@doc """
6474
Deletes multiple items.
6575
"""
66-
@callback delete_all(list(struct()), module()) :: {:ok, term()} | {:error, term()}
76+
@callback delete_all(items :: list(struct()), live_resource :: module()) :: {:ok, term()} | {:error, term()}
6777
end

lib/backpex/adapters/ash.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ if Code.ensure_loaded?(Ash) do
2929
Returns `nil` if no result was found.
3030
"""
3131
@impl Backpex.Adapter
32-
def get(primary_value, _assigns, live_resource) do
32+
def get(primary_value, _fields, _assigns, live_resource) do
3333
resource = live_resource.adapter_config(:resource)
3434
primary_key = live_resource.config(:primary_key)
3535

@@ -42,7 +42,7 @@ if Code.ensure_loaded?(Ash) do
4242
Returns a list of items by given criteria.
4343
"""
4444
@impl Backpex.Adapter
45-
def list(_criteria, _assigns, live_resource) do
45+
def list(_criteria, _fields, _assigns, live_resource) do
4646
live_resource.adapter_config(:resource)
4747
|> Ash.read()
4848
end
@@ -51,7 +51,7 @@ if Code.ensure_loaded?(Ash) do
5151
Returns the number of items matching the given criteria.
5252
"""
5353
@impl Backpex.Adapter
54-
def count(_criteria, _assigns, live_resource) do
54+
def count(_criteria, _fields, _assigns, live_resource) do
5555
live_resource.adapter_config(:resource)
5656
|> Ash.count()
5757
end

0 commit comments

Comments
 (0)