Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optionally inherit from ApplicationController #150

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions lib/generators/stimulus/stimulus_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,20 @@ def controller_name
def stimulus_attribute_value(controller_name)
controller_name.gsub(/\//, "--").gsub("_", "-")
end

def controller_import
if application_controller_exists?
'import ApplicationController from "./application_controller"'
else
'import { Controller } from "@hotwired/stimulus"'
end
end

def parent_controller
application_controller_exists? ? "ApplicationController" : "Controller"
end

def application_controller_exists?
File.exist?(Rails.root.join("app/javascript/controllers/application_controller.js"))
end
end
6 changes: 3 additions & 3 deletions lib/generators/stimulus/templates/controller.js.tt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Controller } from "@hotwired/stimulus"
<%= controller_import %>

// Connects to data-controller="<%= @attribute %>"
export default class extends Controller {
// Connects to data-controller="<%= attribute %>"
export default class extends <%= parent_controller %> {
connect() {
}
}