Class: Stimulus::TagBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/stimulus/tag_builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(view_context) ⇒ TagBuilder

Returns a new instance of TagBuilder.



3
4
5
6
# File 'lib/stimulus/tag_builder.rb', line 3

def initialize(view_context)
  @view_context = view_context
  @view_context.formats |= [:html]
end

Instance Method Details

#action(name, target, **rendering, &block) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/stimulus/tag_builder.rb', line 28

def action(name, target, **rendering, &block)
  return if !block_given? && rendering.blank?

  # render block
  return stimulus_action_tag name, target: target, template: @view_context.capture(&block) if block_given?

  # render view
  stimulus_action_tag name, target: target_name, template: @view_context.render(formats: [:html], **rendering)
end

#append(target, **rendering, &block) ⇒ Object



20
21
22
# File 'lib/stimulus/tag_builder.rb', line 20

def append(target, **rendering, &block)
  action :append, target, **rendering, &block
end

#prepend(target, **rendering, &block) ⇒ Object



24
25
26
# File 'lib/stimulus/tag_builder.rb', line 24

def prepend(target, **rendering, &block)
  action :prepend, target, **rendering, &block
end

#remove(target, **rendering, &block) ⇒ Object



8
9
10
# File 'lib/stimulus/tag_builder.rb', line 8

def remove(target, **rendering, &block)
  action :remove, target, **rendering, &block
end

#replace(target, **rendering, &block) ⇒ Object



12
13
14
# File 'lib/stimulus/tag_builder.rb', line 12

def replace(target, **rendering, &block)
  action :replace, target, **rendering, &block
end

#update(target, **rendering, &block) ⇒ Object



16
17
18
# File 'lib/stimulus/tag_builder.rb', line 16

def update(target, **rendering, &block)
  action :update, target, **rendering, &block
end