Module: Vident::Base

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.stimulus_identifier_from_path(path) ⇒ Object



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

def stimulus_identifier_from_path(path)
  path.split("/").map { |p| p.to_s.dasherize }.join("--")
end

Instance Method Details

#after_initialiseObject

Override this method to perform any initialisation after attributes are set



64
65
# File 'lib/vident/base.rb', line 64

def after_initialise
end

#before_initialise(_attrs) ⇒ Object

Override this method to perform any initialisation before attributes are set



60
61
# File 'lib/vident/base.rb', line 60

def before_initialise(_attrs)
end

#clone(overrides = {}) ⇒ Object



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

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.



104
105
106
# File 'lib/vident/base.rb', line 104

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



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

def default_controller_path
  self.class.identifier_name_path
end

#element_classesObject

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



100
101
# File 'lib/vident/base.rb', line 100

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



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

def id
  @id.presence || random_id
end

#inspect(klass_name = "Component") ⇒ Object



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

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

#outlet_idObject

If connecting an outlet to this specific component instance, use this ID



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

def outlet_id
  @outlet_id ||= [stimulus_identifier, "##{id}"]
end

#prepare_attributes(attributes) ⇒ Object

Raises:

  • (NotImplementedError)


55
56
57
# File 'lib/vident/base.rb', line 55

def prepare_attributes(attributes)
  raise NotImplementedError
end

#render_classes(erb_defined_classes = nil) ⇒ Object

Generates the full list of HTML classes for the component



110
111
112
113
114
115
116
117
# File 'lib/vident/base.rb', line 110

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



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

def stimulus_identifier
  self.class.stimulus_identifier
end