Class: ApiKeys::Generators::InstallGenerator
- Inherits:
-
Rails::Generators::Base
- Object
- Rails::Generators::Base
- ApiKeys::Generators::InstallGenerator
- Includes:
- ActiveRecord::Generators::Migration
- Defined in:
- lib/generators/api_keys/install_generator.rb
Overview
Rails generator for installing the ApiKeys gem. Creates the necessary migration and initializer file.
Class Method Summary collapse
-
.next_migration_number(dirname) ⇒ Object
Implement the required interface for Rails::Generators::Migration.
Instance Method Summary collapse
-
#create_initializer ⇒ Object
Creates the initializer file using the template.
-
#create_migration_file ⇒ Object
Creates the migration file using the template.
-
#display_post_install_message ⇒ Object
Displays helpful information to the user after installation.
Class Method Details
.next_migration_number(dirname) ⇒ Object
Implement the required interface for Rails::Generators::Migration. Borrowed from ActiveRecord::Generators::Base github.com/rails/rails/blob/main/activerecord/lib/rails/generators/active_record/base.rb#L31
18 19 20 21 |
# File 'lib/generators/api_keys/install_generator.rb', line 18 def self.next_migration_number(dirname) next_migration_number = current_migration_number(dirname) + 1 ActiveRecord::Migration.next_migration_number(next_migration_number) end |
Instance Method Details
#create_initializer ⇒ Object
Creates the initializer file using the template.
30 31 32 |
# File 'lib/generators/api_keys/install_generator.rb', line 30 def create_initializer template "initializer.rb", "config/initializers/api_keys.rb" end |
#create_migration_file ⇒ Object
Creates the migration file using the template.
24 25 26 27 |
# File 'lib/generators/api_keys/install_generator.rb', line 24 def create_migration_file migration_template "create_api_keys_table.rb.erb", File.join(db_migrate_path, "create_api_keys_table.rb") end |
#display_post_install_message ⇒ Object
Displays helpful information to the user after installation.
35 36 37 38 39 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/api_keys/install_generator.rb', line 35 def say "\nš api_keys gem successfully installed!", :green say "\nNext steps:" say " 1. Run `rails db:migrate` to create the `api_keys` table." say " ā¢ļø Run migrations before starting your application!", :yellow say "\n 2. Add `has_api_keys` to any models that need to have API keys, with an optional block for configuration:" say " # Example for app/models/user.rb" say " class User < ApplicationRecord" say " has_api_keys do" say " # Optional settings:" say " # max_keys 10" say " end" say " # ..." say " end" say "\n 3. IMPORTANT: If API keys belong to a model other than User (e.g., Organization)," say " configure the owner context in `config/initializers/api_keys.rb`:", :yellow say " # For Organization-owned API keys:" say " config.current_owner_method = :current_organization" say " config.authenticate_owner_method = :authenticate_organization!" say "\n The dashboard requires these methods to exist in your ApplicationController" say " or wherever you mount the engine. They should:" say " - `current_owner_method`: return the logged-in owner (e.g., current_organization)" say " - `authenticate_owner_method`: ensure the owner is authenticated" say "\n 4. Mount the API keys dashboard in your `routes.rb` to provide a self-serve interface:" say " # In config/routes.rb" say " mount ApiKeys::Engine => '/settings/api-keys'" say "\n 5. In your app's API controllers, verify API keys by including `ApiKeys::Controller`:" say " # Example for app/controllers/api/base_controller.rb" say " class Api::BaseController < ActionController::API" say " include ApiKeys::Controller" say " before_action :authenticate_api_key! # Enforce authentication" say " # ..." say " end" say "\nSee the api_keys README for detailed usage and examples. ", :cyan say "Happy coding! š", :green end |