Simplest ComponentHelper works very well

I tried the ComponentHelper for a while and found that it works very well. I love this❤️

module ComponentHelper
  def component(name, locals = {}, &block)
    return render(layout: "components/#{name}", locals: locals, &block) if block_given?

    collection = locals.delete(:collection)
    return render(partial: "components/#{name}", collection: collection, as: locals.delete(:as) || name.to_sym, locals: locals) if collection

    render(partial: "components/#{name}", locals: locals)
  end
end
  • Very simple and clear, no questions.
  • No gems, no additional configs or dependencies required.
  • Does not interrupt Rails-way.
  • The components are just partial-templates, so it can use whatever features partial-templates can do. In other words, you can rediscover the how our partial-templates are strong and fully-featured via the components. The strict locals annotations are the keys.
  • You can call the components with component method.
  • You can put components’ Ruby code in app/components/ or app/views/components/ whichever you want, which organizes the codebase of views naturally. You can easily find that you don’t need to put many view logics into custom helpers.
  • You can test the logic of components as Ruby codes when you extract the logic to rb files.
  • I found that Lookbook can preview ordinal partial-templates as well as ViewComponent, which means Lookbook can be used for the components with ComponentHelper, too. I actually uses Lookbook to preview the components and partials.
  • When the app and the components are grown-up, you can migrate to ViewComponent with ease.
  • tailwindcss-rails is good for styling components (just my view)

The ComponentHelper just encourages and strengthens Rails-way with partial-templates. It is not an additional design-pattern or an additional concept. I believe it is a kind of great invention (it might be that it is too simple to see it is great).

I don’t think the ComponentHelper is an alternative of ViewComponent. Rather it can be a pavement or a red-carpet for preparing ViewComponent. I also believe that It is especially good for educational use.

Just my own view, but this ComponentHelper looks worth to be added to Action View Overview guide. How about this? (I also guess that we don’t need to merge ComponentHelper to Rails. Just adding a short side-story to doc is sufficient for hinting)

Anyway I enjoy with it :face_savoring_food: I just hope the ComponentHelper would get way more popular, especially for younger generations.

1 Like

This guy really loves this helper method :joy: thats the magic of Rails so many eye opening magical things you can do.

1 Like

Hello Hatta-san, I think your ComponentHelper looks neat, but unless it’s native to Rails, I don’t think it belongs in the guides. We can’t possibly cover all of the useful components (in literal sense) the Rails ecosystem has to offer and also make documentation suitable for the framework. Another reason is because the code isn’t maintained by the Rails team, they can’t be responsible if it breaks, even if this code looks only using public APIs the team cannot change it or improve it if they wanted.

You could try make a case it be added to Rails, but that would require a PR and some use-case that make it applicable for majority of applications.

I don’t know if one already exists, but maybe a Rails Recipes style project might be a good place to share it otherwise, like a community maintained project for Rails helpers and plugins. rubyonrails-core is also a reasonable place to share and discuss these things. :pray:

2 Likes

What blogs, recipes/cookbooks would you recommend for rails to learn ‘eye opening magical things’?? I’d love to come across an extensive index I can feed to Claude. I’m pretty new to the development game and have been focused on React/Hono/Workers and mastering the ins and outs of developing full-stack on Cloudflare, but I made a connect recently who’s looking to hire and he prefers Rails for serious projects, and after learning more about it I can see why..

1 Like