Class: Nor::BackendGenerator
- Inherits:
-
Rails::Generators::Base
- Object
- Rails::Generators::Base
- Nor::BackendGenerator
- Includes:
- Rails::Generators::Migration
- Defined in:
- lib/generators/nor/backend/backend_generator.rb
Instance Attribute Summary collapse
-
#mount_path ⇒ Object
readonly
Returns the value of attribute mount_path.
-
#user_class ⇒ Object
readonly
Returns the value of attribute user_class.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(args, *options) ⇒ BackendGenerator
constructor
A new instance of BackendGenerator.
- #install_application_controller ⇒ Object
- #install_cors_config ⇒ Object
- #install_devise_config ⇒ Object
- #install_devise_token_auth ⇒ Object
- #install_routes ⇒ Object
- #install_user ⇒ Object
- #install_user_serializer ⇒ Object
- #remove_wrap_parameters ⇒ Object
- #setup_gems ⇒ Object
Constructor Details
#initialize(args, *options) ⇒ BackendGenerator
Returns a new instance of BackendGenerator.
9 10 11 12 13 14 15 |
# File 'lib/generators/nor/backend/backend_generator.rb', line 9 def initialize(args, *) raise 'Next On Rails can be installed only on a Rails API app! Please create a new app with `rails new APPNAME --api`' unless rails_api? super @user_class = args[0] || 'User' @mount_path = args[1] || 'auth' end |
Instance Attribute Details
#mount_path ⇒ Object (readonly)
Returns the value of attribute mount_path.
5 6 7 |
# File 'lib/generators/nor/backend/backend_generator.rb', line 5 def mount_path @mount_path end |
#user_class ⇒ Object (readonly)
Returns the value of attribute user_class.
5 6 7 |
# File 'lib/generators/nor/backend/backend_generator.rb', line 5 def user_class @user_class end |
Class Method Details
.next_migration_number(_path) ⇒ Object
117 118 119 |
# File 'lib/generators/nor/backend/backend_generator.rb', line 117 def self.next_migration_number(_path) Time.zone.now.utc.strftime('%Y%m%d%H%M%S') end |
Instance Method Details
#install_application_controller ⇒ Object
59 60 61 62 |
# File 'lib/generators/nor/backend/backend_generator.rb', line 59 def install_application_controller remove_file 'app/controllers/application_controller.rb' template 'application_controller.rb', 'app/controllers/application_controller.rb' end |
#install_cors_config ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/generators/nor/backend/backend_generator.rb', line 69 def install_cors_config config = <<~CONFIG Rails.application.config.middleware.insert_before 0, Rack::Cors do allow do origins 'localhost:3001' resource '*', headers: :any, methods: [:get, :post, :put, :patch, :delete, :options, :head], expose: [ DeviseTokenAuth.headers_names[:'access-token'], DeviseTokenAuth.headers_names[:client], DeviseTokenAuth.headers_names[:expiry], DeviseTokenAuth.headers_names[:uid], DeviseTokenAuth.headers_names[:'token-type'] ] end end CONFIG append_to_file 'config/initializers/cors.rb', config end |
#install_devise_config ⇒ Object
64 65 66 67 |
# File 'lib/generators/nor/backend/backend_generator.rb', line 64 def install_devise_config template File.join(Gem.loaded_specs['devise'].full_gem_path, 'lib/generators/templates/devise.rb'), 'config/initializers/devise.rb' gsub_file 'config/initializers/devise.rb', "require 'devise/orm/'", "# require 'devise/orm/'" if File.exist?('config/initializers/devise.rb') end |
#install_devise_token_auth ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/generators/nor/backend/backend_generator.rb', line 25 def install_devise_token_auth generate 'devise_token_auth:install', @user_class, @mount_path config = <<~CONFIG \s\s# Configure default URLs and redirects \s\sconfig.default_confirm_success_url = 'http://localhost:3001/' \s\sconfig.default_password_reset_url = 'http://localhost:3001/change-password' \s\sconfig.redirect_whitelist = ['http://localhost:3001/*'] \s\s# If config.check_current_password_before_update is set to :attributes the \s\s# current_password param is checked before any update, if it is set to \s\s# :password the current_password param is checked only if the request \s\s# updates user password. Default current_password is never checked. \s\sconfig.check_current_password_before_update = :password CONFIG insert_into_file 'config/initializers/devise_token_auth.rb', config, before: /end$/ end |
#install_routes ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/generators/nor/backend/backend_generator.rb', line 43 def install_routes custom_routes = <<~ROUTES , controllers: { confirmations: 'next_on_rails/devise_token_auth_override/confirmations', omniauth_callbacks: 'next_on_rails/devise_token_auth_override/omniauth_callbacks', passwords: 'next_on_rails/devise_token_auth_override/passwords', registrations: 'next_on_rails/devise_token_auth_override/registrations', sessions: 'next_on_rails/devise_token_auth_override/sessions', token_validations: 'next_on_rails/devise_token_auth_override/token_validations', unlocks: 'next_on_rails/devise_token_auth_override/unlocks' } mount NextOnRails::Engine => '/' ROUTES insert_into_file 'config/routes.rb', custom_routes, after: "mount_devise_token_auth_for '#{@user_class}', at: '#{@mount_path}'" end |
#install_user ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/generators/nor/backend/backend_generator.rb', line 17 def install_user model_file = "app/models/#{user_class.singularize.gsub('::', '').underscore}.rb" template 'user.rb', model_file unless File.exist?(model_file) migration_file = "devise_token_auth_create_#{user_class.pluralize.gsub('::', '').underscore}" migration_template 'user_migration.rb', File.join('db', 'migrate', "#{migration_file}.rb") unless self.class.migration_exists?('db/migrate', migration_file) end |
#install_user_serializer ⇒ Object
91 92 93 94 |
# File 'lib/generators/nor/backend/backend_generator.rb', line 91 def install_user_serializer template 'user_serializer.rb', "app/serializers/#{@user_class.underscore}_serializer.rb" template 'current_user_serializer.rb', "app/serializers/current_#{@user_class.underscore}_serializer.rb" end |
#remove_wrap_parameters ⇒ Object
96 97 98 |
# File 'lib/generators/nor/backend/backend_generator.rb', line 96 def remove_wrap_parameters gsub_file 'config/initializers/wrap_parameters.rb', 'wrap_parameters format: [:json]', 'wrap_parameters format: []' end |
#setup_gems ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/generators/nor/backend/backend_generator.rb', line 100 def setup_gems gem_group :development do gem 'foreman' gem 'letter_opener' end template 'Procfile' config = <<~CONFIG \s\s# Mailer setup \s\sconfig.action_mailer.preview_path = Rails.root.join('test', 'mailers', 'previews') \s\sconfig.action_mailer.delivery_method = :letter_opener \s\sconfig.action_mailer.raise_delivery_errors = true \s\sconfig.action_mailer.default_url_options = { host: 'http://localhost:3000' } CONFIG insert_into_file 'config/environments/development.rb', config, before: /end$/ end |