Class: RailsAuthentication::Generators::AuthenticationGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
ActiveRecord::Generators::Migration, Features::Confirmable, Features::Invitable, Features::Lockable, Features::MagicLink, Features::Ott, Features::Recoverable, Features::Registerable, Features::Rememberable, Features::Timeoutable, Features::Trackable, Features::Validatable
Defined in:
lib/generators/authentication/authentication_generator.rb

Overview

Shadows Rails' built-in bin/rails generate authentication command: the explicit authentication:authentication namespace is checked before Rails' own rails:authentication, so the bare authentication invocation lands here. It runs the built-in generator first, then layers the feature set on top.

The Ruby namespace is RailsAuthentication (not Authentication, which would give the same Thor namespace implicitly) because Rails 8's base generator creates an Authentication controller concern in the host app — a top-level constant this gem must not squat on.

Constant Summary collapse

FEATURES =
%i[
  confirmable recoverable registerable rememberable trackable
  timeoutable validatable lockable invitable
].freeze

Instance Method Summary collapse

Instance Method Details

#customize_session_layerObject

Confirmable, Rememberable, Trackable, Timeoutable, and Lockable all hook into the sign-in flow, so the base generator's session files are replaced with versions rendered from the enabled feature set. Overwriting is safe: the base copies were written moments ago by this same run.



131
132
133
134
135
# File 'lib/generators/authentication/authentication_generator.rb', line 131

def customize_session_layer
  template "app/controllers/sessions_controller.rb", force: true
  template "app/controllers/concerns/authentication.rb", force: true
  template "app/views/sessions/new.html.erb", force: true
end

#format_user_modelObject

Runs after every feature install so the blank line separates the concern includes (if any) from the rest of the class body, no matter which features are enabled.



123
124
125
# File 'lib/generators/authentication/authentication_generator.rb', line 123

def format_user_model
  inject_into_file "app/models/user.rb", "\n", after: /(?:  include \w+Concern\n)+/, force: true
end

#install_base_authenticationObject



71
72
73
74
# File 'lib/generators/authentication/authentication_generator.rb', line 71

def install_base_authentication
  say "Running Rails' built-in authentication generator", :green
  Rails::Generators.invoke("rails:authentication", [], behavior: behavior, destination_root: destination_root)
end

#install_confirmableObject



88
89
90
# File 'lib/generators/authentication/authentication_generator.rb', line 88

def install_confirmable
  generate_confirmable if confirmable?
end

#install_invitableObject



108
109
110
# File 'lib/generators/authentication/authentication_generator.rb', line 108

def install_invitable
  generate_invitable if invitable?
end

#install_lockableObject



104
105
106
# File 'lib/generators/authentication/authentication_generator.rb', line 104

def install_lockable
  generate_lockable if lockable?
end


112
113
114
# File 'lib/generators/authentication/authentication_generator.rb', line 112

def install_magic_link
  generate_magic_link if magic_link?
end

#install_ottObject



116
117
118
# File 'lib/generators/authentication/authentication_generator.rb', line 116

def install_ott
  generate_ott if ott?
end

#install_recoverableObject



84
85
86
# File 'lib/generators/authentication/authentication_generator.rb', line 84

def install_recoverable
  generate_recoverable if recoverable?
end

#install_registerableObject



80
81
82
# File 'lib/generators/authentication/authentication_generator.rb', line 80

def install_registerable
  generate_registerable if registerable?
end

#install_rememberableObject



92
93
94
# File 'lib/generators/authentication/authentication_generator.rb', line 92

def install_rememberable
  generate_rememberable if rememberable?
end

#install_timeoutableObject



100
101
102
# File 'lib/generators/authentication/authentication_generator.rb', line 100

def install_timeoutable
  generate_timeoutable if timeoutable?
end

#install_trackableObject



96
97
98
# File 'lib/generators/authentication/authentication_generator.rb', line 96

def install_trackable
  generate_trackable if trackable?
end

#install_validatableObject



76
77
78
# File 'lib/generators/authentication/authentication_generator.rb', line 76

def install_validatable
  generate_validatable if validatable?
end