Class: StandardId::Generators::InstallGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/standard_id/install/install_generator.rb

Overview

Installs StandardId in a host Rails application.

Creates the initializer, mounts the Web/Api engines in the host’s config/routes.rb, copies the engine’s database migrations into the host, and prints a post-install checklist with the remaining manual steps.

Idempotent: re-running the generator will skip files/routes it has already installed.

Class Attribute Summary collapse

Instance Method Summary collapse

Class Attribute Details

.migration_task_runnerObject

Returns the value of attribute migration_task_runner.



141
142
143
# File 'lib/generators/standard_id/install/install_generator.rb', line 141

def migration_task_runner
  @migration_task_runner
end

Instance Method Details

#copy_migrationsObject



73
74
75
76
77
78
79
80
# File 'lib/generators/standard_id/install/install_generator.rb', line 73

def copy_migrations
  return say_status("skip", "db/migrate (--skip-migrations)", :yellow) if options[:skip_migrations]

  # The engine's namespace is `standard_id`, so Rails auto-registers
  # `standard_id:install:migrations`. We run it via a Rails command so
  # the host app's environment (and existing migrations) is honoured.
  run_migration_copy_task
end

#create_initializer_fileObject



34
35
36
37
38
# File 'lib/generators/standard_id/install/install_generator.rb', line 34

def create_initializer_file
  return say_status("skip", "config/initializers/standard_id.rb (--skip-initializer)", :yellow) if options[:skip_initializer]

  template "standard_id.rb", "config/initializers/standard_id.rb"
end

#mount_enginesObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/generators/standard_id/install/install_generator.rb', line 40

def mount_engines
  return say_status("skip", "config/routes.rb (--skip-routes)", :yellow) if options[:skip_routes]

  routes_path = "config/routes.rb"
  unless File.exist?(File.join(destination_root, routes_path))
    say_status("warn", "#{routes_path} not found — add engine mounts manually", :red)
    return
  end

  if engines_already_mounted?(routes_path)
    say_status("identical", "#{routes_path} (StandardId engines already mounted)", :blue)
    return
  end

  inject_into_file routes_path, indent(engine_mount_snippet, 2), after: "Rails.application.routes.draw do\n"

  # inject_into_file silently no-ops when the `after:` string doesn't
  # match exactly (e.g. the host has a trailing comment or a rubocop
  # directive on the `draw do` line, or the file uses CRLF line
  # endings). Re-read and warn so the user isn't left with an
  # un-mounted engine.
  unless engines_already_mounted?(routes_path)
    say_status(
      "warn",
      "Could not auto-mount StandardId engines in #{routes_path}. " \
        "Add the following inside `Rails.application.routes.draw do` manually:\n" \
        "  mount StandardId::WebEngine => \"/\"\n" \
        "  mount StandardId::ApiEngine => \"/api\"",
      :red
    )
  end
end

#print_post_install_messageObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/generators/standard_id/install/install_generator.rb', line 82

def print_post_install_message
  say ""
  say "=" * 79
  say "StandardId installed."
  say ""
  say "To complete setup:"
  say ""
  say "1. Account model — add to app/models/account.rb (or your user model):"
  say ""
  say "     class Account < ApplicationRecord"
  say "       include StandardId::AccountAssociations"
  say "       include StandardId::AccountStatus"
  say "     end"
  say ""
  say "   AccountAssociations wires up :identifiers, :credentials, :sessions,"
  say "   :refresh_tokens, and :client_applications, plus nested attributes."
  say ""
  say "2. Controllers — include in relevant base controllers:"
  say ""
  say "     # app/controllers/application_controller.rb"
  say "     include StandardId::WebAuthentication"
  say ""
  say "     # app/controllers/api/base_controller.rb"
  say "     include StandardId::ApiAuthentication"
  say ""
  say "     # app/channels/application_cable/connection.rb"
  say "     include StandardId::CableAuthentication"
  say ""
  say "3. Configure — review config/initializers/standard_id.rb. Required:"
  say "     - account_class_name (defaults to \"User\" — change if your model is Account)"
  say "     - issuer             (your base URL; used as JWT \"iss\" claim)"
  say "     - oauth.allowed_audiences (array of audience names if enforcing JWT aud)"
  say ""
  say "4. Migrations — run:"
  say ""
  say "     bin/rails db:migrate"
  say ""
  say "5. Scheduled maintenance — schedule the cleanup jobs (e.g. daily):"
  say ""
  say "     StandardId::CleanupExpiredSessionsJob"
  say "     StandardId::CleanupExpiredRefreshTokensJob"
  say ""
  say "6. Social providers — install provider plugins and register them:"
  say ""
  say "     gem \"standard_id-google\""
  say "     gem \"standard_id-apple\""
  say ""
  say "   See the README section on providers for credential configuration."
  say ""
  say "Run `rails g standard_id:install --help` for options."
  say "=" * 79
  say ""
end