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

Using turbo boost elements with cancancan #110

Closed
mhenrixon opened this issue Oct 22, 2023 · 8 comments
Closed

Using turbo boost elements with cancancan #110

mhenrixon opened this issue Oct 22, 2023 · 8 comments

Comments

@mhenrixon
Copy link
Contributor

I am trying to build a minimal CMS. For reasons you know about, I decided to give turbo_boost a try. First, to show how I want it to work, I recorded a little video without the can? check.

CleanShot 2023-10-20 at 12 57 20

Now, if I add cancancan and a can? check, I can only click once. Anything within that can? check is rendered only on page load, not on clicking the command buttons.

<%= tag.div id: dom_id(article) do %>
  <% settings_tag = dom_id(article, :settings) %>
  <% edit_body_tag = dom_id(article, :edit_body) %>

  <% if can?(:edit, article) %>
    <div class="article-commands flex space-x-4 align-right">
      <%= toggle_trigger_tag renders: current_partial_path,
                             morphs: dom_id(article),
                             controls: settings_tag,
                             locals: { article: article },
                             assigns: { article: article } do %>

        <% if toggle_target_collapsed? settings_tag %>
          <%= link_to request.path, id: "edit-settings-cmd" do %>
            <%= heroicon "cog", variant: :outline, class: "h-6 w-auto" %>
          <% end %>
        <% else %>
          <%= link_to request.path, id: "show-article-cmd" do %>
            <%= heroicon "x-mark", variant: :outline, class: "h-6 w-auto" %>
          <% end %>
        <% end %>
      <% end %>

      <%= toggle_trigger_tag renders: current_partial_path,
                             morphs: dom_id(article),
                             controls: edit_body_tag,
                             locals: { article: article },
                             assigns: { article: article } do %>

        <% if toggle_target_collapsed? edit_body_tag %>
          <%= link_to request.path, id: "edit-body-cmd" do %>
            <%= heroicon "pencil-square", variant: :outline, class: "h-6 w-auto" %>
          <% end %>
        <% else %>
          <%= link_to request.path, id: "show-article2-cmd" do %>
            <%= heroicon "x-mark", variant: :outline, class: "h-6 w-auto" %>
          <% end %>
        <% end %>
      <% end %>
    </div>
  <% end %>
<% end %>

Something like hotwired/turbo-rails#243, perhaps?

PS. I opened the issue on turbo_boost-commands instead of turbo_boost-elements because it doesn't have much to do with elements, as it is the command that executes that is missing a current user, current ability, or some such.

If that is a wrong assumption, let me know, and I will close and open an issue on elements instead.

Now that I feel lighter, I will debug this issue myself. Wish me luck 🍀

@mhenrixon
Copy link
Contributor Author

mhenrixon commented Oct 25, 2023

@hopsoft @julianrubisch I'm having a hard time figuring this out. What I can tell is that whatever I do to set my Current.user, etc, is not being run. I am guessing turbo_boost is hijacking the request entirely?

@julianrubisch
Copy link
Collaborator

hmm the first question would be if you see anything in the JS console going wrong. then, commands will fail loudly if anything goes wrong on the server side, so... this might not be the case

looking at the elements' command it does use prevent_controller_action, so you might be correct in that there could be some Current setup missing: https://github.com/hopsoft/turbo_boost-elements/blob/main/app/commands/turbo_boost/elements/toggle_command.rb#L4

other than that I fear I only have the canonical answer of "please provide us an MVCE" 🤷‍♂️

@mhenrixon
Copy link
Contributor Author

looking at the elements' command it does use prevent_controller_action, so you might be correct in that there could be some Current setup missing: https://github.com/hopsoft/turbo_boost-elements/blob/main/app/commands/turbo_boost/elements/toggle_command.rb#L4

This seems to be exactly the problem. Any chance to run some before_command hook or otherwise set up the necessary current attributes? I'd also greatly appreciate any alternative suggestions.

It seems like a perfectly valid use case to me so curious how to continue.

@julianrubisch
Copy link
Collaborator

julianrubisch commented Oct 26, 2023

@hopsoft you must've come across this?

Typically this is set in application_command but with elements I suppose we'd have to supply a config option?

@hopsoft
Copy link
Owner

hopsoft commented Nov 22, 2023

Commands run as a before action, but the command before action is effectively prepended and executes before other before actions. This means you'll need to ensure that you perform any setup that the command may need like current_user or Current.user. You can do this for all commands in your ApplicationCommand.

Here's an example of how to do this. https://github.com/hopsoft/turbo_boost-commands/blob/eb2f5cd4a533ec3fbab38d6aed1e16804f77a474/test/dummy/app/controllers/application_controller.rb

@mhenrixon
Copy link
Contributor Author

Commands run as a before action, but the command before action is effectively prepended and executes before other before actions. This means you'll need to ensure that you perform any setup that the command may need like current_user or Current.user. You can do this for all commands in your ApplicationCommand.

Here's an example of how to do this. https://github.com/hopsoft/turbo_boost-commands/blob/eb2f5cd4a533ec3fbab38d6aed1e16804f77a474/test/dummy/app/controllers/application_controller.rb

That works like a charm; just some documentation missing!

@hopsoft
Copy link
Owner

hopsoft commented Nov 22, 2023

Yeah. I definitely need to carve out some time to improve the docs and demos.

@hopsoft
Copy link
Owner

hopsoft commented Nov 23, 2023

Closing in lieu of #112

@hopsoft hopsoft closed this as completed Nov 23, 2023
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

No branches or pull requests

3 participants