diff --git a/.rubocop.yml b/.rubocop.yml index 5b39c3c..6b2cafe 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -16,8 +16,13 @@ Metrics/LineLength: Metrics/ClassLength: Exclude: + - 'spec/support/gem_test_helpers.rb' - 'lib/cybele/generators/app_generator.rb' +Metrics/ModuleLength: + Exclude: + - 'spec/support/gem_test_helpers.rb' + Style/AccessorMethodName: Exclude: - 'lib/cybele/generators/app_generator.rb' @@ -25,6 +30,13 @@ Style/AccessorMethodName: Metrics/MethodLength: CountComments: false Max: 15 + Exclude: + - 'spec/support/gem_test_helpers.rb' + +Metrics/AbcSize: + Exclude: + - 'spec/support/gem_test_helpers.rb' + - 'spec/support/mail_test_helpers.rb' Metrics/BlockLength: CountComments: false @@ -35,6 +47,7 @@ Metrics/BlockLength: - 'spec/**/*.rb' - 'lib/cybele/app_builder.rb' - 'lib/cybele/generators/app_generator.rb' + - 'spec/support/cybele_test_helpers.rb' Style/FrozenStringLiteralComment: EnforcedStyle: when_needed diff --git a/lib/cybele.rb b/lib/cybele.rb index 08f5805..e25b2d5 100644 --- a/lib/cybele.rb +++ b/lib/cybele.rb @@ -15,4 +15,5 @@ require 'cybele/helpers/mailer' require 'cybele/helpers/paperclip' require 'cybele/helpers/devise' +require 'cybele/helpers/error_pages' require 'cybele/app_builder' diff --git a/lib/cybele/app_builder.rb b/lib/cybele/app_builder.rb index 3026c1b..1ba402f 100644 --- a/lib/cybele/app_builder.rb +++ b/lib/cybele/app_builder.rb @@ -15,6 +15,7 @@ class AppBuilder < Rails::AppBuilder include Cybele::Helpers::Mailer include Cybele::Helpers::Paperclip include Cybele::Helpers::Devise + include Cybele::Helpers::ErrorPages def readme template 'README.md.erb', @@ -75,7 +76,7 @@ def setup_gitignore_files end def setup_gitignore_folders - %w( + %w[ app/assets/images db/migrate spec/support @@ -84,7 +85,7 @@ def setup_gitignore_folders spec/views spec/controllers spec/helpers - ).each do |dir| + ].each do |dir| empty_directory_with_keep_file dir end end diff --git a/lib/cybele/generators/app_generator.rb b/lib/cybele/generators/app_generator.rb index 51a9b5a..9d2ff6c 100644 --- a/lib/cybele/generators/app_generator.rb +++ b/lib/cybele/generators/app_generator.rb @@ -213,6 +213,11 @@ def gitignore_files_and_folders build :setup_gitignore_folders end + def configure_error_pages + say 'Setup custom exception pages and 404 page' + build :configure_error_pages + end + def goodbye say 'Congratulations! That\'s all...', :green end diff --git a/lib/cybele/helpers/error_pages.rb b/lib/cybele/helpers/error_pages.rb new file mode 100644 index 0000000..9baeb83 --- /dev/null +++ b/lib/cybele/helpers/error_pages.rb @@ -0,0 +1,37 @@ +# frozen_string_literal: true + +module Cybele + module Helpers + module ErrorPages + def configure_error_pages + inject_into_file 'app/controllers/application_controller.rb', + template_content('error_pages/error_control.erb'), + before: 'self.responder' + + inject_into_file 'app/controllers/application_controller.rb', + template_content('error_pages/error_method.erb'), + after: 'protect_from_forgery with: :exception' + + inject_into_file 'config/routes.rb', + template_content('error_pages/error_route.erb'), + before: /^end/ + + create_error_pages_files + end + + private + + def create_error_pages_files + # Server Error + template 'error_pages/internal_server_error.html.haml', + 'app/views/errors/internal_server_error.html.haml', + force: true + + # Not Found Error + template 'error_pages/not_found.html.haml', + 'app/views/errors/not_found.html.haml', + force: true + end + end + end +end diff --git a/spec/features/new_default_project_spec.rb b/spec/features/new_default_project_spec.rb index 3c41e1b..77f54d5 100644 --- a/spec/features/new_default_project_spec.rb +++ b/spec/features/new_default_project_spec.rb @@ -31,7 +31,7 @@ expect(gemfile_file).to match(/^gem 'devise-async'/) sidekiq_file = content('config/sidekiq.yml') - expect(sidekiq_file).to match(/^:concurrency: 25/) + expect(sidekiq_file).to match('[high_priority, 2]') sidekiq_schedule_file = content('config/sidekiq_schedule.yml') expect(sidekiq_schedule_file).to match(/-> Daily at midnight/) @@ -207,4 +207,12 @@ it 'uses devise' do devise_test_helper end + + it 'uses error_pages' do + error_pages_helper + end + + it 'uses gitignore' do + gitignore_helper + end end diff --git a/spec/features/new_not_default_project_spec.rb b/spec/features/new_not_default_project_spec.rb index 8bb6500..1ce0eb7 100644 --- a/spec/features/new_not_default_project_spec.rb +++ b/spec/features/new_not_default_project_spec.rb @@ -187,4 +187,12 @@ it 'uses devise' do devise_test_helper end + + it 'uses error_pages' do + error_pages_helper + end + + it 'uses gitignore' do + gitignore_helper + end end diff --git a/spec/support/gem_test_helpers.rb b/spec/support/gem_test_helpers.rb index 71a60b2..049c582 100644 --- a/spec/support/gem_test_helpers.rb +++ b/spec/support/gem_test_helpers.rb @@ -145,4 +145,23 @@ def config_test_helper config_test_file = content('config/environments/test.rb') expect(config_test_file).to match(/^Rails.application.configure/) end + + def error_pages_helper + application_controller_file = content('app/controllers/application_controller.rb') + expect(application_controller_file).to match('rescue_from Exception') + expect(application_controller_file).to match('rescue_from ActiveRecord::RecordNotFound') + expect(application_controller_file).to match('rescue_from ActionController::RoutingError') + expect(application_controller_file).to match('server_error') + expect(application_controller_file).to match('page_not_found') + + route_file = content('config/routes.rb') + expect(route_file).to match('unmatched_route') + end + + def gitignore_helper + application_controller_file = content('.gitignore') + expect(application_controller_file).to match('.DS_Store') + expect(application_controller_file).to match('.secret') + expect(application_controller_file).to match('.env') + end end diff --git a/spec/support/mail_test_helpers.rb b/spec/support/mail_test_helpers.rb index 80d4b0a..031dbe2 100644 --- a/spec/support/mail_test_helpers.rb +++ b/spec/support/mail_test_helpers.rb @@ -10,8 +10,6 @@ def mail_test_helper(path) expect(file).to match('user_name:') expect(file).to match('password:') expect(file).to match('authentication:') - unless content('config/settings.yml').present? - expect(file).to match('host:') - end + expect(file).to match('host:') unless content('config/settings.yml').present? end end diff --git a/templates/config/locales/view.en.yml b/templates/config/locales/view.en.yml index 5029022..3373fc5 100644 --- a/templates/config/locales/view.en.yml +++ b/templates/config/locales/view.en.yml @@ -1,2 +1,5 @@ en: - view: \ No newline at end of file + view: + error: + internal_server_error: "We are sorry. An error occured. Our engineers are working on it. We will solve it as soon as possible." + not_found: "We are sorry. Page was not found. It may have been moved." \ No newline at end of file diff --git a/templates/config/locales/view.tr.yml b/templates/config/locales/view.tr.yml index 7df91e5..ce108f7 100644 --- a/templates/config/locales/view.tr.yml +++ b/templates/config/locales/view.tr.yml @@ -1,2 +1,5 @@ tr: - view: \ No newline at end of file + view: + error: + internal_server_error: "Üzgünüz. Şu anda bir hata var. Mühendislerimiz bu hata üzerinde çalışma yapıyorlar. En kısa sürede çözeceğiz." + not_found: "Üzgünüz. Bu sayfa bulunamadı. Taşınmış olabilir. Buraya gitmek istediğinizden emin olun." \ No newline at end of file diff --git a/templates/error_pages/error_control.erb b/templates/error_pages/error_control.erb new file mode 100644 index 0000000..a5dff31 --- /dev/null +++ b/templates/error_pages/error_control.erb @@ -0,0 +1,5 @@ + + rescue_from Exception, with: :server_error if Rails.env.production? or Rails.env.staging? + rescue_from ActiveRecord::RecordNotFound, with: :page_not_found if Rails.env.production? or Rails.env.staging? + rescue_from ActionController::RoutingError, with: :page_not_found if Rails.env.production? or Rails.env.staging? + diff --git a/templates/error_pages/error_method.erb b/templates/error_pages/error_method.erb new file mode 100644 index 0000000..6f3d741 --- /dev/null +++ b/templates/error_pages/error_method.erb @@ -0,0 +1,10 @@ + + + def server_error(exception) + Rollbar.error "ApplicationController#server_error --exception: #{exception}" + render template: 'errors/internal_server_error', status: 500 + end + + def page_not_found + render template: 'errors/not_found', status: 404 + end diff --git a/templates/error_pages/error_route.erb b/templates/error_pages/error_route.erb new file mode 100644 index 0000000..4732d9c --- /dev/null +++ b/templates/error_pages/error_route.erb @@ -0,0 +1,4 @@ + + if Rails.env.production? or Rails.env.staging? + match '*unmatched_route', to: 'application#page_not_found', via: :all + end diff --git a/templates/error_pages/internal_server_error.html.haml b/templates/error_pages/internal_server_error.html.haml new file mode 100644 index 0000000..54c4307 --- /dev/null +++ b/templates/error_pages/internal_server_error.html.haml @@ -0,0 +1,2 @@ +.well + %p= t('view.internal_server_error') \ No newline at end of file diff --git a/templates/error_pages/not_found.html.haml b/templates/error_pages/not_found.html.haml new file mode 100644 index 0000000..cc9239c --- /dev/null +++ b/templates/error_pages/not_found.html.haml @@ -0,0 +1,2 @@ +.well + %p= t('view.not_found') \ No newline at end of file