Skip to content

Commit e755ed2

Browse files
committed
Feat/Usuario edita e deleta categoria de trabalho
Co-authored-by: Lucas Vasques <[email protected]>
1 parent d216962 commit e755ed2

14 files changed

+258
-49
lines changed

app/assets/images/edit-icon.svg

+4
Loading

app/assets/images/x-icon.svg

+3
Loading

app/controllers/profile_job_categories_controller.rb

+25-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
class ProfileJobCategoriesController < ApplicationController
2-
before_action :authenticate_user!, only: %w[new create]
2+
before_action :authenticate_user!, only: %w[new create edit destroy update]
33
before_action :check_if_job_categories_exist, only: %w[new create]
4+
before_action :set_profile_job_category, only: %w[edit destroy update]
5+
before_action :authorize, only: %w[edit destroy update]
46

57
def new
68
@profile_job_category = current_user.profile.profile_job_categories.build
7-
flash.now[:notice] = t('.inform_user_edit_is_unavailable')
89
end
910

1011
def create
@@ -18,6 +19,18 @@ def create
1819
end
1920
end
2021

22+
def edit; end
23+
24+
def destroy
25+
@profile_job_category.destroy!
26+
redirect_to current_user.profile, notice: t('.success')
27+
end
28+
29+
def update
30+
@profile_job_category.update! profile_job_category_params
31+
redirect_to current_user.profile, notice: t('.success')
32+
end
33+
2134
private
2235

2336
def profile_job_category_params
@@ -30,4 +43,14 @@ def check_if_job_categories_exist
3043
redirect_to profile_path(current_user.profile),
3144
notice: t('.check_if_job_categories_exist.error')
3245
end
46+
47+
def set_profile_job_category
48+
@profile_job_category = ProfileJobCategory.find params[:id]
49+
end
50+
51+
def authorize
52+
return if current_user.profile == @profile_job_category.profile
53+
54+
redirect_to current_user.profile, alert: t('alerts.unauthorized')
55+
end
3356
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<% if @profile_job_category.errors.any? %>
2+
<% @profile_job_category.errors.full_messages.each do |msg| %>
3+
<p class="alert alert-warning">
4+
<%= msg %>
5+
</p>
6+
<% end %>
7+
<% end %>
8+
<%= form_with model: @profile_job_category do |f| %>
9+
<% if should_render %>
10+
<div class="mb-3">
11+
<%= f.label :job_category_id, class: 'form-label' %>
12+
<%= f.collection_select :job_category_id, JobCategory.order(:name), :id, :name, {prompt: 'Selecione uma categoria'}, class: 'form-select' %>
13+
</div>
14+
<% end %>
15+
<div class="mb-3">
16+
<%= f.label :description, class: 'form-label' %>
17+
<%= f.text_area :description, class: 'form-control' %>
18+
</div>
19+
<div class="mb-3">
20+
<%= f.submit t('save_btn'), class: 'btn btn-primary' %>
21+
<%= link_to t('return_btn'), profile_path(current_user.profile), class: 'btn btn-secondary' %>
22+
</div>
23+
<% end %>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<h1><%= t('.form_title') %></h1>
2+
<%= render partial: 'form', locals: { should_render: false } %>
+2-22
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,2 @@
1-
<h1><%= t('content.new_form_title') %></h1>
2-
<% if @profile_job_category.errors.any? %>
3-
<% @profile_job_category.errors.full_messages.each do |msg| %>
4-
<p class="alert alert-warning">
5-
<%= msg %>
6-
</p>
7-
<% end %>
8-
<% end %>
9-
<%= form_with model: @profile_job_category do |f| %>
10-
<div class="mb-3">
11-
<%= f.label :job_category_id, class: 'form-label' %>
12-
<%= f.collection_select :job_category_id, JobCategory.order(:name), :id, :name, {prompt: 'Selecione uma categoria'}, class: 'form-select' %>
13-
</div>
14-
<div class="mb-3">
15-
<%= f.label :description, class: 'form-label' %>
16-
<%= f.text_area :description, class: 'form-control' %>
17-
</div>
18-
<div class="mb-3">
19-
<%= f.submit t('save_btn'), class: 'btn btn-primary' %>
20-
<%= link_to t('return_btn'), profile_path(current_user.profile), class: 'btn btn-secondary' %>
21-
</div>
22-
<% end %>
1+
<h1><%= t('.form_title') %></h1>
2+
<%= render partial: 'form', locals: { should_render: true } %>

