Created
September 9, 2024 08:09
-
-
Save lazaronixon/52f2d59c09028f92c0f20259a76959d8 to your computer and use it in GitHub Desktop.
TagBuilder
This file contains 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
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