Class: ViewComponentReflex::Component

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

._state_adapterObject (readonly)

Returns the value of attribute _state_adapter.



7
8
9
# File 'lib/view_component_reflex/component.rb', line 7

def _state_adapter
  @_state_adapter
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



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

def key
  @key
end

Class Method Details

.after_reflex(*args, &blk) ⇒ Object



44
45
46
# File 'lib/view_component_reflex/component.rb', line 44

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

.around_reflex(*args, &blk) ⇒ Object



48
49
50
# File 'lib/view_component_reflex/component.rb', line 48

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

.before_reflex(*args, &blk) ⇒ Object



40
41
42
# File 'lib/view_component_reflex/component.rb', line 40

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

.callbacks(key) ⇒ Object



29
30
31
32
# File 'lib/view_component_reflex/component.rb', line 29

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

.init_stimulus_reflexObject



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/view_component_reflex/component.rb', line 9

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

  # Always wire up new callbacks in development
  if Rails.env.development?
    reset_callbacks
    wire_up_callbacks
  elsif factory.new? # only wire up callbacks in production if they haven't been wired up yet
    wire_up_callbacks
  end
end

.queue_callback(key, args, blk) ⇒ Object



22
23
24
25
26
27
# File 'lib/view_component_reflex/component.rb', line 22

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

.register_callbacks(key) ⇒ Object



34
35
36
37
38
# File 'lib/view_component_reflex/component.rb', line 34

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

.reset_callbacksObject



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

def reset_callbacks
  # SR uses :process as the underlying callback key
  @stimulus_reflex.reset_callbacks(:process)
end

.state_adapter(what) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/view_component_reflex/component.rb', line 63

def state_adapter(what)
  if what.is_a?(Symbol) || what.is_a?(String)
    class_name = what.to_s.camelize
    @_state_adapter = StateAdapter.const_get class_name
  else
    @_state_adapter = what
  end
end

.stimulus_controllerObject



73
74
75
# File 'lib/view_component_reflex/component.rb', line 73

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

.wire_up_callbacksObject



57
58
59
60
61
# File 'lib/view_component_reflex/component.rb', line 57

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

Instance Method Details

#adapterObject



162
163
164
# File 'lib/view_component_reflex/component.rb', line 162

def adapter
  self.class._state_adapter || ViewComponentReflex::Engine.state_adapter
end

#after_state_initialized(parameters_changed) ⇒ Object



235
236
237
# File 'lib/view_component_reflex/component.rb', line 235

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

#before_renderObject



81
82
83
# File 'lib/view_component_reflex/component.rb', line 81

def before_render
  adapter.extend_component(self)
end

#can_render_to_string?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/view_component_reflex/component.rb', line 104

def can_render_to_string?
  omitted_from_state.empty?
end

#collection_keyObject



223
224
225
# File 'lib/view_component_reflex/component.rb', line 223

def collection_key
  nil
end

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



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/view_component_reflex/component.rb', line 85

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



116
117
118
119
# File 'lib/view_component_reflex/component.rb', line 116

def initialize_component
  initialize_key
  initialize_state
end

#initialize_keyObject



186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/view_component_reflex/component.rb', line 186

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
          erb_file.split(":in")[0]
          
        else
          ""
        end
  key += collection_key.to_s if collection_key
  @key = Digest::SHA2.hexdigest(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



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/view_component_reflex/component.rb', line 125

def initialize_state
  return if state_initialized?

  adapter.extend_component(self)

  # newly mounted
  if !stimulus_reflex? || state(@key).empty?

    new_state = create_safe_state

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

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

    parameters_changed = []
    state(@key).each do |k, v|
      instance_value = instance_variable_get(k)
      if permit_parameter?(initial_state[k], instance_value)
        parameters_changed << k
        wrap_write_async do
          set_state("#{@key}_initial", { k => instance_value })
          set_state(@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



231
232
233
# File 'lib/view_component_reflex/component.rb', line 231

def omitted_from_state
  []
end

#permit_parameter?(initial_param, new_param) ⇒ Boolean

Returns:

  • (Boolean)


227
228
229
# File 'lib/view_component_reflex/component.rb', line 227

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



201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/view_component_reflex/component.rb', line 201

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



214
215
216
217
218
219
220
221
# File 'lib/view_component_reflex/component.rb', line 214

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



243
244
245
# File 'lib/view_component_reflex/component.rb', line 243

def safe_instance_variables
  instance_variables.reject { |ivar| ivar.start_with?("@__vc") } - unsafe_instance_variables - omitted_from_state
end

#set_state(key, new_state) ⇒ Object



170
171
172
# File 'lib/view_component_reflex/component.rb', line 170

def set_state(key, new_state)
  adapter.set_state(request, controller, key, new_state)
end

#state(key) ⇒ Object



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

def state(key)
  adapter.state(request, key)
end

#state_initialized?Boolean

Returns:

  • (Boolean)


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

def state_initialized?
  @state_initialized
end

#stimulus_reflex?Boolean

Returns:

  • (Boolean)


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

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

#store_state(key, new_state = {}) ⇒ Object



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

def store_state(key, new_state = {})
  adapter.store_state(request, key, new_state)
end

#wrap_write_async(&blk) ⇒ Object



166
167
168
# File 'lib/view_component_reflex/component.rb', line 166

def wrap_write_async(&blk)
  adapter.wrap_write_async(&blk)
end