Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: Florian Arens <60519307+Flo0807@users.noreply.github.com>
  • Loading branch information
pehbehbeh and Flo0807 authored Nov 14, 2024
1 parent 545e496 commit 35f7599
Showing 1 changed file with 16 additions and 46 deletions.
62 changes: 16 additions & 46 deletions lib/backpex/live_resource.ex
Original file line number Diff line number Diff line change
Expand Up @@ -514,22 +514,14 @@ defmodule Backpex.LiveResource do

defp field_active?(name, saved_fields) do
case Map.get(saved_fields, Atom.to_string(name)) do
"true" ->
true

"false" ->
false

_other ->
true
"true" -> true
"false" -> false
_other -> true
end
end

def assign_items(socket) do
%{
live_resource: live_resource,
fields: fields
} = socket.assigns
%{live_resource: live_resource, fields: fields} = socket.assigns

criteria = build_criteria(socket.assigns)
items = Resource.list(fields, socket.assigns, live_resource, criteria)
Expand Down Expand Up @@ -586,7 +578,7 @@ defmodule Backpex.LiveResource do
end

@impl Phoenix.LiveView
def render(%{live_action: action} = assigns) when action in [:show, :show_edit] do
def render(%{live_action: action} = assigns) when action in [:show] do
resource_show(assigns)
end

Expand Down Expand Up @@ -619,16 +611,13 @@ defmodule Backpex.LiveResource do
end

defp apply_action(socket, :edit) do
%{
live_resource: live_resource,
singular_name: singular_name
} = socket.assigns
%{live_resource: live_resource, singular_name: singular_name} = socket.assigns

fields = live_resource.fields |> filtered_fields_by_action(socket.assigns, :edit)
primary_value = URI.decode(socket.assigns.params["backpex_id"])
item = Resource.get!(primary_value, socket.assigns, live_resource)

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

socket
|> assign(:fields, fields)
Expand All @@ -639,16 +628,13 @@ defmodule Backpex.LiveResource do
end

defp apply_action(socket, :show) do
%{
live_resource: live_resource,
singular_name: singular_name
} = socket.assigns
%{live_resource: live_resource, singular_name: singular_name} = socket.assigns

fields = live_resource.fields() |> filtered_fields_by_action(socket.assigns, :show)
primary_value = URI.decode(socket.assigns.params["backpex_id"])
item = Resource.get!(primary_value, socket.assigns, live_resource)

unless live_resource.can?(socket.assigns, :show, item), do: raise(Backpex.ForbiddenError)
if not live_resource.can?(socket.assigns, :show, item), do: raise(Backpex.ForbiddenError)

socket
|> assign(:page_title, singular_name)
Expand All @@ -658,13 +644,9 @@ defmodule Backpex.LiveResource do
end

defp apply_action(socket, :new) do
%{
live_resource: live_resource,
schema: schema,
create_button_label: create_button_label
} = socket.assigns
%{live_resource: live_resource, schema: schema, create_button_label: create_button_label} = socket.assigns

unless live_resource.can?(socket.assigns, :new, nil), do: raise(Backpex.ForbiddenError)
if not live_resource.can?(socket.assigns, :new, nil), do: raise(Backpex.ForbiddenError)

fields = live_resource.fields() |> filtered_fields_by_action(socket.assigns, :new)
empty_item = schema.__struct__()
Expand All @@ -687,7 +669,7 @@ defmodule Backpex.LiveResource do

action = live_resource.resource_actions()[id]

unless live_resource.can?(socket.assigns, id, nil), do: raise(Backpex.ForbiddenError)
if not live_resource.can?(socket.assigns, id, nil), do: raise(Backpex.ForbiddenError)

socket
|> assign(:page_title, ResourceAction.name(action, :title))
Expand Down Expand Up @@ -730,7 +712,7 @@ defmodule Backpex.LiveResource do
params: params
} = socket.assigns

unless live_resource.can?(socket.assigns, :index, nil), do: raise(Backpex.ForbiddenError)
if not live_resource.can?(socket.assigns, :index, nil), do: raise(Backpex.ForbiddenError)

fields = live_resource.fields() |> filtered_fields_by_action(socket.assigns, :index)

Expand Down Expand Up @@ -788,11 +770,7 @@ defmodule Backpex.LiveResource do
end

defp assign_changeset(socket, fields) do
%{
item: item,
changeset_function: changeset_function,
live_action: live_action
} = socket.assigns
%{item: item, changeset_function: changeset_function, live_action: live_action} = socket.assigns

metadata = Resource.build_changeset_metadata(socket.assigns)
changeset = changeset_function.(item, default_attrs(live_action, fields, socket.assigns), metadata)
Expand Down Expand Up @@ -830,12 +808,7 @@ defmodule Backpex.LiveResource do
defp default_attrs(_live_action, _fields, _assigns), do: %{}

defp maybe_redirect_to_default_filters(%{assigns: %{filters_changed: false}} = socket) do
%{
live_resource: live_resource,
query_options: query_options,
params: params,
filters: filters
} = socket.assigns
%{live_resource: live_resource, query_options: query_options, params: params, filters: filters} = socket.assigns

filters_with_defaults =
filters
Expand Down Expand Up @@ -1168,10 +1141,7 @@ defmodule Backpex.LiveResource do
end

defp update_item(socket, item) do
%{
live_resource: live_resource,
live_action: live_action
} = socket.assigns
%{live_resource: live_resource, live_action: live_action} = socket.assigns

item_primary_value = primary_value(socket, item)
item = Resource.get(item_primary_value, socket.assigns, live_resource)
Expand Down

0 comments on commit 35f7599

Please sign in to comment.