phlex-ruby/phlex.fun

Document working with Rails helpers — both built in and custom

Opened this issue · 4 comments

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.

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.

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.

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.

@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.