-
Notifications
You must be signed in to change notification settings - Fork 43
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
before_action not calling method #50
Comments
it works for me , make sure authorize is the last def in the class , its something to do with how the macro is working , if you still have issues I'll add a code sample from a working project |
Thanks for you quick response! Here is my controller: https://github.com/drujensen/crystal-blog/blob/master/app/controllers/post_controller.cr When I inspect the @callback_methods, it doesn't have any of the @actions I list. It has the @types instead. Not sure how this works but it seems like you would want to register the actions instead? |
I have a fix but I'm not happy with it. Basically, to register the macro before_action(callback, *actions)
{% for action in actions %}
def _before_{{action.id}}_{{callback.id}}
@before_callbacks["{{action.id}}"] = [] of (-> Bool) unless @before_callbacks["{{action.id}}"]?
@before_callbacks["{{action.id}}"] << ->{{callback.id}}
end
{% end %}
end Before, this method was creating callbacks for the @types instead of the @actions. Let me know if you have a better way of getting the list of actions. Can you grab them from the other macro called |
Well, macros aren't my strong side, I'll leave that to @sdogruyol or @Codcore . But, I know you have two options regarding # before_action :method_to_execute_before_actions, only: [:methods, :to, :use, :the, :before, :action]
before_action :authorized?, only: [:execute, :new, :notifications, :dash_board] Or # This will apply "authorized?" to every action
before_action :authorized? |
I verified that the Specifically: macro before_action(callback, only=[] of Symbol)
{% if only.empty? %}
{% only = @type.methods.map(&.name.stringify) %}
{% end %} If you don't specify the So the callbacks are indexed on any types that are defined in the controller, instead of the actions defined in the other macro called Is there a way to do this in Crystal? |
Hey @drujensen This may help you understand the actions https://github.com/Codcore/amethyst/blob/master/src/amethyst/base/controller.cr#L54-L60 |
Thanks @sdogruyol . How do I get the list of actions in the other macro called |
Well first you have to register it via actions macro and then i think you can access the actions via @actions |
unfortunately, @actions is not available in the scope of the macro. |
I see we need to tinker about macro loading order :( |
Also we need specs for before_action callbacks |
@sdogruyol are you saying that it should be available in the macros? Let me try some things if thats true. |
Yeah.If you register it as an action first then it should be available. |
EDIT: Nope, false alarm. Its still not available even when I register the |
We have to rethink about before_callback implementation and have some proper specs 👍 |
@sdogruyol Ok. Thanks for your help. For now, I will use the only: option and pass in all the actions. |
Your welcome @drujensen i'll tackle this issue |
So, I'm probably doing something wrong, but I have a
before_action :authorize
in my controller and it doesn't seem to call thedef authorize
method.I see the macros defined in the controllers for before_action. Any idea's?
The text was updated successfully, but these errors were encountered: