stimulus-controller 0.0.0 → 0.0.2
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 +4 -4
- data/README.md +49 -1
- data/lib/stimulus-controller.rb +2 -4
- data/lib/stimulus_controller/helper.rb +53 -0
- data/lib/stimulus_controller/railtie.rb +2 -2
- data/lib/stimulus_controller/view_helpers.rb +29 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fba998e8b72b91d84ca482b10b2e8e7bb0fedfa0b875b4a18f1b5e5461c5097b
|
4
|
+
data.tar.gz: 72b1e7b36a1d38648b59cf3aa2a00fc740c3c78c627c9d8f33af0a14e6c2e585
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 06d819531887cbfe6964e9229923a2bc2195c6b1128c9247aad11ae7f8c50469e9dcf36ad97a3d8982347a0cfe8aaa456be39dc5e7f8cd18d9eb3c819916801d
|
7
|
+
data.tar.gz: 8e034be8a25b551ddb0bb4bc98e86a4a9b4d2752ab7c3b5bd729659220f84a3e637cdd2be894180d899f941a6bf197bab7047e0e8880151ce5901b0c89e8e0d8
|
data/README.md
CHANGED
@@ -1,3 +1,51 @@
|
|
1
1
|
# Stimulus Controller
|
2
2
|
|
3
|
-
|
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"] }, class: 'my-css-class') 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
|
+
myAction() {
|
44
|
+
alert(`Items are: «${JSON.stringify(this.itemsValue)}»`)
|
45
|
+
}
|
46
|
+
}
|
47
|
+
```
|
48
|
+
|
49
|
+
### Stimulus is mainly a initializer!
|
50
|
+
|
51
|
+
If you want reactive front-end components, please use our [svelte-on-rails](https://svelte-on-rails.dev/) gem :)
|
data/lib/stimulus-controller.rb
CHANGED
@@ -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 "
|
4
|
+
initializer "stimulus_controller.view_helpers" do
|
5
5
|
ActiveSupport.on_load(:action_view) do
|
6
|
-
include
|
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
|
-
|
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.
|
4
|
+
version: 0.0.2
|
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
|
- - ">="
|