Skip to content

Instantly share code, notes, and snippets.

@lazaronixon
Created September 9, 2024 08:09
Show Gist options
  • Save lazaronixon/52f2d59c09028f92c0f20259a76959d8 to your computer and use it in GitHub Desktop.
Save lazaronixon/52f2d59c09028f92c0f20259a76959d8 to your computer and use it in GitHub Desktop.
TagBuilder
class TagBuilder
def initialize(context, tag_name, content = nil, klass: nil, default_attributes: {}, **attributes, &block)
@context = context
@tag_name = tag_name
@content = content
@klass = klass
@default_attributes = default_attributes
@attributes = attributes
@block = block
end
def render
content_tag tag_name, content, class: merged_classes, **merged_attributes
end
private
attr_reader :context, :tag_name, :content, :klass, :default_attributes, :attributes, :block
delegate :content_tag, :class_names, to: :context
def merged_classes
@merged_classes ||= class_names(klass, attributes.delete(:class))
end
def merged_attributes
@merged_attributes ||= default_attributes.deep_merge(attributes)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment