Module: Vident::Base

Extended by:
ActiveSupport::Concern
Defined in:
lib/vident/base.rb

Instance Method Summary collapse

Instance Method Details

#after_initialiseObject

Override this method to perform any initialisation after attributes are set



132
133
# File 'lib/vident/base.rb', line 132

def after_initialise
end

#before_initialise(_attrs) ⇒ Object

Override this method to perform any initialisation before attributes are set



128
129
# File 'lib/vident/base.rb', line 128

def before_initialise(_attrs)
end

#clone(overrides = {}) ⇒ Object



135
136
137
138
# File 'lib/vident/base.rb', line 135

def clone(overrides = {})
  new_set = to_hash.merge(**overrides)
  self.class.new(**new_set)
end

#component_class_nameObject Also known as: js_event_name_prefix

A HTML class name that can helps identify the component type in the DOM or for styling purposes.



181
182
183
# File 'lib/vident/base.rb', line 181

def component_class_name
  self.class.component_name
end

#component_modified_timeObject

TODO: Move to caching module Component modified time which is combined with other cache key attributes to generate cache key for an instance



202
203
204
# File 'lib/vident/base.rb', line 202

def component_modified_time
  self.class.component_modified_time
end

#default_controller_pathObject

The `component` class name is used to create the controller name. The path of the Stimulus controller when none is explicitly set



208
209
210
# File 'lib/vident/base.rb', line 208

def default_controller_path
  self.class.identifier_name_path
end

#element_classesObject

This can be overridden to return an array of extra class names



177
178
# File 'lib/vident/base.rb', line 177

def element_classes
end

#idObject

Generate a unique ID for a component, can be overridden as required. Makes it easier to setup things like ARIA attributes which require elements to reference by ID. Note this overrides the `id` accessor



147
148
149
# File 'lib/vident/base.rb', line 147

def id
  @id.presence || random_id
end

#inspect(klass_name = "Component") ⇒ Object



140
141
142
143
# File 'lib/vident/base.rb', line 140

def inspect(klass_name = "Component")
  attr_text = attributes.map { |k, v| "#{k}=#{v.inspect}" }.join(", ")
  "#<#{self.class.name}<Vident::#{klass_name}> #{attr_text}>"
end

#parent_element(**options) ⇒ Object Also known as: root

Helper to create the main element



159
160
161
162
163
164
165
166
167
168
169
# File 'lib/vident/base.rb', line 159

def parent_element(**options)
  @parent_element ||= begin
    # Note: we cant mix phlex and view_component render contexts
    klass = if self.class.ancestors.include?(Phlex::HTML)
      RootComponent::UsingPhlexHTML
    else
      RootComponent::UsingViewComponent
    end
    klass.new(**stimulus_options_for_component(options))
  end
end

#prepare_attributes(attributes) ⇒ Object

Raises:

  • (NotImplementedError)


123
124
125
# File 'lib/vident/base.rb', line 123

def prepare_attributes(attributes)
  raise NotImplementedError
end

#render_classes(erb_defined_classes = nil) ⇒ Object

Generates the full list of HTML classes for the component



187
188
189
190
191
192
193
194
# File 'lib/vident/base.rb', line 187

def render_classes(erb_defined_classes = nil)
  # TODO: avoid pointless creation of arrays
  base_classes = [component_class_name] + Array.wrap(element_classes)
  base_classes += Array.wrap(erb_defined_classes) if erb_defined_classes
  classes_on_component = attribute(:html_options)&.fetch(:class, nil)
  base_classes += Array.wrap(classes_on_component) if classes_on_component
  produce_style_classes(base_classes)
end

#stimulus_identifierObject



196
197
198
# File 'lib/vident/base.rb', line 196

def stimulus_identifier
  self.class.stimulus_identifier
end