app/views/profiles/_profile_details.html.erb

+25-11
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<div class="border py-1 px-2 rounded mb-3">
1414
<div class="d-flex gap-1">
1515
<%= image_tag 'person-fill', width: '28rem', class: 'mb-2' %>
16-
<h3><%= PersonalInfo.model_name.human %></h3>
16+
<h3><%= PersonalInfo.model_name.human %></h3>
1717
</div>
1818
<%= render 'personal_info', personal_info: personal_info, user_is_owner: user_is_owner %>
1919
<%= link_to 'Editar Informações Pessoais', edit_user_profile_path, class:'btn btn-secondary mx-4 mb-3' if user_is_owner %>
@@ -27,7 +27,7 @@
2727
<h3><%= ProfessionalInfo.model_name.human %></h3>
2828
</div>
2929
<% end %>
30-
30+
3131
<p>
3232
<%= link_to 'Adicionar Experiência Profissional', new_user_profile_professional_info_path, class:'btn btn-primary' if user_is_owner %>
3333
</p>
@@ -64,7 +64,7 @@
6464
</p>
6565
<% end %>
6666
</div>
67-
67+
6868
<div class="border py-1 px-2 rounded mb-3">
6969
<% if profile.education_infos.visibles_list.any? || user_is_owner %>
7070
<div class="d-flex gap-2">
@@ -77,7 +77,7 @@
7777
</p>
7878
<% profile.education_infos.each do |education_info| %>
7979
<% if education_info.visibility || user_is_owner %>
80-
<h4>
80+
<h4>
8181
<%= EducationInfo.human_attribute_name(:institution) %>: <%= education_info.institution %>
8282
</h4>
8383
<ul>
@@ -104,22 +104,36 @@
104104
</div>
105105
</div>
106106
<div id='profile-job-categories' class="border py-1 px-2 rounded mb-3">
107-
<% if profile.profile_job_categories.any? || user_is_owner %>
107+
<% if profile.profile_job_categories.any? || user_is_owner %>
108108
<div class="d-flex gap-1">
109109
<%= image_tag 'person-working', width: '28rem', class: 'mb-2 mx-1' %>
110110
<h3><%= t('content.section_title') %></h3>
111111
</div>
112-
<% end %>
112+
<% end %>
113113

114114
<%= link_to t('links.new_profile_job_category_path'), new_profile_job_category_path, class:'btn btn-primary' if user_is_owner %>
115115
<div class='mt-2'>
116116
<% profile.profile_job_categories.each do |profile_job_category| %>
117117
<p><strong><%= profile_job_category.job_category.name %></strong></p>
118-
<% if profile_job_category.description.present? %>
119-
<p><%= profile_job_category.description %></p>
120-
<% else %>
121-
<p><%= t('content.profile_job_category_without_description') %></p>
122-
<% end %>
118+
<div class='d-flex row'>
119+
<div class='d-flex column col-6 align-text-top'>
120+
<% if profile_job_category.description.present? %>
121+
<p><%= profile_job_category.description %></p>
122+
<% else %>
123+
<p><%= t('content.profile_job_category_without_description') %></p>
124+
<% end %>
125+
</div>
126+
<% if profile.user == current_user %>
127+
<div class='d-flex column col-6'>
128+
<%= button_to edit_profile_job_category_path(profile_job_category), method: :get, class:'btn btn-sm btn-edit-category' do %>
129+
<%= image_tag 'edit-icon', width: '20rem', height: '20rem' %>
130+
<% end %>
131+
<%= button_to profile_job_category_path(profile_job_category), method: :delete, class:'btn btn-sm btn-delete-category' do %>
132+
<%= image_tag 'x-icon', width: '20rem', height: '20rem' %>
133+
<% end %>
134+
</div>
135+
<% end %>
136+
</div>
123137
<% end %>
124138
</div>
125139
</div>

