Module: Vident::Base
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/vident/base.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#after_initialise ⇒ Object
Override this method to perform any initialisation after attributes are set.
-
#before_initialise(_attrs) ⇒ Object
Override this method to perform any initialisation before attributes are set.
- #clone(overrides = {}) ⇒ Object
-
#component_class_name ⇒ Object
(also: #js_event_name_prefix)
A HTML class name that can helps identify the component type in the DOM or for styling purposes.
-
#default_controller_path ⇒ Object
The `component` class name is used to create the controller name.
-
#element_classes ⇒ Object
This can be overridden to return an array of extra class names.
-
#id ⇒ Object
Generate a unique ID for a component, can be overridden as required.
- #inspect(klass_name = "Component") ⇒ Object
-
#parent_element(**options) ⇒ Object
(also: #root)
Helper to create the main element.
- #prepare_attributes(attributes) ⇒ Object
-
#render_classes(erb_defined_classes = nil) ⇒ Object
Generates the full list of HTML classes for the component.
- #stimulus_identifier ⇒ Object
Class Method Details
.stimulus_identifier_from_path(path) ⇒ Object
146 147 148 |
# File 'lib/vident/base.rb', line 146 def stimulus_identifier_from_path(path) path.split("/").map { |p| p.to_s.dasherize }.join("--") end |
Instance Method Details
#after_initialise ⇒ Object
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_name ⇒ Object 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.
121 122 123 |
# File 'lib/vident/base.rb', line 121 def component_class_name self.class.component_name end |
#default_controller_path ⇒ Object
The `component` class name is used to create the controller name. The path of the Stimulus controller when none is explicitly set
142 143 144 |
# File 'lib/vident/base.rb', line 142 def default_controller_path self.class.identifier_name_path end |
#element_classes ⇒ Object
This can be overridden to return an array of extra class names
117 118 |
# File 'lib/vident/base.rb', line 117 def element_classes end |
#id ⇒ Object
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 |
# File 'lib/vident/base.rb', line 99 def parent_element(**) @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(**()) end end |
#prepare_attributes(attributes) ⇒ Object
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
127 128 129 130 131 132 133 134 |
# File 'lib/vident/base.rb', line 127 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_identifier ⇒ Object
136 137 138 |
# File 'lib/vident/base.rb', line 136 def stimulus_identifier self.class.stimulus_identifier end |