stimulus-controller 0.0.0 → 0.0.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 798abb5408b0b8f06b89fb2497c59039b5711b148cd0851d4171162822a7100f
4
- data.tar.gz: ed2461a44d469b9812d2ca949ca54d29a53ff5c6d44eb1faa36a5af5b0f338ad
3
+ metadata.gz: a1289592ab39d575381186ce813c1d93e3a64d50583a20e1736901d18279002d
4
+ data.tar.gz: 7264c820dbca16ce2a2d044c111421f76187bef2d4509101ae72eb8fa3b4adf9
5
5
  SHA512:
6
- metadata.gz: e0bcefaf47bbd36c620f68c82f2b58fd0258a9fc566b35c4ea899d0ef02402cba75f753d9933e0f00f789979a73d9dc98271f2efc855a2e37516477be99b227b
7
- data.tar.gz: '0835bc01065a15c232b9aa69da6f0983281189964edfead38bdb363290767462d6f4bb0480723f862ada265893fb455ba1915bc99f0acdf90be79d5fc24ba835'
6
+ metadata.gz: 04234d9c0b1aa96b30b223c6c856103e4d8b15789a0c7f056e76ed82bdb1d26c848a4943687f4d536edbf19246f55cbf819f45224a0b6f6b25f7d95ebe661fca
7
+ data.tar.gz: bc7b984d51952d393dbff8af30900f55a50d65b0714bf6478c75c81539b43d8151dea5e2a2cc528e1590d4da421db768bc179cc498ecffd1289b58831f1757ba
data/README.md CHANGED
@@ -1,3 +1,49 @@
1
1
  # Stimulus Controller
2
2
 
3
- Rubygem for add a view helper to a rails app for creating a stimulus controller
3
+ A Ruby gem that adds a view helper to a Rails app for embedding a stimulus controller with its most essential functions.
4
+
5
+ It does not support all functions from Stimulus because, in our opinion, Stimulus should only be
6
+ used as an initialiser with Hotwired and nothing else.
7
+
8
+ ### Installation
9
+
10
+ Add
11
+
12
+ ```ruby
13
+ gem 'stimulus-controller'
14
+ ```
15
+
16
+ to your gemfile and run `bundle`
17
+
18
+ ### Usage example with .haml
19
+
20
+ ```haml
21
+ = stimulus_controller("my-controller", { items: ["Item-1"] }) do |sc|
22
+ = content_tag :div, data: sc.click('myAction') do
23
+ = "my-Action-Button"
24
+ ```
25
+
26
+ Arguments:
27
+ - Controller Identifier
28
+ - Values (optional):
29
+ - It must be a Hash.
30
+ - When the value is a hash or an array, such as in the example of the string '["Item-1"]', it is automatically parsed as JSON.
31
+ - Options for the rendered tag
32
+
33
+ Have a controller like:
34
+
35
+ ```javascript
36
+ import {Controller} from "@hotwired/stimulus"
37
+
38
+ export default class extends Controller {
39
+ static values = {
40
+ items: Array
41
+ }
42
+
43
+ // And now you can use this.itemsValue on your code
44
+ }
45
+ ```
46
+
47
+ ### Stimulus is mainly a initializer!
48
+
49
+ If you want reactive front-end components, please use our [svelte-on-rails](https://svelte-on-rails.dev/) gem :)
@@ -1,5 +1,3 @@
1
1
  require "stimulus_controller/railtie" if defined?(Rails)
2
-
3
- module StimulusController
4
-
5
- end
2
+ require "stimulus_controller/view_helpers"
3
+ require "stimulus_controller/helper"
@@ -0,0 +1,53 @@
1
+ module StimulusController
2
+ class Helper
3
+
4
+ def initialize(controller_identifier)
5
+ @ctrl_id = controller_identifier
6
+ end
7
+
8
+ def value(key, value)
9
+ k = "#{@ctrl_id}-#{key}-value"
10
+ {
11
+ k.to_sym => value
12
+ }
13
+ end
14
+
15
+ def action(action)
16
+ {
17
+ action: action.sub('->#', "->#{@ctrl_id}#"),
18
+ }
19
+ end
20
+
21
+ def click(function)
22
+ {
23
+ action: "click->#{@ctrl_id}##{function}",
24
+ }
25
+ end
26
+
27
+ def change(function)
28
+ {
29
+ action: "change->#{@ctrl_id}##{function}",
30
+ }
31
+ end
32
+
33
+ def keyup(function)
34
+ {
35
+ action: "keyup->#{@ctrl_id}##{function}",
36
+ }
37
+ end
38
+
39
+ def target(name)
40
+ {
41
+ "#{@ctrl_id}-target": name,
42
+ }
43
+ end
44
+
45
+ def props(options)
46
+
47
+ change(options[:change])
48
+ .merge(
49
+ target(options[:target])
50
+ )
51
+ end
52
+ end
53
+ end
@@ -1,9 +1,9 @@
1
1
  module StimulusController
2
2
  class Railtie < Rails::Railtie
3
3
 
4
- initializer "csedl_stimulus_dropdown.view_helpers" do
4
+ initializer "stimulus_controller.view_helpers" do
5
5
  ActiveSupport.on_load(:action_view) do
6
- include CsedlStimulusDropdown::ViewHelpers
6
+ include StimulusController::ViewHelpers
7
7
  end
8
8
  end
9
9
  end
@@ -1,7 +1,35 @@
1
1
  module StimulusController
2
2
  module ViewHelpers
3
- def stimulus_controller
3
+ # first argument: stimulus identifier/path
4
+ # second argument: values (must be a hash)
5
+ # following argument: class, id, of the wrapping tag
6
+ def stimulus_controller(controller_identifier, values = {}, **options)
4
7
 
8
+ if values.present?
9
+ _values = values.each_with_object({}) do |(k, v), h|
10
+ _k = "#{controller_identifier}-#{k}-value"
11
+ if [Array, Hash].include?(v.class)
12
+ h[_k] = v.to_json
13
+ else
14
+ h[_k] = v
15
+ end
16
+ end
17
+ options[:data] = _values
18
+ end
19
+
20
+ hlp = StimulusController::Helper.new(controller_identifier)
21
+
22
+ options = options.deep_merge(
23
+ {
24
+ data: {
25
+ controller: controller_identifier
26
+ }
27
+ }
28
+ )
29
+
30
+ content_tag :div, options do
31
+ yield hlp
32
+ end
5
33
  end
6
34
  end
7
35
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stimulus-controller
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christian Sedlmair
@@ -18,6 +18,7 @@ extra_rdoc_files: []
18
18
  files:
19
19
  - README.md
20
20
  - lib/stimulus-controller.rb
21
+ - lib/stimulus_controller/helper.rb
21
22
  - lib/stimulus_controller/railtie.rb
22
23
  - lib/stimulus_controller/view_helpers.rb
23
24
  homepage: https://gitlab.com/sedl/stimulus-controller
@@ -35,7 +36,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
35
36
  requirements:
36
37
  - - ">="
37
38
  - !ruby/object:Gem::Version
38
- version: '0'
39
+ version: '3.0'
39
40
  required_rubygems_version: !ruby/object:Gem::Requirement
40
41
  requirements:
41
42
  - - ">="