Skip to content

Instantly share code, notes, and snippets.

@andrewmcodes
Created September 23, 2022 22:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewmcodes/f4517f8089723b6c0c78cde219b97734 to your computer and use it in GitHub Desktop.
Save andrewmcodes/f4517f8089723b6c0c78cde219b97734 to your computer and use it in GitHub Desktop.
Example showing how to use `as` option in ViewComponent
class BoxComponent < ViewComponent::Base
DEFAULT_TAG = :div
def initialize(**opts)
@opts = opts
@tag_opts = opts.except(:as, :classes)
end
def call
tag.send tag_name, content, class: classes, **tag_opts
end
private
attr_reader :opts, :tag_opts
def classes
join_classes [opts[:classes], "foo"]
end
def tag_name
opts.fetch(:as, DEFAULT_TAG).to_sym
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment