Skip to content

Commit

Permalink
Merge pull request #659 from naymspace/feature/refactor-liveresource-…
Browse files Browse the repository at this point in the history
…using

Refactor Long Quote Block in LiveResource
  • Loading branch information
pehbehbeh authored Nov 14, 2024
2 parents 9088586 + 85a1f3d commit a14fd38
Show file tree
Hide file tree
Showing 9 changed files with 830 additions and 860 deletions.
8 changes: 6 additions & 2 deletions guides/upgrading/v0.9.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ Update Backpex to the latest version:
end
```


## Refactor calls to [`Backpex.HTML.Form.field_input/1`]()

We've refactored the [`Backpex.HTML.Form.field_input/1`]() component and renamed it to `Backpex.HTML.Form.input/1`.
Expand Down Expand Up @@ -65,4 +64,9 @@ def render_form(assigns) do
</div>
"""
end
```
```

## `Backpex.LiveResource` function usage

Although the change is relatively small, if you are using public functions of the `Backpex.LiveResource` directly,
check the updated function definitions in the module documentation.
4 changes: 2 additions & 2 deletions lib/backpex/adapters/ecto.ex
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ defmodule Backpex.Adapters.Ecto do
TODO: Should be private.
"""
def list_query(assigns, item_query, fields, criteria \\ []) do
%{schema: schema, full_text_search: full_text_search, live_resource: live_resource} = assigns
%{schema: schema, full_text_search: full_text_search} = assigns
associations = associations(fields, schema)

schema
Expand All @@ -132,7 +132,7 @@ defmodule Backpex.Adapters.Ecto do
|> maybe_preload(associations, fields)
|> maybe_merge_dynamic_fields(fields)
|> apply_search(schema, full_text_search, criteria[:search])
|> apply_filters(criteria[:filters], live_resource.get_empty_filter_key())
|> apply_filters(criteria[:filters], Backpex.LiveResource.empty_filter_key())
|> apply_criteria(criteria, fields)
end

Expand Down
6 changes: 3 additions & 3 deletions lib/backpex/field.ex
Original file line number Diff line number Diff line change
Expand Up @@ -260,12 +260,12 @@ defmodule Backpex.Field do
Handles index editable.
"""
def handle_index_editable(socket, value, change) do
if not Backpex.LiveResource.can?(socket.assigns, :edit, socket.assigns.item, socket.assigns.live_resource) do
%{assigns: %{item: item, live_resource: live_resource, fields: fields} = assigns} = socket

if not live_resource.can?(assigns, :edit, item) do
raise Backpex.ForbiddenError
end

%{assigns: %{item: item, live_resource: live_resource, fields: fields} = assigns} = socket

opts = [
after_save_fun: fn item ->
live_resource.on_item_updated(socket, item)
Expand Down
3 changes: 1 addition & 2 deletions lib/backpex/fields/belongs_to.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ defmodule Backpex.Fields.BelongsTo do

import Ecto.Query

alias Backpex.LiveResource
alias Backpex.Router

@impl Phoenix.LiveComponent
Expand Down Expand Up @@ -194,7 +193,7 @@ defmodule Backpex.Fields.BelongsTo do
assigns

link =
if Map.has_key?(field_options, :live_resource) and LiveResource.can?(assigns, :show, value, live_resource) do
if Map.has_key?(field_options, :live_resource) and live_resource.can?(assigns, :show, value) do
Router.get_path(socket, Map.get(field_options, :live_resource), params, :show, value)
else
nil
Expand Down
8 changes: 3 additions & 5 deletions lib/backpex/fields/has_many.ex
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ defmodule Backpex.Fields.HasMany do
import Ecto.Query
import Backpex.HTML.Form
alias Backpex.Adapters.Ecto, as: EctoAdapter
alias Backpex.LiveResource
alias Backpex.Router

@impl Phoenix.LiveComponent
Expand Down Expand Up @@ -256,7 +255,8 @@ defmodule Backpex.Fields.HasMany do
{field_name, field_options} = field
validate_live_resource(field_name, field_options)

schema = field_options.live_resource.schema()
# TODO: do not rely on specific adapter
schema = field_options.live_resource.config(:adapter_config)[:schema]
field_name_string = to_string(field_name)

new_assocs = get_new_assocs(attrs, field_name_string, schema, repo, field_options, assigns)
Expand Down Expand Up @@ -335,14 +335,12 @@ defmodule Backpex.Fields.HasMany do
socket: socket,
field_options: field_options,
item: item,
live_resource: live_resource,
params: params,
link_assocs: link_assocs
} = assigns

link =
if link_assocs and Map.has_key?(field_options, :live_resource) and
LiveResource.can?(assigns, :show, item, live_resource) do
if link_assocs and field_options.live_resource.can?(assigns, :show, item) do
Router.get_path(socket, Map.get(field_options, :live_resource), params, :show, item)
else
nil
Expand Down
19 changes: 8 additions & 11 deletions lib/backpex/html/resource.ex
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ defmodule Backpex.HTML.Resource do
{_name, field_options} = field = Enum.find(fields, fn {field_name, _field_options} -> field_name == name end)

readonly =
not LiveResource.can?(assigns, :edit, item, live_resource) or
not live_resource.can?(assigns, :edit, item) or
Backpex.Field.readonly?(field_options, assigns)

assigns =
Expand Down Expand Up @@ -626,10 +626,7 @@ defmodule Backpex.HTML.Resource do
def resource_buttons(assigns) do
~H"""
<div class="mb-4 flex space-x-2">
<.link
:if={LiveResource.can?(assigns, :new, nil, @live_resource)}
patch={Router.get_path(@socket, @live_resource, @params, :new)}
>
<.link :if={@live_resource.can?(assigns, :new, nil)} patch={Router.get_path(@socket, @live_resource, @params, :new)}>
<button class="btn btn-sm btn-outline btn-primary">
<%= @create_button_label %>
</button>
Expand Down Expand Up @@ -686,8 +683,8 @@ defmodule Backpex.HTML.Resource do
/>
<.index_filter
live_resource={@live_resource}
filter_options={LiveResource.get_filter_options(@live_resource, @query_options)}
filters={LiveResource.get_active_filters(@live_resource, assigns)}
filter_options={LiveResource.get_filter_options(@query_options)}
filters={LiveResource.active_filters(assigns)}
/>
</div>
"""
Expand All @@ -703,7 +700,7 @@ defmodule Backpex.HTML.Resource do

defp resource_actions(assigns, resource_actions) do
Enum.filter(resource_actions, fn {key, _action} ->
LiveResource.can?(assigns, key, nil, assigns.live_resource)
assigns.live_resource.can?(assigns, key, nil)
end)
end

Expand All @@ -712,7 +709,7 @@ defmodule Backpex.HTML.Resource do
resource_actions = resource_actions(assigns, assigns.resource_actions)

Enum.any?(index_item_actions) &&
(Enum.any?(resource_actions) || LiveResource.can?(assigns, :new, nil, assigns.live_resource))
(Enum.any?(resource_actions) || assigns.live_resource.can?(assigns, :new, nil))
end

defp index_item_actions(item_actions) do
Expand All @@ -729,7 +726,7 @@ defmodule Backpex.HTML.Resource do

defp action_disabled?(assigns, action_key, items) do
Enum.filter(items, fn item ->
LiveResource.can?(assigns, action_key, item, assigns.live_resource)
assigns.live_resource.can?(assigns, action_key, item)
end)
|> Enum.empty?()
end
Expand Down Expand Up @@ -758,7 +755,7 @@ defmodule Backpex.HTML.Resource do
|> assign(:search_active?, get_in(assigns, [:query_options, :search]) not in [nil, ""])
|> assign(:filter_active?, get_in(assigns, [:query_options, :filters]) != %{})
|> assign(:title, Backpex.translate({"No %{resources} found", %{resources: assigns.plural_name}}))
|> assign(:create_allowed, LiveResource.can?(assigns, :new, nil, assigns.live_resource))
|> assign(:create_allowed, assigns.live_resource.can?(assigns, :new, nil))

~H"""
<div class="flex justify-center py-16">
Expand Down
2 changes: 1 addition & 1 deletion lib/backpex/html/resource/resource_index_table.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@
>
<div
:for={{key, action} <- row_item_actions(@item_actions)}
:if={LiveResource.can?(assigns, key, item, @live_resource)}
:if={@live_resource.can?(assigns, key, item)}
class="tooltip hover:z-30"
data-tip={action.module.label(assigns, item)}
>
Expand Down
3 changes: 1 addition & 2 deletions lib/backpex/live_components/form_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ defmodule Backpex.FormComponent do
import Backpex.HTML.Resource

alias Backpex.Fields.Upload
alias Backpex.LiveResource
alias Backpex.Resource
alias Backpex.ResourceAction

Expand Down Expand Up @@ -350,7 +349,7 @@ defmodule Backpex.FormComponent do
{:ok, data} ->
selected_items =
Enum.filter(selected_items, fn item ->
LiveResource.can?(socket.assigns, action_key, item, socket.assigns.live_resource)
live_resource.can?(socket.assigns, action_key, item)
end)

{message, socket} =
Expand Down
Loading

0 comments on commit a14fd38

Please sign in to comment.