Class: ViewComponentReflex::Component
- Inherits:
-
ViewComponent::Base
- Object
- ViewComponent::Base
- ViewComponentReflex::Component
- Defined in:
- lib/view_component_reflex/component.rb
Class Attribute Summary collapse
-
._state_adapter ⇒ Object
readonly
Returns the value of attribute _state_adapter.
Instance Attribute Summary collapse
-
#key ⇒ Object
readonly
Returns the value of attribute key.
Class Method Summary collapse
- .after_reflex(*args, &blk) ⇒ Object
- .around_reflex(*args, &blk) ⇒ Object
- .before_reflex(*args, &blk) ⇒ Object
- .callbacks(key) ⇒ Object
- .init_stimulus_reflex ⇒ Object
- .queue_callback(key, args, blk) ⇒ Object
- .register_callbacks(key) ⇒ Object
- .reset_callbacks ⇒ Object
- .state_adapter(what) ⇒ Object
- .stimulus_controller ⇒ Object
- .wire_up_callbacks ⇒ Object
Instance Method Summary collapse
- #adapter ⇒ Object
- #after_state_initialized(parameters_changed) ⇒ Object
- #before_render ⇒ Object
- #can_render_to_string? ⇒ Boolean
- #collection_key ⇒ Object
- #component_controller(opts_or_tag = :div, opts = {}, &blk) ⇒ Object
-
#initialize_component ⇒ Object
We can’t truly initialize the component without the view_context, which isn’t available in the ‘initialize` method.
- #initialize_key ⇒ Object
-
#initialize_state ⇒ Object
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.
- #omitted_from_state ⇒ Object
- #permit_parameter?(initial_param, new_param) ⇒ Boolean
-
#reflex_data_attributes(reflex) ⇒ Object
Helper to use to create the proper reflex data attributes for an element.
- #reflex_tag(reflex, name, content_or_options_with_block = {}, options = {}, escape = true, &block) ⇒ Object
-
#safe_instance_variables ⇒ Object
def receive_params(old_state, params) # no op end.
- #set_state(key, new_state) ⇒ Object
- #state(key) ⇒ Object
- #state_initialized? ⇒ Boolean
- #stimulus_reflex? ⇒ Boolean
- #store_state(key, new_state = {}) ⇒ Object
- #wrap_write_async(&blk) ⇒ Object
Class Attribute Details
._state_adapter ⇒ Object (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
#key ⇒ Object (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_reflex ⇒ Object
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 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_callbacks ⇒ Object
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_controller ⇒ Object
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_callbacks ⇒ Object
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
#adapter ⇒ Object
166 167 168 |
# File 'lib/view_component_reflex/component.rb', line 166 def adapter self.class._state_adapter || ViewComponentReflex::Engine.state_adapter end |
#after_state_initialized(parameters_changed) ⇒ Object
240 241 242 |
# File 'lib/view_component_reflex/component.rb', line 240 def after_state_initialized(parameters_changed) # called after state component has been hydrated end |
#before_render ⇒ Object
85 86 87 |
# File 'lib/view_component_reflex/component.rb', line 85 def before_render adapter.extend_component(self) end |
#can_render_to_string? ⇒ Boolean
108 109 110 |
# File 'lib/view_component_reflex/component.rb', line 108 def can_render_to_string? omitted_from_state.empty? end |
#collection_key ⇒ Object
228 229 230 |
# File 'lib/view_component_reflex/component.rb', line 228 def collection_key nil end |
#component_controller(opts_or_tag = :div, opts = {}, &blk) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/view_component_reflex/component.rb', line 89 def component_controller(opts_or_tag = :div, opts = {}, &blk) initialize_component tag = :div = if opts_or_tag.is_a? Hash opts_or_tag else tag = opts_or_tag opts end [:data] = { controller: self.class.stimulus_controller, key: key, **([:data] || {}) } content_tag tag, capture(&blk), end |
#initialize_component ⇒ Object
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
120 121 122 123 |
# File 'lib/view_component_reflex/component.rb', line 120 def initialize_component initialize_key initialize_state end |
#initialize_key ⇒ Object
190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/view_component_reflex/component.rb', line 190 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_state ⇒ Object
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
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 161 162 163 164 |
# File 'lib/view_component_reflex/component.rb', line 129 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_state ⇒ Object
236 237 238 |
# File 'lib/view_component_reflex/component.rb', line 236 def omitted_from_state [] end |
#permit_parameter?(initial_param, new_param) ⇒ Boolean
232 233 234 |
# File 'lib/view_component_reflex/component.rb', line 232 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
206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/view_component_reflex/component.rb', line 206 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
219 220 221 222 223 224 225 226 |
# File 'lib/view_component_reflex/component.rb', line 219 def reflex_tag(reflex, name, = {}, = {}, escape = true, &block) if .is_a?(Hash) merge_data_attributes(, reflex_data_attributes(reflex)) else merge_data_attributes(, reflex_data_attributes(reflex)) end content_tag(name, , , escape, &block) end |
#safe_instance_variables ⇒ Object
def receive_params(old_state, params)
# no op
end
248 249 250 |
# File 'lib/view_component_reflex/component.rb', line 248 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
174 175 176 |
# File 'lib/view_component_reflex/component.rb', line 174 def set_state(key, new_state) adapter.set_state(request, controller, key, new_state) end |
#state(key) ⇒ Object
178 179 180 |
# File 'lib/view_component_reflex/component.rb', line 178 def state(key) adapter.state(request, key) end |
#state_initialized? ⇒ Boolean
186 187 188 |
# File 'lib/view_component_reflex/component.rb', line 186 def state_initialized? @state_initialized end |
#stimulus_reflex? ⇒ Boolean
81 82 83 |
# File 'lib/view_component_reflex/component.rb', line 81 def stimulus_reflex? helpers.controller.instance_variable_get(:@stimulus_reflex) end |
#store_state(key, new_state = {}) ⇒ Object
182 183 184 |
# File 'lib/view_component_reflex/component.rb', line 182 def store_state(key, new_state = {}) adapter.store_state(request, key, new_state) end |
#wrap_write_async(&blk) ⇒ Object
170 171 172 |
# File 'lib/view_component_reflex/component.rb', line 170 def wrap_write_async(&blk) adapter.wrap_write_async(&blk) end |