Created
May 13, 2021 13:56
-
-
Save andrewkatz/f6d9cb545c20a6238be92b82a611012e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
# Easier access to components in templates. So instead of doing: | |
# | |
# <%= render SectionComponent.new(title: 'Foo') do |c| %> | |
# <p>My content</p> | |
# | |
# <% c.subsection do %> | |
# <p>Cool</p> | |
# <% end %> | |
# <% end %> | |
# | |
# You can do: | |
# | |
# <%= section title: 'Foo' do |c| %> | |
# <p>My content</p> | |
# | |
# <% c.subsection do %> | |
# <p>Cool</p> | |
# <% end %> | |
# <% end %> | |
module ComponentHelper | |
Dir.glob(Rails.root.join('app', 'components', '*.rb')).each do |component_file| | |
component = component_file.match(%r{/([\w_]+)_component.rb})[1] | |
define_method component do |**options, &block| | |
klass = "#{component.classify}Component".constantize | |
render klass.new(**options), &block | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment