From f30c5a7a5dc04bdbd5c5539494bc7afb89bde959 Mon Sep 17 00:00:00 2001 From: hamdibayhan Date: Mon, 13 Nov 2017 14:02:16 +0300 Subject: [PATCH 1/4] KBP-140 #time 2h - Configure custom error pages --- .rubocop.yml | 13 +++++++ lib/cybele.rb | 1 + lib/cybele/app_builder.rb | 5 ++- lib/cybele/generators/app_generator.rb | 5 +++ lib/cybele/helpers/error_pages.rb | 37 +++++++++++++++++++ spec/features/new_default_project_spec.rb | 10 ++++- spec/features/new_not_default_project_spec.rb | 8 ++++ spec/support/gem_test_helpers.rb | 19 ++++++++++ spec/support/mail_test_helpers.rb | 4 +- templates/config/locales/view.en.yml | 5 ++- templates/config/locales/view.tr.yml | 5 ++- templates/error_pages/error_control.erb | 5 +++ templates/error_pages/error_method.erb | 10 +++++ templates/error_pages/error_route.erb | 4 ++ .../internal_server_error.html.haml | 2 + templates/error_pages/not_found.html.haml | 2 + 16 files changed, 127 insertions(+), 8 deletions(-) create mode 100644 lib/cybele/helpers/error_pages.rb create mode 100644 templates/error_pages/error_control.erb create mode 100644 templates/error_pages/error_method.erb create mode 100644 templates/error_pages/error_route.erb create mode 100644 templates/error_pages/internal_server_error.html.haml create mode 100644 templates/error_pages/not_found.html.haml 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 From 7b09310b817e12f158458407b08b3b09255e3c8b Mon Sep 17 00:00:00 2001 From: hamdibayhan Date: Tue, 21 Nov 2017 11:12:17 +0300 Subject: [PATCH 2/4] KBP-140 #time 45m - Code was improved --- .rubocop.yml | 13 -- spec/features/new_default_project_spec.rb | 16 +- spec/features/new_not_default_project_spec.rb | 16 +- spec/spec_helper.rb | 9 +- spec/support/config_test_helper.rb | 35 ++++ spec/support/devise_test_helper.rb | 52 ++++++ spec/support/dotenv_test_helper.rb | 35 ++++ spec/support/error_pages_test_helper.rb | 24 +++ spec/support/gem_test_helpers.rb | 167 ------------------ spec/support/git_ignore_test_helper.rb | 10 ++ spec/support/locale_language_test_helper.rb | 43 +++++ spec/support/mail_test_helpers.rb | 2 +- spec/support/paperclip_test_helper.rb | 48 +++++ spec/support/responder_test_helper.rb | 32 ++++ 14 files changed, 304 insertions(+), 198 deletions(-) create mode 100644 spec/support/config_test_helper.rb create mode 100644 spec/support/devise_test_helper.rb create mode 100644 spec/support/dotenv_test_helper.rb create mode 100644 spec/support/error_pages_test_helper.rb delete mode 100644 spec/support/gem_test_helpers.rb create mode 100644 spec/support/git_ignore_test_helper.rb create mode 100644 spec/support/locale_language_test_helper.rb create mode 100644 spec/support/paperclip_test_helper.rb create mode 100644 spec/support/responder_test_helper.rb diff --git a/.rubocop.yml b/.rubocop.yml index 6b2cafe..5b39c3c 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -16,13 +16,8 @@ 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' @@ -30,13 +25,6 @@ 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 @@ -47,7 +35,6 @@ 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/spec/features/new_default_project_spec.rb b/spec/features/new_default_project_spec.rb index 77f54d5..e4418b6 100644 --- a/spec/features/new_default_project_spec.rb +++ b/spec/features/new_default_project_spec.rb @@ -50,7 +50,7 @@ end it 'uses responders' do - responder_test_helper + responder_test end it 'uses cybele_version' do @@ -126,7 +126,7 @@ end it 'uses config and staging file' do - config_test_helper + config_test end it 'uses recipient_interceptor' do @@ -138,7 +138,7 @@ end it 'uses locale_language' do - locale_language_test_helper + locale_language_test end it 'uses simple_form' do @@ -164,11 +164,11 @@ end it 'control env.sample and .env files' do - dotenv_test_helper + dotenv_test end it 'uses paperclip' do - paperclip_test_helper + paperclip_test end it 'uses mailer' do @@ -205,14 +205,14 @@ end it 'uses devise' do - devise_test_helper + devise_test end it 'uses error_pages' do - error_pages_helper + error_pages_test end it 'uses gitignore' do - gitignore_helper + git_ignore_test end end diff --git a/spec/features/new_not_default_project_spec.rb b/spec/features/new_not_default_project_spec.rb index 1ce0eb7..434aeb1 100644 --- a/spec/features/new_not_default_project_spec.rb +++ b/spec/features/new_not_default_project_spec.rb @@ -42,7 +42,7 @@ end it 'uses responders' do - responder_test_helper + responder_test end it 'uses rollbar' do @@ -108,11 +108,11 @@ end it 'uses config and staging file' do - config_test_helper + config_test end it 'uses locale_language' do - locale_language_test_helper + locale_language_test end it 'uses recipient_interceptor' do @@ -152,11 +152,11 @@ end it 'uses paperclip' do - paperclip_test_helper + paperclip_test end it 'control env.sample and .env files' do - dotenv_test_helper + dotenv_test end it 'uses mailer' do @@ -185,14 +185,14 @@ end it 'uses devise' do - devise_test_helper + devise_test end it 'uses error_pages' do - error_pages_helper + error_pages_test end it 'uses gitignore' do - gitignore_helper + git_ignore_test end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 175eb47..e2aefef 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -11,7 +11,14 @@ RSpec.configure do |config| config.include CybeleTestHelpers - config.include GemTestHelpers + config.include DotenvTestHelper + config.include ConfigTestHelper + config.include DeviseTestHelper + config.include PaperclipTestHelper + config.include LocalLanguageTestHelper + config.include ResponderTestHelper + config.include ErrorPagesTestHelper + config.include GitIgnoreTestHelper config.include MailTestHelpers config.before(:all) do diff --git a/spec/support/config_test_helper.rb b/spec/support/config_test_helper.rb new file mode 100644 index 0000000..feee423 --- /dev/null +++ b/spec/support/config_test_helper.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +module ConfigTestHelper + def config_test + gemfile_file = content('Gemfile') + expect(gemfile_file).to match(/^gem 'config'/) + + config_development_file_test + config_staging_file_test + config_production_file_test + config_test_file_test + end + + private + + def config_development_file_test + config_development_file = content('config/environments/development.rb') + expect(config_development_file).to match(/^Rails.application.configure/) + end + + def config_staging_file_test + config_staging_file = content('config/environments/staging.rb') + expect(config_staging_file).to match(/^Rails.application.configure/) + end + + def config_production_file_test + config_production_file = content('config/environments/production.rb') + expect(config_production_file).to match(/^Rails.application.configure/) + end + + def config_test_file_test + config_test_file = content('config/environments/test.rb') + expect(config_test_file).to match(/^Rails.application.configure/) + end +end diff --git a/spec/support/devise_test_helper.rb b/spec/support/devise_test_helper.rb new file mode 100644 index 0000000..bbc34e8 --- /dev/null +++ b/spec/support/devise_test_helper.rb @@ -0,0 +1,52 @@ +# frozen_string_literal: true + +module DeviseTestHelper + def devise_test + gemfile_file = content('Gemfile') + expect(gemfile_file).to match(/^gem 'devise'/) + + devise_initializers_test + devise_route_file_test + devise_model_file_test + file_control_test + end + + private + + def devise_initializers_test + initializers_devise = content('config/initializers/devise.rb') + expect(initializers_devise).to match('mailer') + expect(initializers_devise).to match('mailer_sender') + + filter_parameter_logging = content('config/initializers/filter_parameter_logging.rb') + expect(filter_parameter_logging).to match(':password') + expect(filter_parameter_logging).to match(':password_confirmation') + end + + def devise_route_file_test + devise_route = content('config/routes.rb') + expect(devise_route).to match('devise_for :users') + end + + def devise_model_file_test # rubocop:disable Metrics/AbcSize + devise_model_file = content('app/models/user.rb') + expect(devise_model_file).to match(':database_authenticatable') + expect(devise_model_file).to match(':registerable') + expect(devise_model_file).to match(':recoverable') + expect(devise_model_file).to match(':rememberable') + expect(devise_model_file).to match(':trackable') + expect(devise_model_file).to match(':validatable') + end + + def file_control_test # rubocop:disable Metrics/AbcSize + expect(File).to exist(file_project_path('app/views/devise/confirmations')) + expect(File).to exist(file_project_path('app/views/devise/mailer')) + expect(File).to exist(file_project_path('app/views/devise/passwords')) + expect(File).to exist(file_project_path('app/views/devise/registrations')) + expect(File).to exist(file_project_path('app/views/devise/sessions')) + expect(File).to exist(file_project_path('app/views/devise/shared')) + expect(File).to exist(file_project_path('app/views/devise/unlocks')) + + expect(File).not_to exist(file_project_path('config/locales/devise.en.yml')) + end +end diff --git a/spec/support/dotenv_test_helper.rb b/spec/support/dotenv_test_helper.rb new file mode 100644 index 0000000..14fdf6a --- /dev/null +++ b/spec/support/dotenv_test_helper.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +module DotenvTestHelper + def dotenv_test + gemfile_file = content('Gemfile') + expect(gemfile_file).to match(/^gem 'dotenv-rails'/) + + env_sample_file_test + env_local_file_test + env_staging_file_test + env_production_file_test + end + + private + + def env_sample_file_test + env_sample_file = content('env.sample') + expect(env_sample_file).to match('ROOT_PATH=http://localhost:3000') + end + + def env_local_file_test + env_local_file = content('.env.local') + expect(env_local_file).to match('ROOT_PATH=http://localhost:3000') + end + + def env_staging_file_test + env_staging_file = content('.env.staging') + expect(env_staging_file).to match('ROOT_PATH=https://staging-dummy_app.herokuapp.com') + end + + def env_production_file_test + env_production_file = content('.env.production') + expect(env_production_file).to match('ROOT_PATH=https://dummy_app.herokuapp.com') + end +end diff --git a/spec/support/error_pages_test_helper.rb b/spec/support/error_pages_test_helper.rb new file mode 100644 index 0000000..5b80f21 --- /dev/null +++ b/spec/support/error_pages_test_helper.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +module ErrorPagesTestHelper + def error_pages_test + controller_file_test + routes_file_test + end + + private + + def controller_file_test # rubocop:disable Metrics/AbcSize + 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') + end + + def routes_file_test + route_file = content('config/routes.rb') + expect(route_file).to match('unmatched_route') + end +end diff --git a/spec/support/gem_test_helpers.rb b/spec/support/gem_test_helpers.rb deleted file mode 100644 index 049c582..0000000 --- a/spec/support/gem_test_helpers.rb +++ /dev/null @@ -1,167 +0,0 @@ -# frozen_string_literal: true - -module GemTestHelpers - def devise_test_helper - gemfile_file = content('Gemfile') - expect(gemfile_file).to match(/^gem 'devise'/) - - initializers_devise = content('config/initializers/devise.rb') - expect(initializers_devise).to match('mailer') - expect(initializers_devise).to match('mailer_sender') - - filter_parameter_logging = content('config/initializers/filter_parameter_logging.rb') - expect(filter_parameter_logging).to match(':password') - expect(filter_parameter_logging).to match(':password_confirmation') - - devise_model_file = content('app/models/user.rb') - expect(devise_model_file).to match(':database_authenticatable') - expect(devise_model_file).to match(':registerable') - expect(devise_model_file).to match(':recoverable') - expect(devise_model_file).to match(':rememberable') - expect(devise_model_file).to match(':trackable') - expect(devise_model_file).to match(':validatable') - - devise_route = content('config/routes.rb') - expect(devise_route).to match('devise_for :users') - - expect(File).to exist(file_project_path('app/views/devise/confirmations')) - expect(File).to exist(file_project_path('app/views/devise/mailer')) - expect(File).to exist(file_project_path('app/views/devise/passwords')) - expect(File).to exist(file_project_path('app/views/devise/registrations')) - expect(File).to exist(file_project_path('app/views/devise/sessions')) - expect(File).to exist(file_project_path('app/views/devise/shared')) - expect(File).to exist(file_project_path('app/views/devise/unlocks')) - - expect(File).not_to exist(file_project_path('config/locales/devise.en.yml')) - end - - def responder_test_helper - gemfile_file = content('Gemfile') - expect(gemfile_file).to match(/^gem 'responders'/) - - lib_file = content('lib/application_responder.rb') - expect(lib_file).to match(/^class ApplicationResponder/) - - controller_file = content('app/controllers/application_controller.rb') - expect(controller_file).to match("^require 'application_responder'") - expect(controller_file).to match('self.responder = ApplicationResponder') - expect(controller_file).to match('respond_to :html, :js, :json') - - expect(File).to exist(file_project_path('config/locales/responders.en.yml')) - locale_file = content('config/locales/responders.tr.yml') - expect(locale_file).not_to match('# alert:') - expect(locale_file).to match('create:') - expect(locale_file).to match('update:') - expect(locale_file).to match('destroy:') - end - - def locale_language_test_helper - expect(File).to exist(file_project_path('config/locales/en.yml')) - locale_file = content('config/locales/tr.yml') - expect(locale_file).to match('phone:') - expect(locale_file).to match('date:') - expect(locale_file).to match('time:') - expect(locale_file).to match('number:') - - locale_file = content('config/locales/email.en.yml') - expect(locale_file).to match('email:') - - locale_file = content('config/locales/email.tr.yml') - expect(locale_file).to match('email:') - - locale_file = content('config/locales/models.en.yml') - expect(locale_file).to match('activerecord:') - - locale_file = content('config/locales/models.tr.yml') - expect(locale_file).to match('activerecord:') - - locale_file = content('config/locales/view.en.yml') - expect(locale_file).to match('view:') - - locale_file = content('config/locales/view.tr.yml') - expect(locale_file).to match('view:') - end - - def paperclip_test_helper - gemfile_file = content('Gemfile') - expect(gemfile_file).to match(/^gem "paperclip"/) - expect(gemfile_file).to match(/^gem 'aws-sdk'/) - - env_sample_file = content('env.sample') - expect(env_sample_file).to match('S3_BUCKET_NAME=') - expect(env_sample_file).to match('AWS_RAW_URL=') - expect(env_sample_file).to match('AWS_ACCESS_KEY_ID=') - expect(env_sample_file).to match('AWS_SECRET_ACCESS_KEY=') - - env_local_file = content('.env.local') - expect(env_local_file).to match('S3_BUCKET_NAME=dummy_app-development') - expect(env_local_file).to match('AWS_RAW_URL=dummy_app-development.s3.amazonaws.com') - expect(env_local_file).to match('AWS_ACCESS_KEY_ID=') - expect(env_local_file).to match('AWS_SECRET_ACCESS_KEY=') - - env_staging_file = content('.env.staging') - expect(env_staging_file).to match('S3_BUCKET_NAME=dummy_app-staging') - expect(env_staging_file).to match('AWS_RAW_URL=dummy_app-staging.s3.amazonaws.com') - expect(env_staging_file).to match('AWS_ACCESS_KEY_ID=') - expect(env_staging_file).to match('AWS_SECRET_ACCESS_KEY=') - - env_production_file = content('.env.production') - expect(env_production_file).to match('S3_BUCKET_NAME=dummy_app') - expect(env_production_file).to match('AWS_RAW_URL=dummy_app.s3.amazonaws.com') - expect(env_production_file).to match('AWS_ACCESS_KEY_ID=') - expect(env_production_file).to match('AWS_SECRET_ACCESS_KEY=') - end - - def dotenv_test_helper - gemfile_file = content('Gemfile') - expect(gemfile_file).to match(/^gem 'dotenv-rails'/) - - env_sample_file = content('env.sample') - expect(env_sample_file).to match('ROOT_PATH=http://localhost:3000') - - env_local_file = content('.env.local') - expect(env_local_file).to match('ROOT_PATH=http://localhost:3000') - - env_staging_file = content('.env.staging') - expect(env_staging_file).to match('ROOT_PATH=https://staging-dummy_app.herokuapp.com') - - env_production_file = content('.env.production') - expect(env_production_file).to match('ROOT_PATH=https://dummy_app.herokuapp.com') - end - - def config_test_helper - gemfile_file = content('Gemfile') - expect(gemfile_file).to match(/^gem 'config'/) - - config_development_file = content('config/environments/development.rb') - expect(config_development_file).to match(/^Rails.application.configure/) - - config_staging_file = content('config/environments/staging.rb') - expect(config_staging_file).to match(/^Rails.application.configure/) - - config_production_file = content('config/environments/production.rb') - expect(config_production_file).to match(/^Rails.application.configure/) - - 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/git_ignore_test_helper.rb b/spec/support/git_ignore_test_helper.rb new file mode 100644 index 0000000..80c2164 --- /dev/null +++ b/spec/support/git_ignore_test_helper.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +module GitIgnoreTestHelper + def git_ignore_test + 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/locale_language_test_helper.rb b/spec/support/locale_language_test_helper.rb new file mode 100644 index 0000000..c2bd41f --- /dev/null +++ b/spec/support/locale_language_test_helper.rb @@ -0,0 +1,43 @@ +# frozen_string_literal: true + +module LocalLanguageTestHelper + def locale_language_test + expect(File).to exist(file_project_path('config/locales/en.yml')) + + file_content_control_test + english_file_test + turkish_file_test + end + + private + + def file_content_control_test + locale_file = content('config/locales/tr.yml') + expect(locale_file).to match('phone:') + expect(locale_file).to match('date:') + expect(locale_file).to match('time:') + expect(locale_file).to match('number:') + end + + def english_file_test + locale_file = content('config/locales/email.en.yml') + expect(locale_file).to match('email:') + + locale_file = content('config/locales/models.en.yml') + expect(locale_file).to match('activerecord:') + + locale_file = content('config/locales/view.en.yml') + expect(locale_file).to match('view:') + end + + def turkish_file_test + locale_file = content('config/locales/email.tr.yml') + expect(locale_file).to match('email:') + + locale_file = content('config/locales/models.tr.yml') + expect(locale_file).to match('activerecord:') + + locale_file = content('config/locales/view.tr.yml') + expect(locale_file).to match('view:') + end +end diff --git a/spec/support/mail_test_helpers.rb b/spec/support/mail_test_helpers.rb index 031dbe2..23578cf 100644 --- a/spec/support/mail_test_helpers.rb +++ b/spec/support/mail_test_helpers.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true module MailTestHelpers - def mail_test_helper(path) + def mail_test_helper(path) # rubocop:disable Metrics/AbcSize file = content(path) expect(file).to match('smtp') expect(file).to match('address:') diff --git a/spec/support/paperclip_test_helper.rb b/spec/support/paperclip_test_helper.rb new file mode 100644 index 0000000..38b6807 --- /dev/null +++ b/spec/support/paperclip_test_helper.rb @@ -0,0 +1,48 @@ +# frozen_string_literal: true + +module PaperclipTestHelper + def paperclip_test + gemfile_file = content('Gemfile') + expect(gemfile_file).to match(/^gem "paperclip"/) + expect(gemfile_file).to match(/^gem 'aws-sdk'/) + + env_sample_file_test + env_local_file_test + env_staging_file_test + env_production_file_test + end + + private + + def env_sample_file_test + env_sample_file = content('env.sample') + expect(env_sample_file).to match('S3_BUCKET_NAME=') + expect(env_sample_file).to match('AWS_RAW_URL=') + expect(env_sample_file).to match('AWS_ACCESS_KEY_ID=') + expect(env_sample_file).to match('AWS_SECRET_ACCESS_KEY=') + end + + def env_local_file_test + env_local_file = content('.env.local') + expect(env_local_file).to match('S3_BUCKET_NAME=dummy_app-development') + expect(env_local_file).to match('AWS_RAW_URL=dummy_app-development.s3.amazonaws.com') + expect(env_local_file).to match('AWS_ACCESS_KEY_ID=') + expect(env_local_file).to match('AWS_SECRET_ACCESS_KEY=') + end + + def env_staging_file_test + env_staging_file = content('.env.staging') + expect(env_staging_file).to match('S3_BUCKET_NAME=dummy_app-staging') + expect(env_staging_file).to match('AWS_RAW_URL=dummy_app-staging.s3.amazonaws.com') + expect(env_staging_file).to match('AWS_ACCESS_KEY_ID=') + expect(env_staging_file).to match('AWS_SECRET_ACCESS_KEY=') + end + + def env_production_file_test + env_production_file = content('.env.production') + expect(env_production_file).to match('S3_BUCKET_NAME=dummy_app') + expect(env_production_file).to match('AWS_RAW_URL=dummy_app.s3.amazonaws.com') + expect(env_production_file).to match('AWS_ACCESS_KEY_ID=') + expect(env_production_file).to match('AWS_SECRET_ACCESS_KEY=') + end +end diff --git a/spec/support/responder_test_helper.rb b/spec/support/responder_test_helper.rb new file mode 100644 index 0000000..6bc72c6 --- /dev/null +++ b/spec/support/responder_test_helper.rb @@ -0,0 +1,32 @@ +# frozen_string_literal: true + +module ResponderTestHelper + def responder_test + gemfile_file = content('Gemfile') + expect(gemfile_file).to match(/^gem 'responders'/) + + lib_file = content('lib/application_responder.rb') + expect(lib_file).to match(/^class ApplicationResponder/) + + controller_file_test + i18n_file_test + end + + private + + def controller_file_test + controller_file = content('app/controllers/application_controller.rb') + expect(controller_file).to match("^require 'application_responder'") + expect(controller_file).to match('self.responder = ApplicationResponder') + expect(controller_file).to match('respond_to :html, :js, :json') + end + + def i18n_file_test # rubocop:disable Metrics/AbcSize + expect(File).to exist(file_project_path('config/locales/responders.en.yml')) + locale_file = content('config/locales/responders.tr.yml') + expect(locale_file).not_to match('# alert:') + expect(locale_file).to match('create:') + expect(locale_file).to match('update:') + expect(locale_file).to match('destroy:') + end +end From 1fa93101eee5c49692238672920898e39f58ae6b Mon Sep 17 00:00:00 2001 From: hamdibayhan Date: Tue, 21 Nov 2017 11:15:07 +0300 Subject: [PATCH 3/4] KBP-140 #time 2m - Code was improved --- spec/spec_helper.rb | 2 +- spec/support/locale_language_test_helper.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index e2aefef..9f7b36d 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -15,7 +15,7 @@ config.include ConfigTestHelper config.include DeviseTestHelper config.include PaperclipTestHelper - config.include LocalLanguageTestHelper + config.include LocaleLanguageTestHelper config.include ResponderTestHelper config.include ErrorPagesTestHelper config.include GitIgnoreTestHelper diff --git a/spec/support/locale_language_test_helper.rb b/spec/support/locale_language_test_helper.rb index c2bd41f..cb87255 100644 --- a/spec/support/locale_language_test_helper.rb +++ b/spec/support/locale_language_test_helper.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -module LocalLanguageTestHelper +module LocaleLanguageTestHelper def locale_language_test expect(File).to exist(file_project_path('config/locales/en.yml')) From f058b96d1f679bf9d31192da58261bdba5ac1a3a Mon Sep 17 00:00:00 2001 From: hamdibayhan Date: Tue, 21 Nov 2017 11:55:25 +0300 Subject: [PATCH 4/4] KBP-140 #time 10m - Code was improved --- lib/cybele/helpers/error_pages.rb | 2 +- templates/error_pages/error_control.erb | 8 +++++--- templates/error_pages/error_route.erb | 2 +- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/cybele/helpers/error_pages.rb b/lib/cybele/helpers/error_pages.rb index 9baeb83..02bdd3d 100644 --- a/lib/cybele/helpers/error_pages.rb +++ b/lib/cybele/helpers/error_pages.rb @@ -6,7 +6,7 @@ module ErrorPages def configure_error_pages inject_into_file 'app/controllers/application_controller.rb', template_content('error_pages/error_control.erb'), - before: 'self.responder' + after: 'class ApplicationController < ActionController::Base' inject_into_file 'app/controllers/application_controller.rb', template_content('error_pages/error_method.erb'), diff --git a/templates/error_pages/error_control.erb b/templates/error_pages/error_control.erb index a5dff31..e033b8a 100644 --- a/templates/error_pages/error_control.erb +++ b/templates/error_pages/error_control.erb @@ -1,5 +1,7 @@ - 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? + if Rails.env.production? || Rails.env.staging? + rescue_from Exception, with: :server_error + rescue_from ActiveRecord::RecordNotFound, with: :page_not_found + rescue_from ActionController::RoutingError, with: :page_not_found + end diff --git a/templates/error_pages/error_route.erb b/templates/error_pages/error_route.erb index 4732d9c..d1e2d0e 100644 --- a/templates/error_pages/error_route.erb +++ b/templates/error_pages/error_route.erb @@ -1,4 +1,4 @@ - if Rails.env.production? or Rails.env.staging? + if Rails.env.production? || Rails.env.staging? match '*unmatched_route', to: 'application#page_not_found', via: :all end