Skip to content

Commit

Permalink
Merge pull request #15 from TreinaDev/feature/edicao-de-perfil
Browse files Browse the repository at this point in the history
Feature/edicao de perfil
  • Loading branch information
paulohenrique-gh authored Jan 22, 2024
2 parents 644c0c3 + d74fa53 commit b836048
Show file tree
Hide file tree
Showing 21 changed files with 405 additions and 48 deletions.
26 changes: 26 additions & 0 deletions app/controllers/profiles_controller.rb
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
15 changes: 15 additions & 0 deletions app/models/profile.rb
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
7 changes: 4 additions & 3 deletions app/views/layouts/_navbar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
<%= link_to 'Home', root_path, class:"nav-link" %>
<%= link_to 'Criar Projeto', new_project_path, class:"nav-link" %>
<% if user_signed_in? %>
<p><%= current_user.email %></p>
<%= button_to "Sair", destroy_user_session_path, method: :delete %>
<%= link_to 'Meu perfil', profile_path(current_user.profile), class: 'nav-link' %>
<p class="nav-link"><%= current_user.email %></p>
<%= button_to "Sair", destroy_user_session_path, method: :delete, class: 'btn btn-danger' %>
<% else %>
<%= link_to "Entrar", new_user_session_path %>
<%= link_to "Entrar", new_user_session_path, class: 'btn btn-primary' %>
<% end %>
</div>
</div>
Expand Down
1 change: 1 addition & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
</head>

<body>

<div class="container">
<%= render partial: 'layouts/navbar' %>

Expand Down
35 changes: 35 additions & 0 deletions app/views/profiles/_form.html.erb
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 %>
14 changes: 14 additions & 0 deletions app/views/profiles/_profile_info.html.erb
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>
9 changes: 7 additions & 2 deletions app/views/profiles/edit.html.erb
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 } %>
18 changes: 18 additions & 0 deletions app/views/profiles/show.html.erb
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 %>
2 changes: 2 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class Application < Rails::Application
config.time_zone = "Brasilia"
# config.eager_load_paths << Rails.root.join("extras")

config.i18n.default_locale = :'pt-BR'

# Don't generate system test files.
config.generators.system_tests = nil
end
Expand Down
2 changes: 1 addition & 1 deletion config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
config.cache_store = :null_store

# Render exception templates for rescuable exceptions and raise for other exceptions.
config.action_dispatch.show_exceptions = :rescuable
config.action_dispatch.show_exceptions = :none

# Disable request forgery protection in test environment.
config.action_controller.allow_forgery_protection = false
Expand Down
22 changes: 22 additions & 0 deletions config/locales/models/profiles.pt-BR.yml
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

1 change: 1 addition & 0 deletions config/locales/pt-BR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ pt-BR:
create: Criar %{model}
submit: Salvar %{model}
update: Atualizar %{model}
complete: Completar %{model}
number:
currency:
format:
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@

root to: "home#index"

resources :profiles, only: %i[edit]
resources :profiles, only: [:edit, :update, :show]
resources :projects, only: [:new, :create, :show]
end
24 changes: 8 additions & 16 deletions db/seeds.rb
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')
9 changes: 4 additions & 5 deletions spec/factories/profiles.rb
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
69 changes: 68 additions & 1 deletion spec/models/profile_spec.rb
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
37 changes: 37 additions & 0 deletions spec/requests/profiles/user_edit_profile_spec.rb
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
Loading

0 comments on commit b836048

Please sign in to comment.