Class: StimulusRailsHelpers::StimulusRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/stimulus_rails_helpers/stimulus_renderer.rb,
lib/stimulus_rails_helpers/stimulus_renderer/attribute_renderers.rb

Defined Under Namespace

Modules: AttributeRenderers

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(view_context:, namespace: nil) ⇒ StimulusRenderer

Returns a new instance of StimulusRenderer.



7
8
9
10
# File 'lib/stimulus_rails_helpers/stimulus_renderer.rb', line 7

def initialize(view_context:, namespace: nil)
  @namespace = namespace
  @view_context = view_context
end

Instance Attribute Details

#namespaceObject (readonly)

Returns the value of attribute namespace.



5
6
7
# File 'lib/stimulus_rails_helpers/stimulus_renderer.rb', line 5

def namespace
  @namespace
end

#view_contextObject (readonly)

Returns the value of attribute view_context.



5
6
7
# File 'lib/stimulus_rails_helpers/stimulus_renderer.rb', line 5

def view_context
  @view_context
end

Instance Method Details

#data(controllers: [], values: {}, outlets: {}, actions: {}, targets: {}, **other_data_attributes) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/stimulus_rails_helpers/stimulus_renderer.rb', line 24

def data(controllers: [], values: {}, outlets: {}, actions: {}, targets: {}, **other_data_attributes)
  returned_attributes = {}

  if controllers.present?
    returned_attributes[:controller] = AttributeRenderers::Controllers.new(controllers, namespace:).to_s
  end

  if values.present?
    returned_attributes.merge!(AttributeRenderers::Values.new(values, namespace:).to_h)
  end

  if outlets.present?
    returned_attributes.merge!(AttributeRenderers::Outlets.new(outlets, namespace:).to_h)
  end

  if actions.present?
    returned_attributes[:action] = AttributeRenderers::Actions.new(actions, namespace:).to_s
  end

  if targets.present?
    returned_attributes.merge!(AttributeRenderers::Targets.new(targets, namespace:).to_h)
  end

  returned_attributes.merge(other_data_attributes)
end

#element(element = :div, controllers: [], values: {}, outlets: {}, actions: {}, targets: {}, **other_attributes, &block) ⇒ Object Also known as: el



12
13
14
15
16
17
18
19
20
21
# File 'lib/stimulus_rails_helpers/stimulus_renderer.rb', line 12

def element(element = :div, controllers: [], values: {}, outlets: {}, actions: {}, targets: {}, **other_attributes, &block)
  generated_data_attributes = data(controllers:, values:, outlets:, actions:, targets:)
  generated_data_attributes.merge!(other_attributes.delete(:data)) if other_attributes[:data]

  if block_given?
    view_context.(element, data: generated_data_attributes, **other_attributes, &block)
  else
    view_context.(element, "", data: generated_data_attributes, **other_attributes)
  end
end