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



76
77
# File 'lib/vident/base.rb', line 76

def after_initialise
end

#before_initialise(_attrs) ⇒ Object

Override this method to perform any initialisation before attributes are set



72
73
# File 'lib/vident/base.rb', line 72

def before_initialise(_attrs)
end

#clone(overrides = {}) ⇒ Object



79
80
81
82
# File 'lib/vident/base.rb', line 79

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.



125
126
127
# File 'lib/vident/base.rb', line 125

def component_class_name
  self.class.component_name
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



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

def default_controller_path
  self.class.identifier_name_path
end

#element_classesObject

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



121
122
# File 'lib/vident/base.rb', line 121

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



91
92
93
# File 'lib/vident/base.rb', line 91

def id
  @id.presence || random_id
end

#inspect(klass_name = "Component") ⇒ Object



84
85
86
87
# File 'lib/vident/base.rb', line 84

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



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/vident/base.rb', line 103

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

#prepare_attributes(attributes) ⇒ Object

Raises:

  • (NotImplementedError)


67
68
69
# File 'lib/vident/base.rb', line 67

def prepare_attributes(attributes)
  raise NotImplementedError
end

#render_classes(erb_defined_classes = nil) ⇒ Object

Generates the full list of HTML classes for the component



131
132
133
134
135
136
137
138
# File 'lib/vident/base.rb', line 131

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



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

def stimulus_identifier
  self.class.stimulus_identifier
end