Skip to content

Instantly share code, notes, and snippets.

@boardfish
Created December 17, 2021 10:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boardfish/3e42fc52d2cb2a0e95e47628ec82a03e to your computer and use it in GitHub Desktop.
Save boardfish/3e42fc52d2cb2a0e95e47628ec82a03e to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
class ApplicationComponent < ViewComponent::Base
end
<div style="background: blue;">Blue row: <%= content %></div>
# frozen_string_literal: true
class BlueRowComponent < RowComponent
end
<div style="background: red;">Red row: <%= content %></div>
# frozen_string_literal: true
class RedRowComponent < RowComponent
end
<div><%= content %></div>
# frozen_string_literal: true
class RowComponent < ApplicationComponent
end
<div>Table color is <%= @color %></div>
<% rows.each do |row| %><%= row %><% end %>
# frozen_string_literal: true
class TableComponent < ApplicationComponent
renders_many :rows, -> { @color == :red ? RedRowComponent.new : BlueRowComponent.new }
def initialize(color:)
@color = color
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment