Class: ViewComponentReflex::Reflex

Inherits:
StimulusReflex::Reflex
  • Object
show all
Includes:
CableReady::Broadcaster
Defined in:
lib/view_component_reflex/reflex.rb

Class Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/view_component_reflex/reflex.rb', line 76

def method_missing(name, *args)
  super unless respond_to_missing?(name)
  state.each do |k, v|
    component.instance_variable_set(k, v)
  end
  name.to_proc.call(component, *args)
  refresh! unless @prevent_refresh
end

Class Attribute Details

.component_classObject

Returns the value of attribute component_class.



6
7
8
# File 'lib/view_component_reflex/reflex.rb', line 6

def component_class
  @component_class
end

Instance Method Details

#method(name) ⇒ Object

SR’s delegate_call_to_reflex in channel.rb uses method to gather the method parameters, but since we’re abusing method_missing here, that’ll always fail



63
64
65
# File 'lib/view_component_reflex/reflex.rb', line 63

def method(name)
  name.to_sym.to_proc
end

#prevent_refresh!Object



85
86
87
# File 'lib/view_component_reflex/reflex.rb', line 85

def prevent_refresh!
  @prevent_refresh = true
end

#refresh!(primary_selector = nil, *rest) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/view_component_reflex/reflex.rb', line 9

def refresh!(primary_selector = nil, *rest)
  save_state

  if primary_selector.nil? && !component.can_render_to_string?
    primary_selector = selector
  end
  if primary_selector
    prevent_refresh!

    controller.process(url_params[:action])
    document = Nokogiri::HTML(controller.response.body)
    [primary_selector, *rest].each do |s|
      html = document.css(s)
      if html.present?
        cable_ready[channel.stream_name].morph(
          selector: s,
          html: html.inner_html,
          children_only: true,
          permanent_attribute_name: "data-reflex-permanent"
        )
      end
    end
  else
    refresh_component!
  end
  cable_ready.broadcast
end

#refresh_all!Object



52
53
54
# File 'lib/view_component_reflex/reflex.rb', line 52

def refresh_all!
  refresh!("body")
end

#refresh_component!Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/view_component_reflex/reflex.rb', line 37

def refresh_component!
  component.tap do |k|
    k.define_singleton_method(:key) do
      element.dataset[:key]
    end
  end
  document = Nokogiri::HTML(controller.render_component_to_string(component))
  cable_ready[channel.stream_name].morph(
    selector: selector,
    children_only: true,
    html: document.css(selector).inner_html,
    permanent_attribute_name: "data-reflex-permanent"
  )
end

#respond_to_missing?(name, _ = false) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/view_component_reflex/reflex.rb', line 67

def respond_to_missing?(name, _ = false)
  !!name.to_proc
end

#selectorObject



56
57
58
# File 'lib/view_component_reflex/reflex.rb', line 56

def selector
  "[data-controller~=\"#{stimulus_controller}\"][data-key=\"#{element.dataset[:key]}\"]"
end