Skip to content

Commit 7ce21ac

Browse files
feat: allow specifying which controller to override
1 parent e05e015 commit 7ce21ac

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

lib/generators/devise/webauthn/controllers_generator.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@ class ControllersGenerator < Rails::Generators::Base
1616
source_root File.expand_path("templates/controllers", __dir__)
1717
argument :scope, required: true,
1818
desc: "The scope to create controllers in, e.g. users, admins"
19+
class_option :controllers, aliases: "-c", type: :array,
20+
desc: "Select specific controllers to generate (#{CONTROLLERS.join(', ')})"
1921

2022
def create_controllers
2123
@scope_prefix = scope.blank? ? "" : "#{scope.camelize}::"
22-
CONTROLLERS.each do |name|
24+
controllers = options[:controllers] || CONTROLLERS
25+
controllers.each do |name|
2326
template "#{name}_controller.rb",
2427
"app/controllers/#{scope}/#{name}_controller.rb"
2528
end

spec/generators/devise/webauthn/controllers_generator_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,30 @@
3434
assert_file "app/controllers/users/two_factor_authentications_controller.rb", /Users::TwoFactorAuthenticationsController/
3535
end
3636
end
37+
38+
context "when using -c to select specific controllers" do
39+
let(:generator_instance) do
40+
generator(["users"], controllers: %w[passkeys two_factor_authentications])
41+
end
42+
43+
it "generates only the selected controllers" do
44+
invoke generator_instance
45+
46+
assert_file "app/controllers/users/passkeys_controller.rb", /Users::PasskeysController/
47+
assert_file "app/controllers/users/two_factor_authentications_controller.rb",
48+
/Users::TwoFactorAuthenticationsController/
49+
50+
assert_no_file "app/controllers/users/second_factor_webauthn_credentials_controller.rb"
51+
end
52+
end
53+
54+
context "when -c includes an unknown controller" do
55+
let(:generator_instance) do
56+
generator(["users"], controllers: %w[passkeys not_a_real_controller])
57+
end
58+
59+
it "raises an error" do
60+
expect { invoke generator_instance }.to raise_error(Thor::Error)
61+
end
62+
end
3763
end

0 commit comments

Comments
 (0)