stimulus-rails 0.7.3 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0fa354a81d7e2305ae1f6e333ca31844b04f1bce376b9df1b502574c73725728
4
- data.tar.gz: 3192954bcd22ebb74c854751646b7089092ed1066a35e778a50bb2384bc0ce30
3
+ metadata.gz: 0ffd052c8f30205c21d994289dc5f4938f7926db658ac93a9e6264f1ff63810f
4
+ data.tar.gz: bafd756e667681009804b4387a04674d8af0b8a868873978f17bfea2759196de
5
5
  SHA512:
6
- metadata.gz: abde8638a567a585f5ec33a9a194da0e28c3263cabb03ca7a42e6628c91fb636a8ef9218bf82856c30acc502cfbae18d59164758254601275cbe40379be83b1c
7
- data.tar.gz: 0cd6800704eea7cfaf913b02b460475b2e6e8b3e20ec28c0db28cf3e10effd9d8f60fec42442a5b28d1e91f2966e59a6cd14bed5c0f3a279a5cc0a6ef22897ca
6
+ metadata.gz: 9fb58d85d883bd881b1ad7a9d1478a98f57d26b41f9c844d27ffebe6020f0f7d578952e401e8057ab5555b69df63def79f8900f4b54b6115c83f25afb5080fa3
7
+ data.tar.gz: 386899154a67d5e79c9ca964757ba40d387367ce65309cbb704d1075d898d095f748478198eb30918af3783b9a7a6938e697b75f8bcf3f20bd014146d753f7d3
data/README.md CHANGED
@@ -7,6 +7,10 @@ Stimulus for Rails makes it easy to use this modest framework with both import-m
7
7
 
8
8
  ## Installation
9
9
 
10
+ This gem is automatically configured for applications made with Rails 7+ (unless `--skip-hotwire` is passed to the generator). But if you're on Rails 6, you can install it with the installer or manually:
11
+
12
+ ### Installing with installer
13
+
10
14
  1. Add the `stimulus-rails` gem to your Gemfile: `gem 'stimulus-rails'`
11
15
  2. Run `./bin/bundle install`.
12
16
  3. Run `./bin/rails stimulus:install`
@@ -15,6 +19,89 @@ The installer will automatically detect whether you're using an [import map](htt
15
19
 
16
20
  The installer amends your JavaScript entry point at `app/javascript/application.js` to import the `app/javascript/controllers/index.js` file, which is responsible for setting up your Stimulus application and registering your controllers.
17
21
 
22
+ ### Installing manually
23
+
24
+ Note that we recommend running the installer as described above. But the following is helpful if you encounter errors while running the installer (e.g., if there are conflicts with existing files) or if you just like doing stuff manually. Follow the instructions for import map *or* JavaScript bundler, depending on your setup.
25
+
26
+ 1. Add the `stimulus-rails` gem to your Gemfile: `gem 'stimulus-rails'`
27
+ 2. Run `./bin/bundle install`.
28
+
29
+ #### With import map
30
+
31
+ 3. Create `app/javascript/controllers/index.js` and load your controllers like this:
32
+ ```javascript
33
+ import { application } from "controllers/application"
34
+
35
+ // Eager load all controllers defined in the import map under controllers/**/*_controller
36
+ import { eagerLoadControllersFrom } from "@hotwired/stimulus-loading"
37
+ eagerLoadControllersFrom("controllers", application)
38
+ ```
39
+
40
+ 4. Create `app/javascript/controllers/application.js` with the following content:
41
+ ```javascript
42
+ import { Application } from "@hotwired/stimulus"
43
+
44
+ const application = Application.start()
45
+
46
+ // Configure Stimulus development experience
47
+ application.debug = false
48
+ window.Stimulus = application
49
+
50
+ export { application }
51
+ ```
52
+
53
+ 5. Add the following line to `app/javascript/application.js` to import all your controllers:
54
+ ```javascript
55
+ import "controllers"
56
+ ```
57
+
58
+ 6. Finally, Pin Stimulus and controllers in `config/importmap.rb` by adding:
59
+ ```ruby
60
+ pin "@hotwired/stimulus", to: "stimulus.min.js", preload: true
61
+ pin "@hotwired/stimulus-loading", to: "stimulus-loading.js", preload: true
62
+ pin_all_from "app/javascript/controllers", under: "controllers"
63
+
64
+ ```
65
+
66
+ #### With JavaScript bundler
67
+
68
+ 3. Create `app/javascript/controllers/index.js` and load your controllers.
69
+
70
+ Make sure to change `HelloController` to an actual controller and repeat for every controller you have or use the command mentioned in the comments below:
71
+ ```javascript
72
+ // This file is auto-generated by ./bin/rails stimulus:manifest:update
73
+ // Run that command whenever you add a new controller or create them with
74
+ // ./bin/rails generate stimulus controllerName
75
+
76
+ import { application } from "./application"
77
+
78
+ import HelloController from "./hello_controller"
79
+ application.register("hello", HelloController)
80
+ ```
81
+
82
+ 4. Create `app/javascript/controllers/application.js` with the following content:
83
+ ```javascript
84
+ import { Application } from "@hotwired/stimulus"
85
+
86
+ const application = Application.start()
87
+
88
+ // Configure Stimulus development experience
89
+ application.debug = false
90
+ window.Stimulus = application
91
+
92
+ export { application }
93
+ ```
94
+
95
+ 5. Add the following line to `app/javascript/application.js` to import all your controllers:
96
+ ```javascript
97
+ import "controllers"
98
+ ```
99
+
100
+ 6. Finally, add the Stimulus package to yarn:
101
+ ```bash
102
+ yarn add @hotwired/stimulus
103
+ ```
104
+
18
105
 
