class DeviseToken::InstallGenerator
Private Class Methods
next_migration_number(path)
click to toggle source
# File lib/generators/devise_token/install_generator.rb, line 103 def self.next_migration_number(path) Time.now.utc.strftime("%Y%m%d%H%M%S") end
Public Instance Methods
add_route_mount()
click to toggle source
# File lib/generators/devise_token/install_generator.rb, line 69 def add_route_mount f = "config/routes.rb" str = "devise_token_for '#{user_class}', at: '#{mount_path}'" if File.exist?(File.join(destination_root, f)) line = parse_file_for_line(f, "devise_token_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 \"devise_token_for '#{user_class}', at: '#{mount_path}'\" to your routes file.") end end
copy_migrations()
click to toggle source
# File lib/generators/devise_token/install_generator.rb, line 14 def copy_migrations if self.class.migration_exists?("db/migrate", "devise_token_create_#{ user_class.underscore }") say_status("skipped", "Migration 'devise_token_create_#{ user_class.underscore }' already exists") else migration_template( "devise_token_create_users.rb.erb", "db/migrate/devise_token_create_#{ user_class.pluralize.underscore }.rb" ) end end
create_initializer_file()
click to toggle source
# File lib/generators/devise_token/install_generator.rb, line 10 def create_initializer_file copy_file("devise_token.rb", "config/initializers/devise_token.rb") end
create_user_model()
click to toggle source
# File lib/generators/devise_token/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 DeviseToken::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 DeviseToken::Concerns::User RUBY end end end end
include_controller_concerns()
click to toggle source
# File lib/generators/devise_token/install_generator.rb, line 46 def include_controller_concerns fname = "app/controllers/application_controller.rb" line = "include DeviseToken::Concerns::AuthenticateToken" 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 DeviseToken::Concerns::AuthenticateToken RUBY end else inject_into_file fname, after: "class ApplicationController < ActionController::Base\n" do <<-'RUBY' include DeviseToken::Concerns::AuthenticateToken RUBY end end else say_status("skipped", "app/controllers/application_controller.rb not found. Add 'include DeviseToken::Concerns::AuthenticateToken' to any controllers that require authentication.") end end
Private Instance Methods
database_name()
click to toggle source
# File lib/generators/devise_token/install_generator.rb, line 152 def database_name ActiveRecord::Base.connection.class.name end
database_version()
click to toggle source
# File lib/generators/devise_token/install_generator.rb, line 156 def database_version ActiveRecord::Base.connection.select_value('SELECT VERSION()') end
insert_after_line(filename, line, str)
click to toggle source
# File lib/generators/devise_token/install_generator.rb, line 107 def insert_after_line(filename, line, str) gsub_file filename, /(#{Regexp.escape(line)})/mi do |match| "#{match}\n #{str}" end end
is_rails_api?()
click to toggle source
# File lib/generators/devise_token/install_generator.rb, line 126 def is_rails_api? fname = "app/controllers/application_controller.rb" line = "class ApplicationController < ActionController::API" parse_file_for_line(fname, line) end
json_supported_database?()
click to toggle source
# File lib/generators/devise_token/install_generator.rb, line 132 def json_supported_database? (postgres? && postgres_correct_version?) || (mysql? && mysql_correct_version?) end
mysql?()
click to toggle source
# File lib/generators/devise_token/install_generator.rb, line 144 def mysql? database_name == 'ActiveRecord::ConnectionAdapters::MysqlAdapter' end
mysql_correct_version?()
click to toggle source
# File lib/generators/devise_token/install_generator.rb, line 148 def mysql_correct_version? database_version > '5.7.7' end
parse_file_for_line(filename, str)
click to toggle source
# File lib/generators/devise_token/install_generator.rb, line 113 def parse_file_for_line(filename, str) match = false File.open(File.join(destination_root, filename)) do |f| f.each_line do |line| if line =~ /(#{Regexp.escape(str)})/mi match = line end end end match end
postgres?()
click to toggle source
# File lib/generators/devise_token/install_generator.rb, line 136 def postgres? database_name == 'ActiveRecord::ConnectionAdapters::PostgreSQLAdapter' end
postgres_correct_version?()
click to toggle source
# File lib/generators/devise_token/install_generator.rb, line 140 def postgres_correct_version? database_version > '9.3' end