-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from TreinaDev/feature/edicao-de-perfil
Feature/edicao de perfil
- Loading branch information
Showing
21 changed files
with
405 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,29 @@ | ||
class ProfilesController < ApplicationController | ||
before_action :authenticate_user!, only: %i[show edit update] | ||
before_action :authorize_user, only: %i[edit update] | ||
before_action :set_profile, only: %i[show edit update] | ||
|
||
def show; end | ||
|
||
def edit; end | ||
|
||
def update | ||
@profile.update(profile_params) | ||
redirect_to profile_path(@profile), notice: t('.success') | ||
end | ||
|
||
private | ||
|
||
def set_profile | ||
@profile = current_user.profile | ||
end | ||
|
||
def profile_params | ||
params.require(:profile).permit(:first_name, :last_name, :work_experience, | ||
:education) | ||
end | ||
|
||
def authorize_user | ||
redirect_to root_path unless current_user == Profile.find(params[:id]).user | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,18 @@ | ||
class Profile < ApplicationRecord | ||
belongs_to :user | ||
|
||
def full_name | ||
return first_name if last_name.blank? | ||
return last_name if first_name.blank? | ||
|
||
"#{first_name} #{last_name}" | ||
end | ||
|
||
def first_update? | ||
attributes.slice('first_name', 'last_name', 'work_experience', 'education').each_value do |v| | ||
return false if v.present? | ||
end | ||
|
||
true | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
</head> | ||
|
||
<body> | ||
|
||
<div class="container"> | ||
<%= render partial: 'layouts/navbar' %> | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<%= form_with model: profile do |f| %> | ||
<div class="mb-3"> | ||
<%= f.label :first_name, class: 'form-label' %> | ||
<%= f.text_field :first_name, class: 'form-control' %> | ||
</div> | ||
|
||
<div class="mb-3"> | ||
<%= f.label :last_name, class: 'form-label' %> | ||
<%= f.text_field :last_name, class: 'form-control' %> | ||
</div> | ||
|
||
<div class="mb-3"> | ||
<%= f.label :work_experience, class: 'form-label' %> | ||
<%= f.text_area :work_experience, class: 'form-control' %> | ||
</div> | ||
|
||
<div class="mb-3"> | ||
<%= f.label :education, class: 'form-label' %> | ||
<%= f.text_field :education, class: 'form-control' %> | ||
</div> | ||
|
||
<div> | ||
<% if profile.first_update? %> | ||
<%= link_to t('skip'), root_path, class: 'btn btn-danger' %> | ||
<%= f.submit t('helpers.submit.complete', | ||
model: Profile.model_name.human), | ||
class: 'btn btn-primary' %> | ||
<% else %> | ||
<%= link_to t('back'), profile_path(@profile), class: 'btn btn-danger' %> | ||
<%= f.submit t('helpers.submit.update', | ||
model: Profile.model_name.human), | ||
class: 'btn btn-primary' %> | ||
<% end %> | ||
</div> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<p> | ||
<strong><%= t 'full_name' %>: </strong> | ||
<%= profile.full_name %> | ||
</p> | ||
|
||
<p> | ||
<strong><%= Profile.human_attribute_name :work_experience %>: </strong> | ||
<%= profile.work_experience %> | ||
</p> | ||
|
||
<p> | ||
<strong><%= Profile.human_attribute_name :education %>: </strong> | ||
<%= profile.education %> | ||
</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,7 @@ | ||
<h1>Profiles#edit</h1> | ||
<p>Find me in app/views/profiles/edit.html.erb</p> | ||
<% if @profile.first_update? %> | ||
<h1><%= t 'complete_profile_msg' %></h1> | ||
<% else %> | ||
<h1><%= t 'update_profile_msg' %></h1> | ||
<% end %> | ||
|
||
<%= render partial: 'form', locals: { profile: @profile } %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<h1>Meu <%= Profile.model_name.human %></h1> | ||
|
||
|
||
<% if @profile.first_update? %> | ||
<h2>Seu perfil ainda não tem informações</h2> | ||
<p> | ||
<strong>Clique no botão abaixo para preencher</strong> | ||
</p> | ||
|
||
<%= link_to 'Completar perfil', | ||
edit_profile_path(current_user.profile), | ||
class: 'btn btn-primary' %> | ||
<% else %> | ||
<%= render partial: 'profile_info', locals: { profile: @profile } %> | ||
<%= link_to t('helpers.submit.update', model: Profile.model_name.human), | ||
edit_profile_path(current_user.profile), | ||
class: 'btn btn-primary' %> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
pt-BR: | ||
activerecord: | ||
models: | ||
profile: | ||
one: Perfil | ||
other: Perfis | ||
attributes: | ||
profile: | ||
first_name: Nome | ||
last_name: Sobrenome | ||
work_experience: Experiência profissional | ||
education: Informação acadêmica | ||
profiles: | ||
update: | ||
success: Perfil atualizado com sucesso! | ||
error: Não foi possível atualizar perfil! | ||
full_name: Nome | ||
skip: Pular etapa | ||
back: Voltar | ||
complete_profile_msg: Complete o seu perfil | ||
update_profile_msg: Atualizar perfil | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,12 @@ | ||
user0 = User.create!(cpf: '004.566.086-72', | ||
email: '[email protected]', | ||
password: '123456') | ||
user1 = User.create!(cpf: '526.866.980-04', | ||
email: '[email protected]', | ||
password: '999999') | ||
user2 = User.create!(cpf: '771.059.370-41', | ||
email: '[email protected]', | ||
password: '654321') | ||
user3 = User.create!(cpf: '913.951.070-09', | ||
email: '[email protected]', | ||
password: 'password') | ||
user4 = User.create!(cpf: '243.089.080-19', | ||
email: '[email protected]', | ||
password: '102030') | ||
ash = FactoryBot.create(:user, email: '[email protected]') | ||
ash = FactoryBot.create(:user, email: '[email protected]', cpf: '837.513.746-47') | ||
FactoryBot.create(:profile, user: ash, first_name: 'Ash', last_name: 'Ketchum', | ||
work_experience: 'Treinador Pokemon', education: 'Escola Pokemon') | ||
brock = FactoryBot.create(:user, email: '[email protected]', cpf: '000.000.001-91') | ||
FactoryBot.create(:profile, user: brock, first_name: 'Brock', last_name: '', | ||
work_experience: 'Treinador de Pokemon de rocha', education: 'Escola Pokemon') | ||
misty = FactoryBot.create(:user, email: '[email protected]', cpf: '293.912.970-30') | ||
FactoryBot.create(:profile, user: misty, first_name: '', last_name: '', | ||
work_experience: '', education: '') | ||
|
||
FactoryBot.create(:project, user: ash) | ||
FactoryBot.create(:project, user: brock, title: 'Líder de Ginásio', description: 'Me tornar líder do estádio de pedra.', category: 'Auto Ajuda') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,8 @@ | ||
FactoryBot.define do | ||
factory :profile do | ||
user { nil } | ||
first_name { 'MyString' } | ||
last_name { 'MyString' } | ||
work_experience { 'MyText' } | ||
education { 'MyText' } | ||
first_name { 'João' } | ||
last_name { 'Neves' } | ||
work_experience { 'Programador' } | ||
education { 'Ensino Médio' } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,72 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe Profile, type: :model do | ||
pending "add some examples to (or delete) #{__FILE__}" | ||
context '#first_update?' do | ||
it 'retorna true quando nenhum campo foi preenchido' do | ||
user = create(:user) | ||
user.profile.update(first_name: '', last_name: '', work_experience: '', | ||
education: '') | ||
|
||
expect(user.profile.first_update?).to be true | ||
end | ||
|
||
it 'retorna falso com pelo menos o nome preenchido' do | ||
user = create(:user) | ||
user.profile.update(first_name: 'Carlos', last_name: '', work_experience: '', | ||
education: '') | ||
|
||
expect(user.profile.first_update?).to be false | ||
end | ||
|
||
it 'retorna falso com pelo menos o sobrenome preenchido' do | ||
user = create(:user) | ||
user.profile.update(first_name: '', last_name: 'Andrade', work_experience: '', | ||
education: '') | ||
|
||
expect(user.profile.first_update?).to be false | ||
end | ||
|
||
it 'retorna falso com pelo menos a experiência de profissional preenchida' do | ||
user = create(:user) | ||
user.profile.update(first_name: '', last_name: '', | ||
work_experience: 'Consultor financeiro', education: '') | ||
|
||
expect(user.profile.first_update?).to be false | ||
end | ||
|
||
it 'retorna falso com pelo menos a informação acadêmica preenchido' do | ||
user = create(:user) | ||
user.profile.update(first_name: '', last_name: '', | ||
work_experience: '', education: 'Engenharia civil') | ||
|
||
expect(user.profile.first_update?).to be false | ||
end | ||
end | ||
|
||
context '#full_name' do | ||
context 'retorna uma string' do | ||
it 'com nome e sobrenome' do | ||
user = create(:user) | ||
user.profile.update(first_name: 'Carlos', last_name: 'Andrade') | ||
|
||
expect(user.profile.full_name).to eq 'Carlos Andrade' | ||
end | ||
|
||
it 'com apenas nome' do | ||
user = create(:user) | ||
user.profile.update(first_name: 'Carlos', last_name: '') | ||
|
||
expect(user.profile.full_name).not_to eq 'Carlos ' | ||
expect(user.profile.full_name).to eq 'Carlos' | ||
end | ||
|
||
it 'com apenas sobrenome' do | ||
user = create(:user) | ||
user.profile.update(first_name: '', last_name: 'Pereira') | ||
|
||
expect(user.profile.full_name).not_to eq ' Pereira' | ||
expect(user.profile.full_name).to eq 'Pereira' | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
require 'rails_helper' | ||
|
||
describe 'Usuário edita perfil' do | ||
it 'de outro usuário sem sucesso' do | ||
user = create(:user, email: '[email protected]', cpf: '787.077.980-67') | ||
user.profile.update(first_name: 'Pedro') | ||
other_user = create(:user, email: '[email protected]', cpf: '047.813.770-25') | ||
other_user.profile.update(first_name: 'Leandro') | ||
|
||
login_as user | ||
patch profile_path other_user.profile, params: { profile: { first_name: 'Fabricio' } } | ||
|
||
expect(other_user.profile.reload.first_name).to eq 'Leandro' | ||
expect(response).to redirect_to root_path | ||
end | ||
|
||
it 'com sucesso' do | ||
user = create(:user) | ||
user.profile.update(first_name: 'Pedro') | ||
|
||
login_as user | ||
patch profile_path user.profile, params: { profile: { first_name: 'Jonas' } } | ||
|
||
expect(user.profile.reload.first_name).to eq 'Jonas' | ||
expect(response).to redirect_to profile_path user.profile | ||
end | ||
|
||
it 'e não está autenticado' do | ||
user = create(:user) | ||
user.profile.update(first_name: 'Pedro') | ||
|
||
patch profile_path user.profile, params: { profile: { first_name: 'Jonas' } } | ||
|
||
expect(user.profile.reload.first_name).not_to eq 'Jonas' | ||
expect(response).to redirect_to new_user_session_path | ||
end | ||
end |
Oops, something went wrong.