19
106
  ## Usage with import map
20
107
 
@@ -19,7 +19,7 @@ function extractControllerNamesFrom(element) {
19
19
  function loadController(name) {
20
20
  import(controllerFilename(name))
21
21
  .then(module => registerController(name, module))
22
- .catch(error => console.log(`Failed to autoload controller: ${name}`, error))
22
+ .catch(error => console.error(`Failed to autoload controller: ${name}`, error))
23
23
  }
24
24
 
25
25
  function controllerFilename(name) {
@@ -21,7 +21,7 @@ function registerControllerFromPath(path, under, application) {
21
21
 
22
22
  import(path)
23
23
  .then(module => application.register(name, module.default))
24
- .catch(error => console.log(`Failed to register controller: ${name} (${path})`, error))
24
+ .catch(error => console.error(`Failed to register controller: ${name} (${path})`, error))
25
25
  }
26
26
 
27
27
  console.warn("stimulus-importmap-autoload.js has been deprecated in favor of stimulus-loading.js")
@@ -21,9 +21,11 @@ function registerControllerFromPath(path, under, application) {
21
21
  .replace(/\//g, "--")
22
22
  .replace(/_/g, "-")
23
23
 
24
- import(path)
25
- .then(module => registerController(name, module, application))
26
- .catch(error => console.debug(`Failed to register controller: ${name} (${path})`, error))
24
+ if (!(name in registeredControllers)) {
25
+ import(path)
26
+ .then(module => registerController(name, module, application))
27
+ .catch(error => console.error(`Failed to register controller: ${name} (${path})`, error))
28
+ }
27
29
  }
28
30
 
29
31
 
@@ -64,9 +66,11 @@ function extractControllerNamesFrom(element) {
64
66
  }
65
67
 
66
68
  function loadController(name, under, application) {
67
- import(controllerFilename(name, under))
68
- .then(module => registerController(name, module, application))
69
- .catch(error => console.debug(`Failed to autoload controller: ${name}`, error))
69
+ if (!(name in registeredControllers)) {
70
+ import(controllerFilename(name, under))
71
+ .then(module => registerController(name, module, application))
72
+ .catch(error => console.error(`Failed to autoload controller: ${name}`, error))
73
+ }
70
74
  }
71
75
 
72
76
  function controllerFilename(name, under) {
@@ -74,8 +78,8 @@ function controllerFilename(name, under) {
74
78
  }
75
79
 
76
80
  function registerController(name, module, application) {
77
- if (name in registeredControllers) return
78
-
79
- application.register(name, module.default)
80
- registeredControllers[name] = true
81
+ if (!(name in registeredControllers)) {
82
+ application.register(name, module.default)
83
+ registeredControllers[name] = true
84
+ }
81
85
  }
@@ -4,13 +4,17 @@ class StimulusGenerator < Rails::Generators::NamedBase # :nodoc:
4
4
  source_root File.expand_path("templates", __dir__)
5
5
 
6
6
  def copy_view_files
7
- @attribute = stimulus_attribute_value(name)
8
- template "controller.js", "app/javascript/controllers/#{name}_controller.js"
9
- rails_command "stimulus:manifest:update" if Rails.root.join("package.json").exist?
7
+ @attribute = stimulus_attribute_value(controller_name)
8
+ template "controller.js", "app/javascript/controllers/#{controller_name}_controller.js"
9
+ rails_command "stimulus:manifest:update" unless Rails.root.join("config/importmap.rb").exist?
10
10
  end
11
11
 
12
12
  private
13
- def stimulus_attribute_value(name)
14
- name.gsub(/\//, "--").gsub("_", "-")
13
+ def controller_name
14
+ name.underscore.gsub(/_controller$/, "")
15
+ end
16
+
17
+ def stimulus_attribute_value(controller_name)
18
+ controller_name.gsub(/\//, "--").gsub("_", "-")
15
19
  end
16
20
  end
@@ -1,5 +1,6 @@
1
1
  // This file is auto-generated by ./bin/rails stimulus:manifest:update
2
- // Run that command whenever you add a new controller
2
+ // Run that command whenever you add a new controller or create them with
3
+ // ./bin/rails generate stimulus controllerName
3
4
 
4
5
  import { application } from "./application"
5
6
 
@@ -11,9 +11,12 @@ say "Import Stimulus controllers"
11
11
  append_to_file "app/javascript/application.js", %(import "controllers"\n)
12
12
 
13
13
  say "Pin Stimulus"
14
- append_to_file "config/importmap.rb" do <<-RUBY
15
- pin "@hotwired/stimulus", to: "stimulus.min.js", preload: true
16
- pin "@hotwired/stimulus-loading", to: "stimulus-loading.js", preload: true
17
- pin_all_from "app/javascript/controllers", under: "controllers"
18
- RUBY
19
- end
14
+ say %(Appending: pin "@hotwired/stimulus", to: "stimulus.min.js", preload: true")
15
+ append_to_file "config/importmap.rb", %(pin "@hotwired/stimulus", to: "stimulus.min.js", preload: true\n)
16
+
17
+ say %(Appending: pin "@hotwired/stimulus-loading", to: "stimulus-loading.js", preload: true)
18
+ append_to_file "config/importmap.rb", %(pin "@hotwired/stimulus-loading", to: "stimulus-loading.js", preload: true\n)
19
+
20
+ say "Pin all controllers"
21
+ say %(Appending: pin_all_from "app/javascript/controllers", under: "controllers")
22
+ append_to_file "config/importmap.rb", %(pin_all_from "app/javascript/controllers", under: "controllers"\n)
@@ -11,7 +11,7 @@ if (Rails.root.join("app/javascript/application.js")).exist?
11
11
  say "Import Stimulus controllers"
12
12
  append_to_file "app/javascript/application.js", %(import "./controllers"\n)
13
13
  else
14
- say %(You must import "./controllers" in your JavaScript entrypoint), :red
14
+ say %(Couldn't find "app/javascript/application.js".\nYou must import "./controllers" in your JavaScript entrypoint file), :red
15
15
  end
16
16
 
17
17
  say "Install Stimulus"
@@ -1,3 +1,3 @@
1
1
  module Stimulus
2
- VERSION = "0.7.3"
2
+ VERSION = "1.0.4"
3
3
  end
@@ -37,7 +37,8 @@ namespace :stimulus do
37
37
 
38
38
  File.open(Rails.root.join("app/javascript/controllers/index.js"), "w+") do |index|
39
39
  index.puts "// This file is auto-generated by ./bin/rails stimulus:manifest:update"
40
- index.puts "// Run that command whenever you add a new controller"
40
+ index.puts "// Run that command whenever you add a new controller or create them with"
41
+ index.puts "// ./bin/rails generate stimulus controllerName"
41
42
  index.puts
42
43
  index.puts %(import { application } from "./application")
43
44
  index.puts manifest
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stimulus-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.3
4
+ version: 1.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Stephenson
@@ -10,10 +10,10 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2021-11-24 00:00:00.000000000 Z
13
+ date: 2022-02-22 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
- name: rails
16
+ name: railties
17
17
  requirement: !ruby/object:Gem::Requirement
18
18
  requirements:
19
19
  - - ">="
@@ -76,7 +76,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
76
76
  - !ruby/object:Gem::Version
77
77
  version: '0'
78
78
  requirements: []
79
- rubygems_version: 3.2.22
79
+ rubygems_version: 3.2.32
80
80
  signing_key:
81
81
  specification_version: 4
82
82
  summary: A modest JavaScript framework for the HTML you already have.