Class: DeviseTokenAuth::InstallGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
Rails::Generators::Migration
Defined in:
lib/generators/devise_token_auth/install_generator.rb

Instance Method Summary collapse

Instance Method Details

#add_route_mountObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/generators/devise_token_auth/install_generator.rb', line 69

def add_route_mount
  f    = "config/routes.rb"
  str  = "mount_devise_token_auth_for '#{user_class}', at: '#{mount_path}'"

  if File.exist?(File.join(destination_root, f))
    line = parse_file_for_line(f, "mount_devise_token_auth_for")

    unless line
      line = "Rails.application.routes.draw do"
      existing_user_class = false
    else
      existing_user_class = true
    end

    if parse_file_for_line(f, str)
      say_status("skipped", "Routes already exist for #{user_class} at #{mount_path}")
    else
      insert_after_line(f, line, str)

      if existing_user_class
        scoped_routes = ""+
          "as :#{user_class.underscore} do\n"+
          "    # Define routes for #{user_class} within this block.\n"+
          "  end\n"
        insert_after_line(f, str, scoped_routes)
      end
    end
  else
    say_status("skipped", "config/routes.rb not found. Add \"mount_devise_token_auth_for '#{user_class}', at: '#{mount_path}'\" to your routes file.")
  end
end

#copy_migrationsObject



14
15
16
17
18
19
20
21
22
23
# File 'lib/generators/devise_token_auth/install_generator.rb', line 14

def copy_migrations
  if self.class.migration_exists?("db/migrate", "devise_token_auth_create_#{ user_class.underscore }")
    say_status("skipped", "Migration 'devise_token_auth_create_#{ user_class.underscore }' already exists")
  else
    migration_template(
      "devise_token_auth_create_users.rb.erb",
      "db/migrate/devise_token_auth_create_#{ user_class.pluralize.underscore }.rb"
    )
  end
end

#create_initializer_fileObject



10
11
12
# File 'lib/generators/devise_token_auth/install_generator.rb', line 10

def create_initializer_file
  copy_file("devise_token_auth.rb", "config/initializers/devise_token_auth.rb")
end

#create_user_modelObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/generators/devise_token_auth/install_generator.rb', line 25

def create_user_model
  fname = "app/models/#{ user_class.underscore }.rb"
  unless File.exist?(File.join(destination_root, fname))
    template("user.rb", fname)
  else
    inclusion = "include DeviseTokenAuth::Concerns::User"
    unless parse_file_for_line(fname, inclusion)
      
      active_record_needle = (Rails::VERSION::MAJOR == 5) ? 'ApplicationRecord' : 'ActiveRecord::Base'
      inject_into_file fname, after: "class #{user_class} < #{active_record_needle}\n" do <<-'RUBY'
  # Include default devise modules.
  devise :database_authenticatable, :registerable,
      :recoverable, :rememberable, :trackable, :validatable,
      :confirmable, :omniauthable
  include DeviseTokenAuth::Concerns::User
      RUBY
      end
    end
  end
end

#include_controller_concernsObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/generators/devise_token_auth/install_generator.rb', line 46

def include_controller_concerns
  fname = "app/controllers/application_controller.rb"
  line  = "include DeviseTokenAuth::Concerns::SetUserByToken"

  if File.exist?(File.join(destination_root, fname))
    if parse_file_for_line(fname, line)
      say_status("skipped", "Concern is already included in the application controller.")
    elsif is_rails_api?
      inject_into_file fname, after: "class ApplicationController < ActionController::API\n" do <<-'RUBY'
  include DeviseTokenAuth::Concerns::SetUserByToken
      RUBY
      end
    else
      inject_into_file fname, after: "class ApplicationController < ActionController::Base\n" do <<-'RUBY'
  include DeviseTokenAuth::Concerns::SetUserByToken
      RUBY
      end
    end
  else
    say_status("skipped", "app/controllers/application_controller.rb not found. Add 'include DeviseTokenAuth::Concerns::SetUserByToken' to any controllers that require authentication.")
  end
end