Sha256: bc72d6ddfb8c42548f15058a2d3024b922b03ee296bc5feeb9ff76e8b4a878a7

Contents?: true

Size: 1.8 KB

Versions: 20

Compression:

Stored size: 1.8 KB

Contents

# frozen_string_literal: true

module RailsAppGenerator
  # Custom add-ons for RailsAppGenerator
  module AddOns
    # Add Devise authentication to rails application
    class Devise < RailsAppGenerator::Addon
      depends_on :active_record

      required_gem gem.version('devise', '4.8.1', 'Flexible authentication solution for Rails with Warden')

      # Requires Testing
      def apply
        generate('devise:install', capture: true)
        generate(:devise, 'User', 'name', 'admin:boolean', capture: true)

        directory('app/views/devise', 'app/views/devise') if uses?(:views)

        generate('devise:controllers', 'users', capture: true)

        update_development_rb
        gsub_file 'config/initializers/devise.rb', /# config.pepper = .+/, "  # config.pepper = 'pepper'"
        gsub_file 'config/initializers/devise.rb', /# config.secret_key = .+/, "  # config.secret_key = 'secret_key'"
        db_changes
        create_seed
      end

      private

      def update_development_rb
        inject_into_file 'config/environments/development.rb', before: /^end/ do
          <<-RUBY

  # Enable devise mailer
  config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
          RUBY
        end
      end

      def db_changes
        in_root do
          migration = Dir.glob('db/migrate/*').max_by { |f| File.mtime(f) }
          gsub_file migration, /:admin/, ':admin, default: false'
        end
      end

      def create_seed
        insert_into_file 'db/seeds.rb' do
          <<~RUBY
            # Create an initial admin user for development
            User.find_or_create_by(email: "admin@admin.com") do |user|
              user.name = 'Admin'
              user.password = 'password'
              user.admin = true
            end
          RUBY
        end
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
rails_app_generator-0.2.22 lib/rails_app_generator/addons/devise_old.rb
rails_app_generator-0.2.21 lib/rails_app_generator/addons/devise.rb
rails_app_generator-0.2.20 lib/rails_app_generator/addons/devise.rb
rails_app_generator-0.2.19 lib/rails_app_generator/addons/devise.rb
rails_app_generator-0.2.18 lib/rails_app_generator/addons/devise.rb
rails_app_generator-0.2.17 lib/rails_app_generator/addons/devise.rb
rails_app_generator-0.2.16 lib/rails_app_generator/addons/devise.rb
rails_app_generator-0.2.15 lib/rails_app_generator/addons/devise.rb
rails_app_generator-0.2.14 lib/rails_app_generator/addons/devise.rb
rails_app_generator-0.2.13 lib/rails_app_generator/addons/devise.rb
rails_app_generator-0.2.12 lib/rails_app_generator/addons/devise.rb
rails_app_generator-0.2.11 lib/rails_app_generator/addons/devise.rb
rails_app_generator-0.2.10 lib/rails_app_generator/addons/devise.rb
rails_app_generator-0.2.9 lib/rails_app_generator/addons/devise.rb
rails_app_generator-0.2.8 lib/rails_app_generator/addons/devise.rb
rails_app_generator-0.2.7 lib/rails_app_generator/addons/devise.rb
rails_app_generator-0.2.6 lib/rails_app_generator/addons/devise.rb
rails_app_generator-0.2.5 lib/rails_app_generator/addons/devise.rb
rails_app_generator-0.2.4 lib/rails_app_generator/addons/devise.rb
rails_app_generator-0.2.3 lib/rails_app_generator/addons/devise.rb