Class: ViewComponentReflex::Component

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



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

def key
  @key
end

Class Method Details

.after_reflex(*args, &blk) ⇒ Object



35
36
37
# File 'app/components/view_component_reflex/component.rb', line 35

def after_reflex(*args, &blk)
  queue_callback(:after, args, blk)
end

.around_reflex(*args, &blk) ⇒ Object



39
40
41
# File 'app/components/view_component_reflex/component.rb', line 39

def around_reflex(*args, &blk)
  queue_callback(:around, args, blk)
end

.before_reflex(*args, &blk) ⇒ Object



31
32
33
# File 'app/components/view_component_reflex/component.rb', line 31

def before_reflex(*args, &blk)
  queue_callback(:before, args, blk)
end

.callbacks(key) ⇒ Object



20
21
22
23
# File 'app/components/view_component_reflex/component.rb', line 20

def callbacks(key)
  @callbacks ||= {}
  @callbacks[key] ||= []
end

.init_stimulus_reflexObject



7
8
9
10
11
# File 'app/components/view_component_reflex/component.rb', line 7

def init_stimulus_reflex
  factory = ViewComponentReflex::ReflexFactory.new(self)
  @stimulus_reflex ||= factory.reflex
  wire_up_callbacks if factory.new?
end

.queue_callback(key, args, blk) ⇒ Object



13
14
15
16
17
18
# File 'app/components/view_component_reflex/component.rb', line 13

def queue_callback(key, args, blk)
  callbacks(key).push({
    args: args,
    blk: blk
  })
end

.register_callbacks(key) ⇒ Object



25
26
27
28
29
# File 'app/components/view_component_reflex/component.rb', line 25

def register_callbacks(key)
  callbacks(key).each do |cb|
    @stimulus_reflex.send("#{key}_reflex", *cb[:args], &cb[:blk])
  end
end

.stimulus_controllerObject



50
51
52
# File 'app/components/view_component_reflex/component.rb', line 50

def self.stimulus_controller
  name.chomp("Component").underscore.dasherize.gsub("/", "--")
end

.wire_up_callbacksObject



43
44
45
46
47
# File 'app/components/view_component_reflex/component.rb', line 43

def wire_up_callbacks
  register_callbacks(:before)
  register_callbacks(:after)
  register_callbacks(:around)
end

Instance Method Details

#after_state_initialized(parameters_changed) ⇒ Object



186
187
188
# File 'app/components/view_component_reflex/component.rb', line 186

def after_state_initialized(parameters_changed)
  # called after state component has been hydrated
end

#can_render_to_string?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'app/components/view_component_reflex/component.rb', line 77

def can_render_to_string?
  omitted_from_state.empty?
end

#collection_keyObject



174
175
176
# File 'app/components/view_component_reflex/component.rb', line 174

def collection_key
  nil
end

#component_controller(opts_or_tag = :div, opts = {}, &blk) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/components/view_component_reflex/component.rb', line 58

def component_controller(opts_or_tag = :div, opts = {}, &blk)
  initialize_component

  tag = :div
  options = if opts_or_tag.is_a? Hash
    opts_or_tag
  else
    tag = opts_or_tag
    opts
  end

  options[:data] = {
    controller: self.class.stimulus_controller,
    key: key,
    **(options[:data] || {})
  }
   tag, capture(&blk), options
end

#initialize_componentObject

We can’t truly initialize the component without the view_context, which isn’t available in the ‘initialize` method. We require the developer to wrap components in `component_controller`, so this is where we truly initialize the component. This method is overridden in reflex.rb when the component is re-rendered. The override simply sets @key to element.dataset We don’t want it to initialize the state again, and since we’re rendering the component outside of the view, we need to skip the initialize_key method as well



89
90
91
92
# File 'app/components/view_component_reflex/component.rb', line 89

def initialize_component
  initialize_key
  initialize_state
end

#initialize_keyObject



138
139
140
141
142
143
144
145
146
147
148
149
# File 'app/components/view_component_reflex/component.rb', line 138

def initialize_key
  # we want the erb file that renders the component. `caller` gives the file name,
  # and line number, which should be unique. We hash it to make it a nice number
  erb_file = caller.select { |p| p.match? /.\.html\.(haml|erb|slim)/ }[1]
  key = if erb_file
    Digest::SHA2.hexdigest(erb_file.split(":in")[0])
  else
    ""
  end
  key += collection_key.to_s if collection_key
  @key = key
end

#initialize_stateObject

Note to self: This has to be in the Component class because there are situations where the controller is the one rendering the component so we can’t rely on the component created by the reflex



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'app/components/view_component_reflex/component.rb', line 98

def initialize_state
  return if state_initialized?
  adapter = ViewComponentReflex::Engine.state_adapter
  
  # newly mounted
  if !stimulus_reflex? || adapter.state(request, @key).empty?

    new_state = create_safe_state

    adapter.wrap_write_async do
      adapter.store_state(request, @key, new_state)
      adapter.store_state(request, "#{@key}_initial", new_state)
    end

  # updating a mounted component
  else
    initial_state = adapter.state(request, "#{@key}_initial")

    parameters_changed = []
    adapter.state(request, @key).each do |k, v|
      instance_value = instance_variable_get(k)
      if permit_parameter?(initial_state[k], instance_value)
        parameters_changed << k
        adapter.wrap_write_async do
          adapter.set_state(request, controller, "#{@key}_initial", {k => instance_value})
          adapter.set_state(request, controller, @key, {k => instance_value})
        end
      else
        instance_variable_set(k, v)
      end
    end
    after_state_initialized(parameters_changed)
  end
  @state_initialized = true
end

#omitted_from_stateObject



182
183
184
# File 'app/components/view_component_reflex/component.rb', line 182

def omitted_from_state
  []
end

#permit_parameter?(initial_param, new_param) ⇒ Boolean

Returns:

  • (Boolean)


178
179
180
# File 'app/components/view_component_reflex/component.rb', line 178

def permit_parameter?(initial_param, new_param)
  initial_param != new_param
end

#reflex_data_attributes(reflex) ⇒ Object

Helper to use to create the proper reflex data attributes for an element



152
153
154
155
156
157
158
159
160
161
162
163
# File 'app/components/view_component_reflex/component.rb', line 152

def reflex_data_attributes(reflex)
  action, method = reflex.to_s.split("->")
  if method.nil?
    method = action
    action = "click"
  end

  {
    reflex: "#{action}->#{self.class.name}##{method}",
    key: key
  }
end

#reflex_tag(reflex, name, content_or_options_with_block = {}, options = {}, escape = true, &block) ⇒ Object



165
166
167
168
169
170
171
172
# File 'app/components/view_component_reflex/component.rb', line 165

def reflex_tag(reflex, name, content_or_options_with_block = {}, options = {}, escape = true, &block)
  if content_or_options_with_block.is_a?(Hash)
    merge_data_attributes(content_or_options_with_block, reflex_data_attributes(reflex))
  else
    merge_data_attributes(options, reflex_data_attributes(reflex))
  end
  (name, content_or_options_with_block, options, escape, &block)
end

#safe_instance_variablesObject

def receive_params(old_state, params)

# no op

end



194
195
196
# File 'app/components/view_component_reflex/component.rb', line 194

def safe_instance_variables
  instance_variables - unsafe_instance_variables - omitted_from_state
end

#state_initialized?Boolean

Returns:

  • (Boolean)


134
135
136
# File 'app/components/view_component_reflex/component.rb', line 134

def state_initialized?
  @state_initialized
end

#stimulus_reflex?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'app/components/view_component_reflex/component.rb', line 54

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