Devise with optional bootstrap

Set up devise and add a basic navigation partial with login/logout links when bootstrap is present
Icons/chart bar
Used 324 times
Created by
T TobiasBales

Usage

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

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

Review the code before running this template on your machine.

def file_contains?(filename, string)
  File.foreach(filename).detect { |line| line.include?(string) }
end

use_bootstrap = file_contains? 'package.json', 'bootstrap'

gem 'devise'
if use_bootstrap
	gem 'devise-bootstrapped', github: 'king601/devise-bootstrapped', branch: 'bootstrap4'
end
gem 'devise-pwned_password'
gem 'gravatar_image_tag'

Bundler.with_unbundled_env { run 'bundle install' }

run 'bin/spring stop'

run 'rails generate devise:install'
run 'rails generate devise user'
run 'rails generate devise:views:bootstrapped'
rails_command 'db:migrate'

inject_into_file 'app/models/user.rb', after: 'class User < ApplicationRecord' do <<~RB

    validates :email, presence: true
  RB
end

generate 'controller dashboard index'
route 'root to: \'dashboard#index\''
system('rm app/helpers/dashboard_helper.rb')

gsub_file 'app/models/user.rb', ':validatable', ':validatable, :pwned_password'

gsub_file 'test/fixtures/users.yml', 'one: {}' do <<~EOF
    one:
      email: [email protected]
  EOF
end

gsub_file 'test/fixtures/users.yml', 'two: {}' do <<~EOF
    two:
      email: [email protected]
  EOF
end

return unless use_bootstrap
    

create_file 'app/views/shared/_navigation.html.erb' do
  <<~ERB
    <nav class="navbar navbar-expand-md navbar-light bg-light">
      <div class="container">
        <button aria-controls="navbarsExample04" aria-expanded="false" aria-label="Toggle navigation" class="navbar-toggler" data-target="#navbarMain" data-toggle="collapse" type="button">
          <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarMain">
          <ul class="navbar-nav mr-auto mt-2 mt-md-0">
            <li class="nav-item">
              <%= link_to "Root", root_path, class: "nav-link \#{'active' if controller_name == 'dashboard'}" %>
            </li>
          </ul>
          <ul class="navbar-nav">
            <% if user_signed_in? %>
              <li class="nav-item dropdown">
                <%= link_to "#", id: "navbar-dropdown", class: "nav-link dropdown-toggle", data: { target: "nav-account-dropdown", toggle: "dropdown" }, aria: { haspopup: true, expanded: false } do %>
                  <%= image_tag gravatar_image_url(current_user.email, size: 40, secure: true), height: 20, width: 20, class: "rounded" %>
                <% end %>
                <div aria-labelledby="navbar-dropdown" class="dropdown-menu dropdown-menu-right" id="nav-account-dropdown">
                  <%= link_to "Settings", edit_user_registration_path, class: "dropdown-item" %>
                  <div class="dropdown-divider"></div>
                  <%= link_to "Logout", destroy_user_session_path, method: :delete, class: "dropdown-item" %>
                </div>
              </li>
            <% else %>
              <li class="nav-item">
                <%= link_to "Sign Up", new_user_registration_path, class: "nav-link" %>
              </li>
              <li class="nav-item">
                <%= link_to "Login", new_user_session_path, class: "nav-link" %>
              </li>
            <% end %>
          </ul>
        </div>
      </div>
    </nav>
  ERB
end

gsub_file 'app/views/layouts/application.html.erb', '<%= yield %>' do
  <<~ERB
    <%= render 'shared/navigation' %>
    <div class="container mt-3">
	  <%= yield %>
    </div>
  ERB
end
Comments
Andrea Fomera
Hey just a heads up the
king601/devise-bootstrapped
Repo is now afomera/devise-bootstrapped


Alex
"data-bs-target" => "nav-account-dropdown", "data-bs-toggle" => "dropdown"

© 2022 GoRails, LLC