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



60
61
62
63
64
65
66
67
# File 'lib/view_component_reflex/reflex.rb', line 60

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_class ⇒ Object

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



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

def method(name)
  component.method(name.to_sym)
end

#prevent_refresh! ⇒ Object



69
70
71
# File 'lib/view_component_reflex/reflex.rb', line 69

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
# 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?
        morph(s, html.inner_html)
      end
    end
  else
    refresh_component!
  end
end

#refresh_all! ⇒ Object



41
42
43
# File 'lib/view_component_reflex/reflex.rb', line 41

def refresh_all!
  refresh!("body")
end

#refresh_component! ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/view_component_reflex/reflex.rb', line 31

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))
  morph selector, document.css(selector).inner_html
end

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

Returns:

  • (Boolean)


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

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

#selector ⇒ Object



45
46
47
# File 'lib/view_component_reflex/reflex.rb', line 45

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