Class: Phlex::Stimulus::Components::Controller Abstract
- Defined in:
- lib/phlex/stimulus/components/controller.rb
Overview
Abstract class for Phlex components that wrap Stimulus.js controllers.
Defined Under Namespace
Classes: ActionDefinition, TargetDefinition
Class Attribute Summary collapse
-
.controller_name ⇒ Object
Name of the Stimulus controller.
Class Method Summary collapse
- .action_defs ⇒ Object
-
.actions(*actions) ⇒ Object
Register actions that exist on the Stimulus controller.
- .controller_path ⇒ Object
-
.data_target_key ⇒ Object
Returns the full name of the data attribute for the controller's targets eg.
-
.dispatched(event_name) ⇒ Object
Constructs a Stimulus dispatched event name eg.
-
.param(param_name) ⇒ Object
Constructs a Stimulus action parameter name eg.
- .target_defs ⇒ Object
-
.target_key ⇒ Object
Returns the name of the data attribute for the controller's targets without the initial
"data-"eg. -
.targets(*targets) ⇒ Object
Register targets that exist on the Stimulus controller.
Instance Method Summary collapse
Methods inherited from Base
#attrbool, #class_list, #class_list!, #event, #h, #image_exists?, #inspect, #newline
Class Attribute Details
.controller_name ⇒ Object
Name of the Stimulus controller
66 67 68 |
# File 'lib/phlex/stimulus/components/controller.rb', line 66 def controller_name @controller_name end |
Class Method Details
.action_defs ⇒ Object
78 79 80 |
# File 'lib/phlex/stimulus/components/controller.rb', line 78 def action_defs @action_defs ||= [] end |
.actions(*actions) ⇒ Object
Register actions that exist on the Stimulus controller. Will define typed getter methods for each action.
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/phlex/stimulus/components/controller.rb', line 127 def actions(*actions) actions.each do |action| action_def = ActionDefinition.new( component: self, action_name: action.to_s, ) action_defs << action_def instance_eval <<~RUBY, __FILE__, __LINE__ + 1 def #{action_def.ruby_action_method_name} "\#{controller_name}##{action}" end RUBY end end |
.controller_path ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/phlex/stimulus/components/controller.rb', line 69 def controller_path name = controller_name sections = name.split('--') subpath = sections.map(&:underscore).join('/') "app/javascript/controllers/#{subpath}_controller.ts" end |
.data_target_key ⇒ Object
Returns the full name of the data attribute for the controller's targets
eg. "data-summary-target"
119 120 121 |
# File 'lib/phlex/stimulus/components/controller.rb', line 119 def data_target_key "data-#{target_key}" end |
.dispatched(event_name) ⇒ Object
Constructs a Stimulus dispatched event name eg.
SummaryController.dispatched('ready') #=> "summary:ready"
103 104 105 |
# File 'lib/phlex/stimulus/components/controller.rb', line 103 def dispatched(event_name) "#{controller_name}:#{event_name}" end |
.param(param_name) ⇒ Object
Constructs a Stimulus action parameter name eg.
SummaryController.param('id') #=> "summary-id-param"
93 94 95 |
# File 'lib/phlex/stimulus/components/controller.rb', line 93 def param(param_name) "#{controller_name}-#{param_name}-param" end |
.target_defs ⇒ Object
83 84 85 |
# File 'lib/phlex/stimulus/components/controller.rb', line 83 def target_defs @target_defs ||= [] end |
.target_key ⇒ Object
Returns the name of the data attribute for the controller's targets without the initial "data-"
eg. "summary-target"
111 112 113 |
# File 'lib/phlex/stimulus/components/controller.rb', line 111 def target_key "#{controller_name}-target" end |
.targets(*targets) ⇒ Object
Register targets that exist on the Stimulus controller. Will define typed getter methods for each target.
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/phlex/stimulus/components/controller.rb', line 147 def targets(*targets) targets.each do |target| target_str = target.to_s target_def = TargetDefinition.new( component: self, target_name: target_str, ) target_defs << target_def instance_eval <<~RUBY, __FILE__, __LINE__ + 1 def #{target_def.ruby_target_method_name} #{target_str.inspect} end RUBY end end |
Instance Method Details
#controller_name ⇒ Object
167 168 169 |
# File 'lib/phlex/stimulus/components/controller.rb', line 167 def controller_name self.class.controller_name end |
#view_template(&block) ⇒ Object
173 174 175 176 177 |
# File 'lib/phlex/stimulus/components/controller.rb', line 173 def view_template(&block) div(data: { controller: self.class.controller_name }) do block&.call end end |