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
localsannotations are the keys. - You can call the components with
componentmethod. - You can put components’ Ruby code in
app/components/orapp/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
I just hope the ComponentHelper would get way more popular, especially for younger generations.