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

Fix update_item may raise #457

Merged
merged 5 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 lib/backpex/live_resource.ex
Original file line number Diff line number Diff line change
Expand Up @@ -911,16 +911,16 @@ defmodule Backpex.LiveResource do
%{assigns: %{live_action: live_action, repo: repo, schema: schema} = assigns} = socket

fields = filtered_fields_by_action(fields(), assigns, :show)
item = Resource.get!(id, repo, schema, &item_query(&1, live_action, assigns), fields)
item = Resource.get(id, repo, schema, &item_query(&1, live_action, assigns), fields)

socket =
cond do
live_action in [:index, :resource_action] ->
live_action in [:index, :resource_action] and item ->
items = Enum.map(socket.assigns.items, &if(&1.id == id, do: item, else: &1))

assign(socket, :items, items)

live_action == :show ->
live_action == :show and item ->
assign(socket, :item, item)

true ->
Expand Down
23 changes: 21 additions & 2 deletions lib/backpex/resource.ex
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ defmodule Backpex.Resource do
end

@doc """
Gets a database record with the given fields by the given id.
Gets a database record with the given fields by the given id. Raises Ecto.NoResultsError if no record was found.
Flo0807 marked this conversation as resolved.
Show resolved Hide resolved

## Parameters

Expand All @@ -265,6 +265,25 @@ defmodule Backpex.Resource do
* `fields` (list): A list of atoms representing the fields to be selected and potentially preloaded.
"""
def get!(id, repo, schema, item_query, fields) do
query_record(id, repo, schema, item_query, fields, :one!)
end

@doc """
Gets a database record with the given fields by the given id. Returns nil if no result was found.

## Parameters

* `id`: The identifier for the specific item to be fetched.
* `repo` (module): The repository module.
* `schema`: The Ecto schema module corresponding to the item
* `item_query` (function): A function that modifies the base query. This function should accept an Ecto.Queryable and return an Ecto.Queryable. It's used to apply additional query logic.
* `fields` (list): A list of atoms representing the fields to be selected and potentially preloaded.
"""
def get(id, repo, schema, item_query, fields) do
query_record(id, repo, schema, item_query, fields, :one)
Flo0807 marked this conversation as resolved.
Show resolved Hide resolved
end

defp query_record(id, repo, schema, item_query, fields, fetch_function) do
schema_name = name_by_schema(schema)
id_type = schema.__schema__(:type, :id)
associations = associations(fields, schema)
Expand All @@ -275,7 +294,7 @@ defmodule Backpex.Resource do
|> maybe_preload(associations, fields)
|> maybe_merge_dynamic_fields(fields)
|> where_id(schema_name, id_type, id)
|> repo.one!()
|> then(fn query -> apply(repo, fetch_function, [query]) end)
end

defp where_id(query, schema_name, :id, id) do
Expand Down