Skip to content

Commit

Permalink
Make the AA installer interactive - Issue activeadmin#2513
Browse files Browse the repository at this point in the history
 - The installer asks the user for input in wheter or not he wants Devise and the name of the user class;
 - Creates an option for no interactivity, which is used in our tests
 - Updates the documentation
  • Loading branch information
LeGorge committed Mar 20, 2021
1 parent 5128d4b commit a8f5815
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 15 deletions.
22 changes: 15 additions & 7 deletions docs/0-installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,32 @@ that can be injected into your existing Ruby on Rails application.

## Setting up Active Admin

After installing the gem, you need to run the generator. Here are your options:
After installing the gem, you need to run the generator. It will check with you
things like whether you want to use Devise or not and the name of the User class.

* If you don't want to use Devise, run it with `--skip-users`:
```sh
rails g active_admin:install
```

If you don't want this interaction, run it with `--no_interaction` and we will use
default values and create an `AdminUser` class to use with Devise

```sh
rails g active_admin:install --skip-users
rails g active_admin:install --no_interaction
```

* If you want to use an existing user class, provide it as an argument:
Here are your other options and they all skip their respective interactive questions:

* If you don't want to use Devise, run it with `--skip-users`:

```sh
rails g active_admin:install User
rails g active_admin:install --skip-users
```

* Otherwise, with no arguments we will create an `AdminUser` class to use with Devise:
* If you want to use an existing user class, provide it as an argument:

```sh
rails g active_admin:install
rails g active_admin:install User
```

The generator adds these core files, among others:
Expand Down
33 changes: 26 additions & 7 deletions lib/generators/active_admin/install/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,34 @@ class InstallGenerator < ActiveRecord::Generators::Base
desc "Installs Active Admin and generates the necessary migrations"
argument :name, type: :string, default: "AdminUser"

hook_for :users, default: "devise", desc: "Admin user generator to run. Skip with --skip-users"
class_option "skip-users", type: :boolean, default: false, desc: "No authentication"
class_option :no_interaction, type: :boolean, default: false, desc: "No User Interaction"
class_option :skip_comments, type: :boolean, default: false, desc: "Skip installation of comments"
class_option :use_webpacker, type: :boolean, default: false, desc: "Use Webpacker assets instead of Sprockets"

source_root File.expand_path("templates", __dir__)

def interactive_setup
@use_authentication_method = true
return if options[:no_interaction]

unless options["skip-users"]
if yes?("Do you want authentication with Devise? [y for yes]")
@name = ask("What would you like the user model to be called? [Press Enter for default: AdminUser]")
else
@use_authentication_method = false
end
end
end

def setup_devise
if @use_authentication_method
generate("active_admin:devise", @name) unless options["skip-users"]
end
end

def copy_initializer
@underscored_user_name = name.underscore.gsub("/", "_")
@use_authentication_method = options[:users].present?
@underscored_user_name = @name.underscore.gsub("/", "_")
@skip_comments = options[:skip_comments]
@use_webpacker = options[:use_webpacker]
template "active_admin.rb.erb", "config/initializers/active_admin.rb"
Expand All @@ -23,14 +42,14 @@ def copy_initializer
def setup_directory
empty_directory "app/admin"
template "dashboard.rb", "app/admin/dashboard.rb"
if options[:users].present?
@user_class = name
template "admin_users.rb.erb", "app/admin/#{name.underscore.pluralize}.rb"
if @use_authentication_method
@user_class = @name
template "admin_users.rb.erb", "app/admin/#{@name.underscore.pluralize}.rb"
end
end

def setup_routes
if options[:users] # Ensure Active Admin routes occur after Devise routes so that Devise has higher priority
if @use_authentication_method # Ensure Active Admin routes occur after Devise routes so that Devise has higher priority
inject_into_file "config/routes.rb", "\n ActiveAdmin.routes(self)", after: /devise_for .*, ActiveAdmin::Devise\.config/
else
route "ActiveAdmin.routes(self)"
Expand Down
2 changes: 1 addition & 1 deletion spec/support/rails_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
end

# Setup Active Admin
generate "active_admin:install#{" --use-webpacker" if webpacker_app}"
generate "active_admin:install --no_interaction#{" --use-webpacker" if webpacker_app}"

# Force strong parameters to raise exceptions
inject_into_file "config/application.rb", after: "class Application < Rails::Application" do
Expand Down

0 comments on commit a8f5815

Please sign in to comment.