Class: Adminpanel::CustomErrorsGenerator

Inherits:
ActiveRecord::Generators::Base
  • Object
show all
Defined in:
lib/generators/adminpanel/custom_errors/custom_errors_generator.rb

Instance Method Summary collapse

Instance Method Details

#copy_errors_controllerObject



9
10
11
# File 'lib/generators/adminpanel/custom_errors/custom_errors_generator.rb', line 9

def copy_errors_controller
  copy_file 'errors_controller.rb', 'app/controllers/errors_controller.rb'
end

#copy_errors_templateObject



13
14
15
# File 'lib/generators/adminpanel/custom_errors/custom_errors_generator.rb', line 13

def copy_errors_template
  copy_file 'show.html.erb', 'app/views/errors/show.html.erb'
end

#inject_error_handling_app_in_configurationObject



23
24
25
26
27
# File 'lib/generators/adminpanel/custom_errors/custom_errors_generator.rb', line 23

def inject_error_handling_app_in_configuration
  inject_into_file 'config/application.rb', after: 'class Application < Rails::Application' do
    "\n    config.exceptions_app = -> (env) { ErrorsController.action(:show).call(env) }"
  end
end

#inject_errors_into_routesObject



17
18
19
20
21
# File 'lib/generators/adminpanel/custom_errors/custom_errors_generator.rb', line 17

def inject_errors_into_routes
  inject_into_file 'config/routes.rb', before: "\nend\n" do
    "\n  get \"(errors)/:status\", to: \"errors#show\", constraints: { status: /\d{3}/ }"
  end
end


29
30
31
32
33
34
35
# File 'lib/generators/adminpanel/custom_errors/custom_errors_generator.rb', line 29

def print_messages
  puts "The generator tried it's best to insert into config/routes.rb"
  puts '  get "(errors)/:status", to: "errors#show", constraints: { status: /\d{3}/ }'
  puts 'and into config/application.rb'
  puts '  config.exceptions_app = -> (env) { ErrorsController.action(:show).call(env) }'
  puts 'Make sure those lines are actually there. You can customize your messages in app/controllers/errors_controller.rb and in app/views/errors/show.html.erb'
end