Authentication with Devise (with optional config)

Adds user authentication to your app using the Devise gem
Icons/chart bar
Used 14 times
Created by
M Marshall Scott

Usage
Basically just added the recommended steps from the devise install, and a migration.

Run this command in your Rails app directory in the terminal:

rails app:template LOCATION="https://railsbytes.com/script/VD7sRy"
Template Source

Review the code before running this template on your machine.

def do_bundle
  # Custom bundle command ensures dependencies are correctly installed
  Bundler.with_unbundled_env { run "bundle install" }
end

run "bundle add devise"
do_bundle

rails_command "generate devise:install"

# Add suggested mailer default url options to development environment
inject_into_file "config/environments/development.rb", :before => "end\n" do
  "  config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }\n"
end

# Optionally add a home controller with index page
if yes?("Would you like to add an index page? [y/n]")
  controller = ask("What would you like to call the controller? [home] ")
  controller = "home" if controller.empty?
  run "rails generate controller #{controller} index"
end

# Display flash messages with the application layout
inject_into_file "app/views/layouts/application.html.erb", :after => "<body>\n" do
  "    <p class='notice'><%= notice %></p>\n    <p class='alert'><%= alert %></p>\n"
end

model_name = ask("What do you want to call your Devise model?")
attributes = ""
if yes?("Do you want to any extra attributes to #{model_name}? [y/n]")
  attributes = ask("What attributes?")
end

# We don't use rails_command here to avoid accidentally having RAILS_ENV=development as an attribute
run "rails generate devise #{model_name} #{attributes}"

# Run db:migrate
run "rails db:migrate"

# Install Devise views
run "rails generate devise:views"
Comments

Sign up or Login to leave a comment.