Class: Devise::Webauthn::WebauthnCredentialModelGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
Rails::Generators::Migration
Defined in:
lib/generators/devise/webauthn/webauthn_credential_model/webauthn_credential_model_generator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.next_migration_number(dirname) ⇒ Object



19
20
21
# File 'lib/generators/devise/webauthn/webauthn_credential_model/webauthn_credential_model_generator.rb', line 19

def self.next_migration_number(dirname)
  ActiveRecord::Generators::Base.next_migration_number(dirname)
end

Instance Method Details

#generate_migrationObject

TODO: Remove this in favor of strandard model generator with not null modifier (!) once we drop support for Rails < 8.



29
30
31
# File 'lib/generators/devise/webauthn/webauthn_credential_model/webauthn_credential_model_generator.rb', line 29

def generate_migration
  migration_template "webauthn_credential_migration.rb.erb", "db/migrate/create_webauthn_credentials.rb"
end

#generate_modelObject



23
24
25
# File 'lib/generators/devise/webauthn/webauthn_credential_model/webauthn_credential_model_generator.rb', line 23

def generate_model
  invoke "active_record:model", ["webauthn_credential"], migration: false
end

#inject_webauthn_credential_contentObject



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/generators/devise/webauthn/webauthn_credential_model/webauthn_credential_model_generator.rb', line 33

def inject_webauthn_credential_content
  inject_into_file("app/models/webauthn_credential.rb", before: /^end\s*$/) do
    <<~RUBY.indent(2)
      validates :external_id, :public_key, :name, :sign_count, presence: true
      validates :external_id, uniqueness: true

      enum :authentication_factor, { first_factor: 0, second_factor: 1 }

      scope :passkey, -> { first_factor }
    RUBY
  end
end

#show_instructionsObject



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/generators/devise/webauthn/webauthn_credential_model/webauthn_credential_model_generator.rb', line 46

def show_instructions
  say <<~MSG
    WebauthnCredential model has been generated! Next steps:

    1. Run the migration:
       rails db:migrate

    2. Make sure your User model includes :passkey_authenticatable in the devise line:
       devise :database_authenticatable, :passkey_authenticatable, ...
  MSG
end