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



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

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

.around_reflex(*args, &blk) ⇒ Object



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

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

.before_reflex(*args, &blk) ⇒ Object



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

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

.reflex_methodsObject



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

def self.reflex_methods
  public_instance_methods - ViewComponentReflex::Component.instance_methods - [:call, :"_call_#{name.underscore}"]
end

.register_callbacks(key) ⇒ Object



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

def register_callbacks(key)
  ancestors.each do |klass|
    next unless klass.respond_to?(:callbacks)
    break if klass == ViewComponentReflex::Component
    klass.callbacks(key).each do |cb|
      @stimulus_reflex.send("#{key}_reflex", *cb[:args], &cb[:blk])
    end
  end
end

.reset_callbacksObject



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

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

.state_adapter(what) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/view_component_reflex/component.rb', line 67

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



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

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

.wire_up_callbacksObject



61
62
63
64
65
# File 'lib/view_component_reflex/component.rb', line 61

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

Instance Method Details

#adapterObject



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

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

#after_state_initialized(parameters_changed) ⇒ Object



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

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

#before_renderObject



89
90
91
# File 'lib/view_component_reflex/component.rb', line 89

def before_render
  adapter.extend_component(self)
end

#can_render_to_string?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/view_component_reflex/component.rb', line 112

def can_render_to_string?
  omitted_from_state.empty?
end

#collection_keyObject



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

def collection_key
  nil
end

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



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/view_component_reflex/component.rb', line 93

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



124
125
126
127
# File 'lib/view_component_reflex/component.rb', line 124

def initialize_component
  initialize_key
  initialize_state
end

#initialize_keyObject



194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/view_component_reflex/component.rb', line 194

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
  key = caller.select { |p| p.match? /.\.html\.(haml|erb|slim)/ }.map do |erb_file|
    if erb_file
      erb_file.split(":in")[0]
    else
      ""
    end
  end.join("+")

  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



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
161
162
163
164
165
166
167
168
# File 'lib/view_component_reflex/component.rb', line 133

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



240
241
242
# File 'lib/view_component_reflex/component.rb', line 240

def omitted_from_state
  []
end

#permit_parameter?(initial_param, new_param) ⇒ Boolean

Returns:

  • (Boolean)


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

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



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

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



223
224
225
226
227
228
229
230
# File 'lib/view_component_reflex/component.rb', line 223

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



252
253
254
# File 'lib/view_component_reflex/component.rb', line 252

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



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

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

#state(key) ⇒ Object



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

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

#state_initialized?Boolean

Returns:

  • (Boolean)


190
191
192
# File 'lib/view_component_reflex/component.rb', line 190

def state_initialized?
  @state_initialized
end

#stimulus_reflex?Boolean

Returns:

  • (Boolean)


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

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

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



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

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

#wrap_write_async(&blk) ⇒ Object



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

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