hotwired / stimulus-rails

Use Stimulus in your Ruby on Rails app
https://stimulus.hotwired.dev
MIT License
637 stars 91 forks source link

Should controller names be registered as camelCase? #106

Closed nshki closed 2 years ago

nshki commented 2 years ago

First off, thank you all for such a lovely framework. I've been using it in Rails projects and it's been an absolute joy.

Something that I came across during development the other day was a naming convention-related misunderstanding, where I'd be trying to reference a controller with camelCase in a data-action and nothing fired. The way I fixed this, of course, was to use dash-case in the data-action, but it made me wonder if there's merit to letting Stimulus Rails recognize controllers by the camelCase equivalent as well, given that's a standard convention in JavaScript.

For example. if we have a controller declared as:

// app/javascript/controllers/multi_word_controller.js
import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
  logHello() {
    console.log("Hello!")
  }
}

And we try to use it in a view like:

<div data-controller="multi-word">
  <button data-action="click->multi-word#logHello>Log hello</button>
</div>

This will correctly log "Hello!" in the console when the button is clicked. However, if the button element is declared as:

<button data-action="click->multiWord#logHello">Log hello</button>

The logHello method will not fire. Intuitively, I want to write the action declaration the second way, where I'm using camelCase for both the controller name as well as the method name.

Would love to hear a maintainer's take on this!

dhh commented 2 years ago

Ship has sailed long ago on this convention 😄

nshki commented 2 years ago

Okay understood! I totally missed that ship but glad I asked. :)