-
Notifications
You must be signed in to change notification settings - Fork 3
37 feature use module attributes to map events to action names #44
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
Merged
vincentvanbush
merged 13 commits into
main
from
37-feature-use-module-attributes-to-map-events-to-action-names
Dec 4, 2025
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
765e395
Add @permit_action decorator attribute in LiveView events [#37]
vincentvanbush 8a2da55
Update docs and comments
vincentvanbush 3e26a8a
Add reload_on_event/2 callback
vincentvanbush c5a7943
Add :update to default preload_actions in LiveView
vincentvanbush 433081a
Add missing defoverridable to Permit.Phoenix.Actions
vincentvanbush 1f11c6f
Add Permit.Ecto-based reloading of @loaded_resource when event has no…
vincentvanbush 9269cad
Add missing reload_on_event?/2 defoverridable
vincentvanbush 017aec4
Authorize already preloaded resource if reload_on_event? is false
vincentvanbush e6a274f
Add tests for event authorization without record param ID
vincentvanbush 8dc1ea4
Add event mapping tests for Ecto and loader functions; bump Permit to…
vincentvanbush 78bfeae
Update CHANGELOG
vincentvanbush 4e6d1bd
Raise on invalid types in :reload_on_event? and :use_scope? opts
vincentvanbush c4c5af7
Disuse pipe operator
vincentvanbush File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| defmodule Permit.Phoenix.Decorators.LiveView do | ||
| @moduledoc false | ||
|
|
||
| def __on_definition__(env, _kind, :handle_event, args, _guards, _body) do | ||
| event_name = List.first(args) | ||
| attribute_value = Module.get_last_attribute(env.module, :permit_action, nil) | ||
|
|
||
| # event_name must be a string - this means the event handler is defined | ||
| # using pattern matching; otherwise, we cannot infer the event name from the | ||
| # function body. | ||
|
|
||
| cond do | ||
| is_binary(event_name) and not is_nil(attribute_value) -> | ||
| prior_value = Module.get_attribute(env.module, :__event_mapping__, %{}) | ||
|
|
||
| Module.put_attribute( | ||
| env.module, | ||
| :__event_mapping__, | ||
| Map.put(prior_value, event_name, attribute_value) | ||
| ) | ||
|
|
||
| # delete the permit_action attribute to avoid mapping different event names | ||
| # to the same action. | ||
| Module.delete_attribute(env.module, :permit_action) | ||
|
|
||
| not is_binary(event_name) and not is_nil(attribute_value) -> | ||
| # handle_event is not defined using pattern matching, so we cannot infer the event name from the | ||
| # function body. | ||
| # In this case, the user will have to implement event_mapping/0. | ||
|
|
||
| msg = """ | ||
| @permit_action module attribute cannot be used with handle_event/3 clauses that do not pattern match directly on the event name. | ||
|
|
||
| To handle events covered by this clause, please implement event_mapping/0 to map event names (strings) to Permit actions (atoms), for example: | ||
|
|
||
| def event_mapping do | ||
| %{ | ||
| "store" => :create, | ||
| "patch" => :update | ||
| } | ||
| end | ||
|
|
||
| Note that, in the presence of other event handlers that pattern match on the event name, mappings defined using @permit_action take precedence over mappings defined in event_mapping/0. | ||
| """ | ||
|
|
||
| raise ArgumentError, msg | ||
|
|
||
| true -> | ||
| :ok | ||
| end | ||
| end | ||
|
|
||
| def __on_definition__(_env, _kind, _name, _args, _guards, _body), do: :ok | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔥