Using helpers as shorthand methods #385
-
Hi I'm considering introducing the following "pattern?" to my project where I use Rails view helpers as shorthand methods for common component uses. Here is my helper: # app/helpers/component_helpers/glyph_helper.rb
module ComponentHelpers::GlyphHelper
def a_glyph(glyph, **args)
render Atoms::GlyphComponent.new(glyph, **args)
end
def a_glyph_back(label)
render Atoms::GlyphComponent.new('#arrow', label: t("app.#{label}"), glyph_deg: 0)
end
end And then use it like this: = link_to a_glyph_back(:dashboard), root_path Thanks in advance 👍 |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
I proposed a similar way of doing this at #1293 that requires you to write less code: https://gist.github.com/bradgessler/cd91d5f2e333b99eed2fc0c3f76d827b Currently it doesn't support namespaces, but if you had a class named If you add namespace support share the gist back here. As an aside, I'm tempted to write a gem that more aggressively integrates with Rails. |
Beta Was this translation helpful? Give feedback.
-
To add to the conversation, I view the (admittedly somewhat verbose) explicit references to ViewComponents to be a feature, not a bug. They enable readers of code to quickly understand, without ambiguity, what will be rendered. It also greatly enhances the ability for static analysis tools to interpret view code, for similar reasons. |
Beta Was this translation helpful? Give feedback.
To add to the conversation, I view the (admittedly somewhat verbose) explicit references to ViewComponents to be a feature, not a bug. They enable readers of code to quickly understand, without ambiguity, what will be rendered. It also greatly enhances the ability for static analysis tools to interpret view code, for similar reasons.