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

Document working with Rails helpers — both built in and custom #10

Open
tubsandcans opened this issue Dec 21, 2022 · 4 comments
Open
Assignees

Comments

@tubsandcans
Copy link

tubsandcans commented Dec 21, 2022

Many Rails apps utilize the app/helpers feature for code reusability when rendering. I cannot figure out how to integrate the methods defined under app/helpers/ into Phlex's rendering pipeline, which seems very much like the kind of thing that would be pointed out under a Rails migration guide.

The docs make it clear how to create custom "helpers" for tags, which is nice but I absolutely need the logic in my helpers to become accessible inside Phlex classes/views. Also, the docs like to use the word "helpers" both for Rails built-in helpers and Plex's helpers, without a single mention of the existence of user-defined helpers under app/helpers, which is mildly confusing.

@joeldrapper joeldrapper transferred this issue from phlex-ruby/phlex Jan 7, 2023
@joeldrapper
Copy link
Collaborator

joeldrapper commented Jan 7, 2023

Thanks for opening this issue. I agree the docs need some major clarification here and I’ve transferred it to the phlex.fun repo where the docs are hosted.

@tubsandcans
Copy link
Author

After working all week converting a bunch of views over to Phlex (love it btw!), I've found I was able to extract a decent amount of helpers into Phlex components. Compute-only helper methods used in multiple places can be placed in a file views/<controller_name>/helpers.rb under Helpers module. I then declare module-methods and call them where needed. Compute only methods used in one place can either be included in this module or a private method in the component that references it.

This or a similar suggestion about what to do with custom helper methods under app/helpers would at least get people headed in the right direction.

@nimmolo
Copy link

nimmolo commented Aug 19, 2024

Found this thread when searching for advice for what to do with presenter logic - similar question, I guess. I'm psyched to start using Phlex but i'm unsure where to put presenter logic - i'm guessing similar to @tubsandcans included module above.

Example - I have an ImagePresenter that I call with an Image instance and args, that returns a presenter object with formatted data for outputting the image as an HTML component potentially wrapped in a whole UI (image caption, lightbox markup, voting UI, info about objects associated with the image...).

I instantiate the presenter it in a few places in the code where we need an image "with the works":

  • standalone images
  • carousel images
  • carousel thumbnails

Since these would be separate Phlex components, I obviously want to reuse the logic. Advice welcome, even a short word in this thread, but some documentation of this pattern would be great too.

@joeldrapper
Copy link
Collaborator

@nimmolo depending on your requirements, either approach — having each component take a common presenter object, or having them share behaviour through inheritance from some common module or superclass — could make sense. You may also be able to have your carousel component just render the standalone image component for its own images and thumbnails.

The library doesn’t have any opinions on this.

@joeldrapper joeldrapper changed the title "Migrating To Phlex" for Rails docs glaring omission Document working with Rails helpers — both built in and custom Sep 9, 2024
@joeldrapper joeldrapper self-assigned this Sep 9, 2024
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