Module: DeviseJwtAuth::InstallGeneratorHelpers
- Included in:
- InstallGenerator, InstallMongoidGenerator
- Defined in:
- lib/generators/devise_jwt_auth/install_generator_helpers.rb
Overview
Helper methods for installation generators.
Class Method Summary collapse
Class Method Details
.included(mod) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/generators/devise_jwt_auth/install_generator_helpers.rb', line 7 def included(mod) mod.class_eval do source_root File.('templates', __dir__) argument :user_class, type: :string, default: 'User' argument :mount_path, type: :string, default: 'auth' def create_initializer_file copy_file('devise_jwt_auth.rb', 'config/initializers/devise_jwt_auth.rb') end def include_controller_concerns fname = 'app/controllers/application_controller.rb' line = 'include DeviseJwtAuth::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 rails_api? inject_into_file fname, after: "class ApplicationController < ActionController::API\n" do " include DeviseJwtAuth::Concerns::SetUserByToken\n RUBY\n end\n else\n inject_into_file fname,\n after: \"class ApplicationController < ActionController::Base\\n\" do\n <<-'RUBY'\n include DeviseJwtAuth::Concerns::SetUserByToken\n RUBY\n end\n end\n else\n say_status('skipped', \"app/controllers/application_controller.rb not found. Add 'include DeviseJwtAuth::Concerns::SetUserByToken' to any controllers that require authentication.\")\n end\n end\n\n def add_route_mount\n f = 'config/routes.rb'\n str = \"mount_devise_jwt_auth_for '\#{user_class}', at: '\#{mount_path}'\"\n\n if File.exist?(File.join(destination_root, f))\n line = parse_file_for_line(f, 'mount_devise_jwt_auth_for')\n\n if line\n existing_user_class = true\n else\n line = 'Rails.application.routes.draw do'\n existing_user_class = false\n end\n\n if parse_file_for_line(f, str)\n say_status('skipped', \"Routes already exist for \#{user_class} at \#{mount_path}\")\n else\n insert_after_line(f, line, str)\n\n if existing_user_class\n scoped_routes = ''\\\n \"as :\#{user_class.underscore} do\\n\"\\\n \" # Define routes for \#{user_class} within this block.\\n\"\\\n \" end\\n\"\n insert_after_line(f, str, scoped_routes)\n end\n end\n else\n say_status('skipped', \"config/routes.rb not found. Add \\\"mount_devise_jwt_auth_for '\#{user_class}', at: '\#{mount_path}'\\\" to your routes file.\")\n end\n end\n\n def ip_column\n # Padded with spaces so it aligns nicely with the rest of the columns.\n format('%-8s', (inet? ? 'inet' : 'string'))\n end\n\n def inet?\n postgresql?\n end\n\n def postgresql?\n config = ActiveRecord::Base.connection_db_config.configuration_hash\n config && config['adapter'] == 'postgresql'\n end\n\n private\n\n def insert_after_line(filename, line, str)\n gsub_file filename, /(\#{Regexp.escape(line)})/mi do |match|\n \"\#{match}\\n \#{str}\"\n end\n end\n\n def parse_file_for_line(filename, str)\n match = false\n\n File.open(File.join(destination_root, filename)) do |f|\n f.each_line do |line|\n match = line if line =~ /(\#{Regexp.escape(str)})/mi\n end\n end\n match\n end\n\n def rails_api?\n fname = 'app/controllers/application_controller.rb'\n line = 'class ApplicationController < ActionController::API'\n parse_file_for_line(fname, line)\n end\n end\nend\n" |