Skip to content
This repository has been archived by the owner on Apr 12, 2023. It is now read-only.

current_user helper in components? #21

Closed
danturu opened this issue Aug 4, 2019 · 8 comments
Closed

current_user helper in components? #21

danturu opened this issue Aug 4, 2019 · 8 comments

Comments

@danturu
Copy link

danturu commented Aug 4, 2019

How could I make current_user available in all components without passing it each time? include ApplicationHelper doesn't work.

P.S. I use Sorcery.

@skyksandr
Copy link

Current attributes can be a solution. More info here:
https://api.rubyonrails.org/classes/ActiveSupport/CurrentAttributes.html

@danturu
Copy link
Author

danturu commented Aug 4, 2019

@skyksandr thank you, but I'd prefer not to use them.

@joelhawksley
Copy link
Member

@rosendi currently, we're explicitly passing in the values we need to render our components. I'd be curious to hear what you come up with on your end, though.

@joelhawksley
Copy link
Member

Closing this issue due to staleness. Feel free to reopen if desired ❤️

@danturu
Copy link
Author

danturu commented Sep 6, 2019

I think we should find a way :-) Technically yes - we have to pass all the values (props) we need to render a component, but helpers are more like utilities. In React I would import them and use directly:

import projectPath from '....';

const ProjectItem = ({ project }) => (
  <Link to={project_path(project)}>Go to project</Link>
} 

Currently if I define a helper in a controller, I have to compute its value and explicitly pass it via attributes.

Let's say I have a polymorphic controller (real-world example) for comments. Users can comment posts and images.

module Post
  class CommentsController < CommentsController
    helper_method :edit_comment_path

    def edit_comment_path(comment, *args)
      edit_post_comment_path(subject, comment, *args)
    end

  private

      def subject
        @subject ||= Post.find(...)
      end
  end
end

module Image
  class CommentsController < CommentsController
    helper_method :edit_comment_path

    def edit_comment_path(comment, *args)
      edit_image_comment_path(subject, comment, *args)
    end

  private

      def subject
        @subject ||= Comment.find(...)
      end
  end
end

My comment's component should be decoupled from post, image, etc. Currently I do:

<%= render Comment, comment: comment, edit_comment_path: edit_comment_path(comment) %>

but I think this way should work too:

<%= render Comment, comment: comment %>

and the html of the comment's component then would be:

<%= link_to 'Edit comment', edit_comment_path(comment) %>

@danturu
Copy link
Author

danturu commented Feb 16, 2020

@joelhawksley Hello! It's been a while. Do you still pass all helpers explicitly? It sounds like a non-rails way to pass all current_* such as current_user, current_organization, current_project, etc..

@danturu
Copy link
Author

danturu commented Feb 16, 2020

ahhh....

Screenshot 2020-02-16 at 17 47 33

@joelhawksley
Copy link
Member

@rosendi currently, we do inject the current state explicitly, as doing so enables us to change that state in unit tests.

You can also use the helpers. escape hatch, but it will make your components more difficult to unit test.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants