stimulus-rails-helpers 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ab19e9c3efbc6ab8b6631c0094ef7a320bbf28c2f110f942f9ea6abb740ce775
4
+ data.tar.gz: 8a51ffae0265d1737530bc360da6de7519ad0980747bb4f0d0b9b678e1219b87
5
+ SHA512:
6
+ metadata.gz: 4e061768233c231107ef8823b11e743ba7680e6acb44492b4c616886842efdce39237f3ed7940a2c86f7cbd12beedc0f87873630b268828c4cc4382e15f1293e
7
+ data.tar.gz: 8fbc2b845f999eb192e3a922098c34148f221154db1323b123c3b991fcd37890c4cdb51eb7732dc9979d178bc50d8c683e423e31962f02c099a58011822bce3d
@@ -0,0 +1,3 @@
1
+ require_relative "stimulus_rails_helpers/rails_helpers"
2
+ require_relative "stimulus_rails_helpers/stimulus_renderer"
3
+ require_relative "stimulus_rails_helpers/railtie"
@@ -0,0 +1,18 @@
1
+ module StimulusRailsHelpers
2
+ module RailsHelpers
3
+ def stimulus_namespace(*namespaces)
4
+ yield(*namespaces.map { |ns| StimulusRenderer.new(namespace: ns, view_context: self) })
5
+ end
6
+ alias stim_ns stimulus_namespace
7
+
8
+ def stimulus_element(element = :div, namespace: nil, controllers: [], values: {}, outlets: {}, actions: {}, targets: {}, **other_attributes, &block)
9
+ StimulusRenderer.new(namespace:, view_context: self).element(element, controllers:, values:, outlets:, actions:, targets:, **other_attributes, &block)
10
+ end
11
+ alias stim_el stimulus_element
12
+
13
+ def stimulus_data(namespace: nil, controllers: [], values: {}, outlets: {}, actions: {}, targets: {}, **other_data_attributes)
14
+ StimulusRenderer.new(namespace:, view_context: self).data(controllers:, values:, outlets:, actions:, targets:, **other_data_attributes)
15
+ end
16
+ alias stim_data stimulus_data
17
+ end
18
+ end
@@ -0,0 +1,9 @@
1
+ module StimulusRailsHelpers
2
+ class Railtie < Rails::Railtie
3
+ initializer "stimulus_rails_helpers.action_view" do
4
+ ActiveSupport.on_load(:action_view) do
5
+ include StimulusRailsHelpers::RailsHelpers
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,114 @@
1
+ module StimulusRailsHelpers
2
+ class StimulusRenderer
3
+ module AttributeRenderers
4
+ class Base
5
+ include ActionView::Helpers::OutputSafetyHelper
6
+
7
+ attr_reader :descriptors, :namespace
8
+
9
+ def initialize(descriptors, namespace: [])
10
+ @descriptors = descriptors
11
+ @namespace = namespace
12
+ end
13
+
14
+ private
15
+
16
+ def prefix
17
+ @prefix ||= namespace.present? ? parse_namespace(namespace) : ""
18
+ end
19
+
20
+ def parse_namespace(namespace)
21
+ if namespace.is_a?(String) || namespace.is_a?(Symbol)
22
+ kebabize(namespace.to_s) + "--"
23
+ elsif namespace.is_a?(Array)
24
+ namespace.map { |name| parse_namespace(name) }.join
25
+ elsif namespace.is_a?(Hash)
26
+ namespace.map { |name, children| parse_namespace(name) + parse_namespace(children) }.join
27
+ end
28
+ end
29
+
30
+ def kebabize(string_or_symbol)
31
+ string_or_symbol.to_s.underscore.tr("_", "-")
32
+ end
33
+ end
34
+
35
+ class Controllers < Base
36
+ def to_s
37
+ Array(descriptors).map { |controller_name| "#{prefix}#{kebabize(controller_name)}" }.join(" ")
38
+ end
39
+ end
40
+
41
+ class Values < Base
42
+ def to_h
43
+ {}.tap do |out|
44
+ descriptors.each do |controller, values|
45
+ values.each do |value_name, value_value|
46
+ out["#{prefix}#{kebabize(controller)}-#{kebabize(value_name)}-value"] = value_value
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+
53
+ class Outlets < Base
54
+ def to_h
55
+ {}.tap do |out|
56
+ descriptors.each do |controller, outlets|
57
+ outlets.each do |target_controller, outlet_selector|
58
+ out["#{prefix}#{kebabize(controller)}-#{prefix}#{kebabize(target_controller)}-outlet"] = outlet_selector
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
64
+
65
+ class Actions < Base
66
+ def to_s
67
+ # Have to consider escaping due to action notation containing `->`
68
+ safe_join(to_a, " ")
69
+ end
70
+
71
+ def to_a
72
+ [].tap do |out|
73
+ descriptors.each do |controller, action_descriptor|
74
+ Array(parse_action_names(controller, action_descriptor)).each { |el| out << el }
75
+ end
76
+ end
77
+ end
78
+
79
+ private
80
+
81
+ # Note that this can return a string, or array of strings
82
+ def parse_action_names(controller_name, descriptor)
83
+ # actions: { controller: :action }
84
+ if descriptor.is_a?(String) || descriptor.is_a?(Symbol)
85
+ # Have to consider escaping due to action notation containing `->`
86
+ "#{prefix}#{kebabize(controller_name)}##{descriptor.to_s.camelize(:lower)}".html_safe
87
+
88
+ # actions: { controller: { event: :action } }
89
+ elsif descriptor.is_a?(Hash)
90
+ dom_event = descriptor.keys.first
91
+ stimulus_function = descriptor.values.first
92
+
93
+ # Have to consider escaping due to action notation containing `->`
94
+ "#{dom_event}->#{parse_action_names(controller_name, stimulus_function)}".html_safe
95
+
96
+ # actions: { controller: [:action, { event: :action }] }
97
+ elsif descriptor.is_a?(Array)
98
+ descriptor.map { |element| parse_action_names(controller_name, element) }
99
+ end
100
+ end
101
+ end
102
+
103
+ class Targets < Base
104
+ def to_h
105
+ {}.tap do |out|
106
+ descriptors.each do |controller, target_name|
107
+ out["#{prefix}#{kebabize(controller)}-target"] = target_name.to_s.camelize(:lower)
108
+ end
109
+ end
110
+ end
111
+ end
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,50 @@
1
+ require_relative "stimulus_renderer/attribute_renderers"
2
+
3
+ module StimulusRailsHelpers
4
+ class StimulusRenderer
5
+ attr_reader :namespace, :view_context
6
+
7
+ def initialize(view_context:, namespace: nil)
8
+ @namespace = namespace
9
+ @view_context = view_context
10
+ end
11
+
12
+ def element(element = :div, controllers: [], values: {}, outlets: {}, actions: {}, targets: {}, **other_attributes, &block)
13
+ generated_data_attributes = data(controllers:, values:, outlets:, actions:, targets:)
14
+ generated_data_attributes.merge!(other_attributes.delete(:data)) if other_attributes[:data]
15
+
16
+ if block_given?
17
+ view_context.content_tag(element, data: generated_data_attributes, **other_attributes, &block)
18
+ else
19
+ view_context.content_tag(element, "", data: generated_data_attributes, **other_attributes)
20
+ end
21
+ end
22
+ alias el element
23
+
24
+ def data(controllers: [], values: {}, outlets: {}, actions: {}, targets: {}, **other_data_attributes)
25
+ returned_attributes = {}
26
+
27
+ if controllers.present?
28
+ returned_attributes[:controller] = AttributeRenderers::Controllers.new(controllers, namespace:).to_s
29
+ end
30
+
31
+ if values.present?
32
+ returned_attributes.merge!(AttributeRenderers::Values.new(values, namespace:).to_h)
33
+ end
34
+
35
+ if outlets.present?
36
+ returned_attributes.merge!(AttributeRenderers::Outlets.new(outlets, namespace:).to_h)
37
+ end
38
+
39
+ if actions.present?
40
+ returned_attributes[:action] = AttributeRenderers::Actions.new(actions, namespace:).to_s
41
+ end
42
+
43
+ if targets.present?
44
+ returned_attributes.merge!(AttributeRenderers::Targets.new(targets, namespace:).to_h)
45
+ end
46
+
47
+ returned_attributes.merge(other_data_attributes)
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,13 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "stimulus-rails-helpers"
3
+ s.version = "0.1.0"
4
+ s.summary = "Some helpers to help tame the task of wrangling Stimulus' data attributes."
5
+ s.authors = ["Jon Gilbraith"]
6
+ s.files = ["stimulus-rails-helpers.gemspec"] + Dir["lib/**/*.rb"]
7
+ s.metadata = { "source_code_uri" => "https://github.com/jongilbraith/stimulus-rails-helpers" }
8
+ s.license = "MIT"
9
+ s.homepage = "https://github.com/jongilbraith/stimulus-rails-helpers"
10
+
11
+ s.add_runtime_dependency "actionview", "~> 3.0.0"
12
+ s.add_runtime_dependency "activesupport", "~> 3.0.0"
13
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: stimulus-rails-helpers
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jon Gilbraith
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2026-06-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: actionview
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 3.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 3.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 3.0.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 3.0.0
41
+ description:
42
+ email:
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - lib/stimulus-rails-helpers.rb
48
+ - lib/stimulus_rails_helpers/rails_helpers.rb
49
+ - lib/stimulus_rails_helpers/railtie.rb
50
+ - lib/stimulus_rails_helpers/stimulus_renderer.rb
51
+ - lib/stimulus_rails_helpers/stimulus_renderer/attribute_renderers.rb
52
+ - stimulus-rails-helpers.gemspec
53
+ homepage: https://github.com/jongilbraith/stimulus-rails-helpers
54
+ licenses:
55
+ - MIT
56
+ metadata:
57
+ source_code_uri: https://github.com/jongilbraith/stimulus-rails-helpers
58
+ post_install_message:
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubygems_version: 3.0.3.1
74
+ signing_key:
75
+ specification_version: 4
76
+ summary: Some helpers to help tame the task of wrangling Stimulus' data attributes.
77
+ test_files: []