Class: StandardId::Provider::Generators::InstallGenerator

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

Overview

Installs StandardId::Provider in a host Rails application.

Three steps, each independently skippable:

* copies the engine's migrations into db/migrate/
* writes config/initializers/standard_id_provider.rb
* mounts the engine in config/routes.rb

Idempotent: re-running skips pieces that are already installed and says so. Pass --skip-* to opt out of individual steps and --force to overwrite an existing initializer.

Instance Method Summary collapse

Instance Method Details

#copy_initializerObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/generators/standard_id/provider/install/install_generator.rb', line 71

def copy_initializer
  path = "config/initializers/standard_id_provider.rb"

  if options[:skip_initializer]
    say_status("skip", "#{path} (--skip-initializer)", :yellow)
    return
  end

  if File.exist?(File.join(destination_root, path)) && !options[:force]
    say_status("identical", "#{path} (already exists; pass --force to overwrite)", :blue)
    return
  end

  template "initializer.rb.erb", path, force: options[:force]
end

#copy_migrationsObject

Copies the engine's migrations with the same .standard_id_provider suffix rails standard_id_provider:install:migrations uses, so the two are interchangeable and neither double-installs the other's work. Done in-process rather than by shelling out to that rake task so the generator stays testable and does not need a booted host app.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/generators/standard_id/provider/install/install_generator.rb', line 50

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

  if existing_migrations.any?
    say_status(
      "identical",
      "StandardId::Provider migrations already present (#{existing_migrations.size}), skipping",
      :blue
    )
    return
  end

  engine_migrations.each_with_index do |source, index|
    name = File.basename(source, ".rb").sub(/\A\d+_/, "")
    copy_file source, "db/migrate/#{migration_number(index)}_#{name}.standard_id_provider.rb"
  end
end

#mount_engineObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/generators/standard_id/provider/install/install_generator.rb', line 87

def mount_engine
  routes_path = "config/routes.rb"

  if options[:skip_routes]
    say_status("skip", "#{routes_path} (--skip-routes)", :yellow)
    return
  end

  unless File.exist?(File.join(destination_root, routes_path))
    say_status("warn", "#{routes_path} not found; mount the engine yourself", :red)
    return
  end

  if File.read(File.join(destination_root, routes_path)).include?("StandardId::Provider::Engine")
    say_status("identical", "#{routes_path} (engine already mounted)", :blue)
    return
  end

  route(%(mount StandardId::Provider::Engine => "#{options[:mount_path]}"))
end


108
109
110
111
112
113
114
115
116
117
118
# File 'lib/generators/standard_id/provider/install/install_generator.rb', line 108

def print_next_steps
  say ""
  say "StandardId::Provider installed. Next:", :green
  say "  1. rails db:migrate"
  say "  2. Set config.oauth.discovery_endpoint_base in your StandardId"
  say "     initializer — this engine's discovery document cannot detect"
  say "     the ApiEngine mount on its own. See the README."
  say "  3. Set config.oauth.introspection_enabled = true if you want the"
  say "     RFC 7662 endpoint (it is off by default in standard_id)."
  say ""
end