config/locales/models/profile_job_category.pt-BR.yml

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
pt-BR:
22
activerecord:
33
models:
4-
profile_job_category:
4+
profile_job_category:
55
one: Categoria de trabalho
66
other: Categorias de trabalho
77
attributes:
@@ -13,11 +13,18 @@ pt-BR:
1313
create:
1414
success: Categoria de trabalho adicionada com sucesso!
1515
error: Não foi possível adicionar a categoria de trabalho.
16+
destroy:
17+
success: Categoria de trabalho deletada com sucesso!
1618
new:
1719
inform_user_edit_is_unavailable: 'Atenção: Os dados cadastrados não poderão ser alterados após salvar. Preencha com cuidado.'
1820
check_if_job_categories_exist:
1921
error: Essa página não está disponível no momento, entre em contato com o administrador.
20-
links:
22+
form_title: Cadastrar Categoria de Trabalho no Perfil
23+
edit:
24+
form_title: Editar Categoria de Trabalho no Perfil
25+
update:
26+
success: Categoria de trabalho editada com sucesso!
27+
links:
2128
new_profile_job_category_path: Adicionar nova categoria de trabalho
2229
content:
2330
profile_job_category_without_description: Não foi adicionada uma descrição para essa categoria.

config/routes.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
resources :education_infos, shallow: true, only: %i[new create edit update]
6868
end
6969

70-
resources :profile_job_categories, only: %i[new create]
70+
resources :profile_job_categories, only: %i[new create edit destroy update]
7171

7272
namespace :api do
7373
namespace :v1 do
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
require 'rails_helper'
2+
3+
describe 'Usuário remove categoria de trabalho' do
4+
it 'mas precisa estar logado' do
5+
user = create(:user, full_name: 'Zelda Hyrule')
6+
job_category = create(:job_category, name: 'Luta com espada')
7+
job_category_two = create(:job_category, name: 'Magia')
8+
ProfileJobCategory.create!(job_category:, profile: user.profile, description: 'Eu sou muito bom nessa categoria')
9+
ProfileJobCategory.create!(job_category: job_category_two, profile: user.profile)
10+
11+
delete profile_job_category_path job_category
12+
13+
expect(response).to redirect_to new_user_session_path
14+
expect(flash[:alert]).to eq 'Para continuar, faça login ou registre-se.'
15+
end
16+
17+
it 'mas precisa ser dono da categoria' do
18+
user = create(:user, full_name: 'Zelda Hyrule')
19+
another_user = create(:user)
20+
job_category = create(:job_category, name: 'Luta com espada')
21+
job_category_two = create(:job_category, name: 'Magia')
22+
ProfileJobCategory.create!(job_category:, profile: user.profile, description: 'Eu sou muito bom nessa categoria')
23+
ProfileJobCategory.create!(job_category: job_category_two, profile: user.profile)
24+
25+
login_as another_user
26+
delete profile_job_category_path job_category
27+
28+
expect(response).to redirect_to profile_path(another_user.profile)
29+
expect(flash[:alert]).to eq 'Você não têm permissão para realizar essa ação.'
30+
end
31+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
require 'rails_helper'
2+
3+
describe 'Usuário remove categoria de trabalho' do
4+
it 'mas precisa estar logado' do
5+
user = create(:user, full_name: 'Zelda Hyrule')
6+
job_category = create(:job_category, name: 'Luta com espada')
7+
job_category_two = create(:job_category, name: 'Magia')
8+
ProfileJobCategory.create!(job_category:, profile: user.profile, description: 'Eu sou muito bom nessa categoria')
9+
ProfileJobCategory.create!(job_category: job_category_two, profile: user.profile)
10+
11+
patch profile_job_category_path job_category
12+
13+
expect(response).to redirect_to new_user_session_path
14+
expect(flash[:alert]).to eq 'Para continuar, faça login ou registre-se.'
15+
end
16+
17+
it 'mas precisa ser dono da categoria' do
18+
user = create(:user, full_name: 'Zelda Hyrule')
19+
another_user = create(:user)
20+
job_category = create(:job_category, name: 'Luta com espada')
21+
job_category_two = create(:job_category, name: 'Magia')
22+
ProfileJobCategory.create!(job_category:, profile: user.profile, description: 'Eu sou muito bom nessa categoria')
23+
ProfileJobCategory.create!(job_category: job_category_two, profile: user.profile)
24+
25+
login_as another_user
26+
patch profile_job_category_path job_category
27+
28+
expect(response).to redirect_to profile_path(another_user.profile)
29+
expect(flash[:alert]).to eq 'Você não têm permissão para realizar essa ação.'
30+
end
31+
end

