Skip to content

fix: add catch-all can?/3 clause for custom item actions#13

Merged
enoonan merged 1 commit intoenoonan:mainfrom
udin-io:fix/custom-item-actions-can
Dec 22, 2025
Merged

fix: add catch-all can?/3 clause for custom item actions#13
enoonan merged 1 commit intoenoonan:mainfrom
udin-io:fix/custom-item-actions-can

Conversation

@pshoukry
Copy link
Copy Markdown
Contributor

Summary

  • Fixes crash when using custom item_actions in AshBackpex.LiveResource
  • Adds a fallback can?/3 clause that handles custom actions

Problem

When using custom item_actions in an AshBackpex.LiveResource, the page crashes with:

no function clause matching in MyAppWeb.Admin.UserLive."-inlined-can?/3-"/3

This happens because the transformer generates specific can?/3 function clauses for standard actions (:new, :index, :show, :edit, :delete) but does NOT generate a catch-all fallback clause.

Solution

Added a catch-all can?/3 clause after the destroy_action block that:

  1. Returns true for actions that don't exist on the Ash resource (custom UI actions)
  2. Checks Ash authorization using Ash.can? for actions that do exist on the resource

This mirrors the base Backpex.LiveResource behavior (which returns true by default) while integrating with Ash's authorization system when applicable.

Test plan

  • Added TestPromoteItemAction - a custom item action with icon for testing
  • Added TestCustomItemActionLive - a LiveResource with the custom item action
  • Added tests verifying:
    • Unknown actions (like :promote) return true
    • Known Ash actions (like :read) check Ash authorization
  • All existing tests pass

When using custom item_actions in an AshBackpex.LiveResource, the page
would crash with FunctionClauseError because the transformer only
generated can?/3 clauses for standard actions (:new, :index, :show,
:edit, :delete).

The fix adds a fallback can?/3 clause that:
- Returns true for actions that don't exist on the Ash resource
- Checks Ash authorization for actions that do exist on the resource

This mirrors the base Backpex.LiveResource behavior while integrating
with Ash's authorization system.
@enoonan
Copy link
Copy Markdown
Owner

enoonan commented Dec 22, 2025

Awesome work. Thank you for this!

@enoonan enoonan merged commit 896a455 into enoonan:main Dec 22, 2025
1 check passed
@pshoukry
Copy link
Copy Markdown
Contributor Author

🙇 Thanks for merging!

@enoonan
Copy link
Copy Markdown
Owner

enoonan commented Dec 22, 2025

Just pushed the new version to Hex and created a release

@pshoukry pshoukry deleted the fix/custom-item-actions-can branch December 23, 2025 09:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants