Class: Phlex::Stimulus::Components::Controller Abstract

Inherits:
Base
  • Object
show all
Defined in:
lib/phlex/stimulus/components/controller.rb

Overview

This class is abstract.

Abstract class for Phlex components that wrap Stimulus.js controllers.

Defined Under Namespace

Classes: ActionDefinition, TargetDefinition

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#attrbool, #class_list, #class_list!, #event, #h, #image_exists?, #inspect, #newline

Class Attribute Details

.controller_nameObject

Name of the Stimulus controller

: String



66
67
68
# File 'lib/phlex/stimulus/components/controller.rb', line 66

def controller_name
  @controller_name
end

Class Method Details

.action_defsObject

: -> Array



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.

: (*Symbol) -> void



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/phlex/stimulus/components/controller.rb', line 117

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_pathObject

: -> String



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_keyObject

Returns the full name of the data attribute for the controller's targets eg. "data-summary-target"

: -> String



109
110
111
# File 'lib/phlex/stimulus/components/controller.rb', line 109

def data_target_key
  "data-#{target_key}"
end

.dispatched(event_name) ⇒ Object

Constructs a Stimulus dispatched event name eg.

SummaryController.dispatched('ready') #=> "summary:ready"

: (String) -> String



93
94
95
# File 'lib/phlex/stimulus/components/controller.rb', line 93

def dispatched(event_name)
  "#{controller_name}:#{event_name}"
end

.target_defsObject

: -> Array



83
84
85
# File 'lib/phlex/stimulus/components/controller.rb', line 83

def target_defs
  @target_defs ||= []
end

.target_keyObject

Returns the name of the data attribute for the controller's targets without the initial "data-" eg. "summary-target"

: -> String



101
102
103
# File 'lib/phlex/stimulus/components/controller.rb', line 101

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.

: (*Symbol) -> void



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/phlex/stimulus/components/controller.rb', line 137

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_nameObject

: -> String



157
158
159
# File 'lib/phlex/stimulus/components/controller.rb', line 157

def controller_name
  self.class.controller_name
end

#view_template(&block) ⇒ Object

: ?{ -> void } -> void



163
164
165
166
167
# File 'lib/phlex/stimulus/components/controller.rb', line 163

def view_template(&block)
  div(data: { controller: self.class.controller_name }) do
    block&.call
  end
end