Class: ViewComponentReflex::Component

Inherits:
ViewComponent::Base
  • Object
show all
Defined in:
app/components/view_component_reflex/component.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.reflex(name, &blk) ⇒ Object



4
5
6
# File 'app/components/view_component_reflex/component.rb', line 4

def reflex(name, &blk)
  stimulus_reflex.reflex(name, &blk)
end

.stimulus_reflexObject



8
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/components/view_component_reflex/component.rb', line 8

def stimulus_reflex
  klass = self
  @stimulus_reflex ||= Object.const_set(name + "Reflex", Class.new(StimulusReflex::Reflex) {
    def state
      ViewComponentReflex::Engine.state_adapter.state(request, element.dataset[:key])
    end

    def refresh!(primary_selector = "[data-controller=\"#{stimulus_controller}\"]", *selectors)
      @channel.send :render_page_and_broadcast_morph, self, [primary_selector, *selectors], {
        dataset: element.dataset.to_h,
        args: [],
        attrs: element.attributes.to_h,
        selectors: ['body'],
        target: "#{self.class.name}##{method_name}",
        url: request.url,
        permanentAttributeName: "data-reflex-permanent"
      }
    end

    def refresh_all!
      refresh!('body')
    end

    def set_state(new_state = {}, primary_selector = nil, *selectors)
      ViewComponentReflex::Engine.state_adapter.set_state(self, element.dataset[:key], new_state)
      refresh!(primary_selector, *selectors)
    end

    before_reflex do |reflex, *args|
      instance_exec(*args, &self.class.callbacks[self.method_name.to_sym]) if self.class.callbacks.include?(self.method_name.to_sym)
      throw :abort
    end

    def self.callbacks
      @callbacks ||= {}
    end

    define_method :stimulus_controller do
      klass.name.chomp("Component").underscore.dasherize
    end

    define_singleton_method(:reflex) do |name, &blk|
      callbacks[name] = blk
      define_method(name) do |*args|
      end
    end
  })
end

Instance Method Details

#initialize_state(obj) ⇒ Object



58
59
60
# File 'app/components/view_component_reflex/component.rb', line 58

def initialize_state(obj)
  @state = obj
end

#keyObject

key is required if you’re using state We can’t initialize the session state in the initial method because it doesn’t have a view_context yet This is the next best place to do it



70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/components/view_component_reflex/component.rb', line 70

def key
  @key ||= caller.find { |p| p.include? ".html.erb" }&.hash.to_s

  # initialize session state
  if !stimulus_reflex? || session[@key].nil?
    ViewComponentReflex::Engine.state_adapter.store_state(request, @key, @state)
    ViewComponentReflex::Engine.state_adapter.store_state(request, "#{@key}_initial", @state)
  else
    # ViewComponentReflex::Engine.state_adapter.reconcile_state(request, @key, @state)
  end
  @key
end

#stateObject



83
84
85
# File 'app/components/view_component_reflex/component.rb', line 83

def state
  ViewComponentReflex::Engine.state_adapter.state(request, key)
end

#stimulus_reflex?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'app/components/view_component_reflex/component.rb', line 62

def stimulus_reflex?
  helpers.controller.instance_variable_get(:@stimulus_reflex)
end