The project is in a healthy, maintained state
A simple Rails generator to create new Stimulus controllers
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
 Project Readme

Rails Stimulus Generator

This project adds a new Rails generator to create new Stimulus controllers in your app/javascript/controllers directory.

Installation

Add the gem to your Gemfile

gem 'rails-stimulus-generator'

And bundle:install

Usage

rails g stimulus Hello

Creates app/javascript/controllers/hello_controller.js

The generated Stimulus controller will look like this:

// app/javascript/controllers/hello_controller.js

import { Controller } from 'stimulus'

export default class extends Controller {
  static targets = []

  initialize() {
    // Called once, when the controller is first instantiated
  }

  connect() {
    // Called any time the controller is connected to the DOM
  }

  disconnect() {
    // Called any time the controller is disconnected from the DOM
  }
}

Assumed directory structure

This generator assumes that your Stimulus controllers live in app/javascript/controllers/. If the controllers directory does not exist, the generator will create the directory when it runs.

Tests

Clone this repository to your local machine and run rake test

Acknowledgements

The basic structure of this generator was inspired by stimulus-generator-rails. My preference is to structure my controllers by function instead of by action, so I created this gem for those with a similar need and a standard Rails 6 + webpack directory structure.

License

This gem is available as open source under the terms of the MIT License.