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



151
152
153
# File 'lib/vident/base.rb', line 151

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



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

def after_initialise
end

#before_initialise(_attrs) ⇒ Object

Override this method to perform any initialisation before attributes are set



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

def before_initialise(_attrs)
end

#clone(overrides = {}) ⇒ Object



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

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.



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

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



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

def default_controller_path
  self.class.identifier_name_path
end

#element_classesObject

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



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

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



87
88
89
# File 'lib/vident/base.rb', line 87

def id
  @id.presence || random_id
end

#inspect(klass_name = "Component") ⇒ Object



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

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



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

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
    element_attrs = options
      .except(:id, :element_tag, :html_options, :controller, :controllers, :actions, :targets, :named_classes, :data_maps)
      .merge(
        stimulus_options_for_component(options)
      )
    klass.new(**element_attrs)
  end
end

#prepare_attributes(attributes) ⇒ Object

Raises:

  • (NotImplementedError)


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

def prepare_attributes(attributes)
  raise NotImplementedError
end

#render_classes(erb_defined_classes = nil) ⇒ Object

Generates the full list of HTML classes for the component



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

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



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

def stimulus_identifier
  self.class.stimulus_identifier
end