spec/system/profile_job_categories/user_create_profile_job_category_spec.rb

-11
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,6 @@
7676
expect(page).to have_current_path profile_path(user.profile)
7777
end
7878

79-
it 'e é alertado no formulário sobre não poder editar ou remover os dados no futuro' do
80-
user = create(:user)
81-
create(:job_category)
82-
83-
login_as user
84-
visit new_profile_job_category_path
85-
86-
alert_text = 'Atenção: Os dados cadastrados não poderão ser alterados após salvar. Preencha com cuidado.'
87-
expect(page).to have_content alert_text
88-
end
89-
9079
it 'e vê um link para retornar à página de meu perfil' do
9180
user = create(:user)
9281
create(:job_category)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
require 'rails_helper'
2+
3+
describe 'Usuário deleta categoria de trabalho' do
4+
it 'do seu perfil com sucesso' do
5+
user = create(:user, full_name: 'Zelda Hyrule')
6+
job_category = create(:job_category, name: 'Luta com espada')
7+
job_category_two = create(:job_category, name: 'Magia')
8+
ProfileJobCategory.create!(job_category:, profile: user.profile, description: 'Eu sou muito bom nessa categoria')
9+
ProfileJobCategory.create!(job_category: job_category_two, profile: user.profile)
10+
11+
login_as user
12+
visit profile_path(user.profile)
13+
page.all('.btn-delete-category').to_a.second.click
14+
15+
expect(ProfileJobCategory.count).to eq 1
16+
expect(page).to have_content 'Eu sou muito bom nessa categoria'
17+
expect(page).to have_content 'Categoria de trabalho deletada com sucesso!'
18+
expect(page).to have_content 'Luta com espada'
19+
expect(page).not_to have_content 'Magia'
20+
end
21+
22+
it 'e não consegue ver o botão em outro perfil' do
23+
user = create(:user, full_name: 'Zelda Hyrule')
24+
other_user = create(:user)
25+
job_category = create(:job_category, name: 'Luta com espada')
26+
job_category_two = create(:job_category, name: 'Magia')
27+
ProfileJobCategory.create!(job_category:, profile: user.profile, description: 'Eu sou muito bom nessa categoria')
28+
ProfileJobCategory.create!(job_category: job_category_two, profile: user.profile)
29+
30+
login_as other_user
31+
visit profile_path(user.profile)
32+
33+
expect(page).to have_content 'Zelda Hyrule'
34+
expect(page).to have_content 'Luta com espada'
35+
expect(page).to have_content 'Magia'
36+
expect(page.all('.btn-delete-category').count).to eq 0
37+
end
38+
end

0 commit comments

Comments
 (0)