Skip to content

Commit

Permalink
[101] Create method and message to record not found access (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
0jonjo authored Mar 16, 2024
1 parent 644fa0d commit 869aac6
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 87 deletions.
9 changes: 9 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
# frozen_string_literal: true

require 'active_record'

class ApplicationController < ActionController::Base
rescue_from ActiveRecord::RecordNotFound, with: :invalid_page

def user_has_profile
return unless user_signed_in?

redirect_to new_profile_path if current_user.profile.blank?
end

def invalid_page
logger.error "Attempt to access invalid page: #{request.url}"
redirect_to root_url, notice: I18n.t('errors.messages.invalid_page')
end
end
2 changes: 1 addition & 1 deletion app/controllers/proposal_comments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@ def proposal_comment
end

def proposal_comment_params
params.require(:proposal_comment).permit(:id, :proposal_id, :author_id, :author_type, :body)
params.require(:proposal_comment).permit(:proposal_id, :author_id, :author_type, :body)
end
end
33 changes: 0 additions & 33 deletions config/locales/en.yml

This file was deleted.

3 changes: 2 additions & 1 deletion config/locales/pt-BR-rails.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pt-BR:
long: "%d de %B de %Y"
short: "%d de %B"
month_names:
-
-
- janeiro
- fevereiro
- março
Expand Down Expand Up @@ -120,6 +120,7 @@ pt-BR:
greater_than_or_equal_to: deve ser maior ou igual a %{count}
inclusion: não está incluído na lista
invalid: não é válido
invalid_page: "Página inválida"
less_than: deve ser menor que %{count}
less_than_or_equal_to: deve ser menor ou igual a %{count}
model_invalid: 'A validação falhou: %{errors}'
Expand Down
118 changes: 66 additions & 52 deletions spec/system/visitor_visit_homepage_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,66 +3,80 @@
require 'rails_helper'

describe 'Visitor visit homepage' do
it 'and view title and menu content' do
visit root_path
expect(page).to have_content('All Jobs')
expect(page).to have_link('All Jobs', href: root_path)
expect(page).to have_content(I18n.t('go_to_login'))
within('nav') do
expect(page).to have_link('Login User', href: new_user_session_path)
expect(page).to have_link('Login Headhunter', href: new_headhunter_session_path)
expect(page).not_to have_link(Profile.model_name.human, href: new_profile_path)
expect(page).not_to have_link('Logout User', href: destroy_user_session_path)
expect(page).not_to have_link('Logout Headhunter', href: destroy_headhunter_session_path)
expect(page).not_to have_link(I18n.t('openings'), href: jobs_path)
expect(page).not_to have_link(I18n.t('new_opening'), href: new_job_path)
expect(page).not_to have_link(I18n.t('profiles'), href: profiles_path)
expect(page).not_to have_link(I18n.t('stars'), href: stars_path)
expect(page).not_to have_link(I18n.t('applies'), href: applies_path)
context 'when view title and menu content' do
it 'only see login options' do
visit root_path
expect(page).to have_content('All Jobs')
expect(page).to have_link('All Jobs', href: root_path)
expect(page).to have_content(I18n.t('go_to_login'))
within('nav') do
expect(page).to have_link('Login User', href: new_user_session_path)
expect(page).to have_link('Login Headhunter', href: new_headhunter_session_path)
expect(page).not_to have_link(Profile.model_name.human, href: new_profile_path)
expect(page).not_to have_link('Logout User', href: destroy_user_session_path)
expect(page).not_to have_link('Logout Headhunter', href: destroy_headhunter_session_path)
expect(page).not_to have_link(I18n.t('openings'), href: jobs_path)
expect(page).not_to have_link(I18n.t('new_opening'), href: new_job_path)
expect(page).not_to have_link(I18n.t('profiles'), href: profiles_path)
expect(page).not_to have_link(I18n.t('stars'), href: stars_path)
expect(page).not_to have_link(I18n.t('applies'), href: applies_path)
end
end
end

it 'after User login and view title and menu content' do
user = User.create!(email: '[email protected]', password: 'test123')
login_as(user, scope: :user)
visit root_path
context 'when login as user and view title and menu content' do
it 'see only user menu' do
user = User.create!(email: '[email protected]', password: 'test123')
login_as(user, scope: :user)
visit root_path

expect(page).to have_content(I18n.t('choose_option'))
expect(page).to have_link('All Jobs', href: root_path)
within('nav') do
expect(page).to have_content("#{I18n.t('you_are')} [email protected]")
expect(page).to have_link(Profile.model_name.human, href: new_profile_path)
expect(page).to have_link(I18n.t('openings'), href: jobs_path)
expect(page).to have_link('Logout User', href: destroy_user_session_path)
expect(page).to have_link(I18n.t('applies'), href: applies_path)
expect(page).not_to have_link('Logout Headhunter', href: destroy_headhunter_session_path)
expect(page).not_to have_link(I18n.t('new_opening'), href: new_job_path)
expect(page).not_to have_link(I18n.t('profiles'), href: profiles_path)
expect(page).not_to have_link(I18n.t('stars'), href: stars_path)
expect(page).not_to have_link('Login User', href: new_user_session_path)
expect(page).not_to have_link('Login Headhunter', href: new_headhunter_session_path)
expect(page).to have_content(I18n.t('choose_option'))
expect(page).to have_link('All Jobs', href: root_path)
within('nav') do
expect(page).to have_content("#{I18n.t('you_are')} [email protected]")
expect(page).to have_link(Profile.model_name.human, href: new_profile_path)
expect(page).to have_link(I18n.t('openings'), href: jobs_path)
expect(page).to have_link('Logout User', href: destroy_user_session_path)
expect(page).to have_link(I18n.t('applies'), href: applies_path)
expect(page).not_to have_link('Logout Headhunter', href: destroy_headhunter_session_path)
expect(page).not_to have_link(I18n.t('new_opening'), href: new_job_path)
expect(page).not_to have_link(I18n.t('profiles'), href: profiles_path)
expect(page).not_to have_link(I18n.t('stars'), href: stars_path)
expect(page).not_to have_link('Login User', href: new_user_session_path)
expect(page).not_to have_link('Login Headhunter', href: new_headhunter_session_path)
end
end
end

it 'after Headhunter login and view title and menu content' do
headhunter = Headhunter.create!(email: '[email protected]', password: 'test123')
login_as(headhunter, scope: :headhunter)
visit root_path
context 'when login as headhunter and view title and menu content' do
it 'see only the headhunter menu' do
headhunter = Headhunter.create!(email: '[email protected]', password: 'test123')
login_as(headhunter, scope: :headhunter)
visit root_path

expect(page).to have_content(I18n.t('choose_option'))
expect(page).to have_link('All Jobs', href: root_path)
within('nav') do
expect(page).to have_content("#{I18n.t('you_are')} [email protected]")
expect(page).to have_link('Logout Headhunter', href: destroy_headhunter_session_path)
expect(page).to have_link(I18n.t('new_opening'), href: new_job_path)
expect(page).to have_link(I18n.t('profiles'), href: profiles_path)
expect(page).to have_link(I18n.t('stars'), href: stars_path)
expect(page).to have_link(I18n.t('openings'), href: jobs_path)
expect(page).not_to have_link(I18n.t('applies'), href: applies_path)
expect(page).not_to have_link(Profile.model_name.human, href: new_profile_path)
expect(page).not_to have_link('Logout User', href: destroy_user_session_path)
expect(page).not_to have_link('Login User', href: new_user_session_path)
expect(page).not_to have_link('Login Headhunter', href: new_headhunter_session_path)
expect(page).to have_content(I18n.t('choose_option'))
expect(page).to have_link('All Jobs', href: root_path)
within('nav') do
expect(page).to have_content("#{I18n.t('you_are')} [email protected]")
expect(page).to have_link('Logout Headhunter', href: destroy_headhunter_session_path)
expect(page).to have_link(I18n.t('new_opening'), href: new_job_path)
expect(page).to have_link(I18n.t('profiles'), href: profiles_path)
expect(page).to have_link(I18n.t('stars'), href: stars_path)
expect(page).to have_link(I18n.t('openings'), href: jobs_path)
expect(page).not_to have_link(I18n.t('applies'), href: applies_path)
expect(page).not_to have_link(Profile.model_name.human, href: new_profile_path)
expect(page).not_to have_link('Logout User', href: destroy_user_session_path)
expect(page).not_to have_link('Login User', href: new_user_session_path)
expect(page).not_to have_link('Login Headhunter', href: new_headhunter_session_path)
end
end
end

context 'when try to access a not found record' do
it 'redirect to home page and return invalid page message' do
visit profile_path(9_999_999_999)
expect(current_path).to eq(root_path)
expect(page).to have_content(I18n.t('errors.messages.invalid_page'))
end
end
end

0 comments on commit 869aac6

Please sign in to comment.