Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/perfil do usuario #25

Merged
merged 13 commits into from
Jan 22, 2024
25 changes: 25 additions & 0 deletions app/controllers/profile_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class ProfileController < ApplicationController
before_action :authenticate_user!
before_action :set_profile, only: %i[edit update show]

def edit; end

def update
redirect_to user_profile_path if @profile.update(profile_params)
end

def show; end

private

def profile_params
personal_info_attributes = %i[street city state
area phone zip_code visibility
street_number birth_date]
params.require(:profile).permit :cover_letter, personal_info_attributes:
end

def set_profile
@profile = current_user.profile
end
end
4 changes: 4 additions & 0 deletions app/models/personal_info.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class PersonalInfo < ApplicationRecord
belongs_to :profile
has_one :user, through: :profile
end
5 changes: 5 additions & 0 deletions app/models/profile.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
class Profile < ApplicationRecord
belongs_to :user
has_one :personal_info, dependent: :destroy

accepts_nested_attributes_for :personal_info

after_create :create_personal_info!
end
5 changes: 3 additions & 2 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ class User < ApplicationRecord
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
has_one :profile, dependent: :destroy
has_one :personal_info, through: :profile
has_many :posts, dependent: :destroy

enum role: { user: 0, admin: 10 }

validates :full_name, :citizen_id_number, presence: true
validates :citizen_id_number, uniqueness: true
validate :validate_citizen_id_number

after_create :'create_profile!'

enum role: { user: 0, admin: 10 }

def self.search_by_full_name(query)
where('full_name LIKE ?',
"%#{sanitize_sql_like(query)}%")
Expand Down
19 changes: 11 additions & 8 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,19 @@
<header class="container mt-3">
<%= render 'shared/navbar' %>
</header>
<main class="container">
<div class="flash-messages">
<p style="color: red">
<%= flash[:alert] %>
</p>
<p style="color: green">
<div class="container mt-3">
<% if alert %>
<div class="alert alert-warning" role="alert">
<%= alert %>
</div>
<% end %>
<% if notice %>
<div class="alert alert-info" role="alert">
<%= notice %>
</p>
</div>
</div>
<% end %>
<%= yield %>
</main>
</body>

</html>
47 changes: 47 additions & 0 deletions app/views/profile/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@

<%= form_with model: @profile, url: user_profile_path, method: :patch do |form| %>
<%= form.fields_for :personal_info do |personal_info| %>
<p>
<%= personal_info.label :street %>
<%= personal_info.text_field :street %>
</p>
<p>
<%= personal_info.label :street_number %>
<%= personal_info.text_field :street_number %>
</p>
<p>
<%= personal_info.label :area %>
<%= personal_info.text_field :area %>
</p>
<p>
<%= personal_info.label :city %>
<%= personal_info.text_field :city %>
</p>
<p>
<%= personal_info.label :state %>
<%= personal_info.text_field :state %>
</p>
<p>
<%= personal_info.label :zip_code %>
<%= personal_info.text_field :zip_code %>
</p>
<p>
<%= personal_info.label :phone %>
<%= personal_info.text_field :phone %>
</p>
<p>
<%= personal_info.label :birth_date %>
<%= personal_info.date_field :birth_date %>
</p>
<p>
<%= personal_info.label :visibility %>
<%= personal_info.check_box :visibility %>
</p>
<% end %>

<%= form.label :cover_letter %>
<%= form.text_area :cover_letter %>


<%= form.submit 'Salvar' %>
<% end %>
19 changes: 19 additions & 0 deletions app/views/profile/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<%= link_to 'Editar Informações Pessoais', edit_user_profile_path %>

<div>
<h2><%= @profile.user.full_name %></h2>
<p>Email: <%= @profile.user.email %></p>
<p>Carta de Apresentação: <%= @profile.cover_letter %></p>

<h3>Informações Pessoais</h3>
<ul>
<li>Endereço: <%= @profile.user.personal_info.street %></li>
<li>Área: <%= @profile.user.personal_info.area %></li>
<li>CEP: <%= @profile.user.personal_info.zip_code %></li>
<li>Cidade: <%= @profile.user.personal_info.city %></li>
<li>Estado: <%= @profile.user.personal_info.state %></li>
<li>Telefone: <%= @profile.user.personal_info.phone %></li>
<li>Data de Nascimento: <%= @profile.user.personal_info&.birth_date&.strftime("%d/%m/%Y") %></li>
<li>Visível: <%= t(@profile.user.personal_info.visibility) %></li>
</ul>
</div>
48 changes: 29 additions & 19 deletions app/views/shared/_navbar.html.erb
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
<nav>
<ul>
<li><%= link_to 'Portfoliorrr', root_path %></li>

<% if user_signed_in? && current_user.admin? %>
<li><%= link_to 'Categorias de trabalho', job_categories_path %></li>
<% end %>

<% if user_signed_in? %>
<%= button_to "Sair", destroy_user_session_path, method: :delete %>
<% else %>
<li>
<%= link_to 'Entrar', new_user_session_path, 'data-turbo': 'false' %>
</li>
<li>
<%= link_to 'Cadastrar Usuário', new_user_registration_path, 'data-turbo': 'false' %>
</li>
<% end %>
</ul>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container-fluid">
<%= link_to 'Portfoliorrr', root_path, class: 'navbar-brand' %>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<% if user_signed_in? %>
<% if current_user.admin? %>
<li class="nav-item">
<%= link_to 'Categorias de trabalho', job_categories_path, class: 'nav-link' %>
</li>
<% else %>
<li class="nav-item">
<%= link_to 'Meu Perfil', user_profile_path, class: 'nav-link' %>
</li>
<% end %>
<li class="nav-item">
<%= button_to 'Sair', destroy_user_session_path, method: :delete, class: 'btn btn-danger nav-link' %>
</li>
<% else %>
<li class="nav-item">
<%= link_to 'Entrar', new_user_session_path, class: 'nav-link', 'data-turbo': 'false' %>
</li>
<% end %>
</ul>
</div>
</div>
</nav>

<% if user_signed_in? %>
Expand Down
15 changes: 15 additions & 0 deletions config/locales/personal_info.pt-BR.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
pt-BR:
activerecord:
models:
personal_info: 'Informações Pessoais'
attributes:
personal_info:
street: 'Rua'
city: 'Cidade'
state: 'Estado'
area: 'Bairro'
phone: 'Telefone'
zip_code: 'CEP'
street_number: 'Número'
birth_date: 'Data de Nascimento'
visibility: 'Visível'
1 change: 0 additions & 1 deletion config/locales/profile.pt-BR.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
---
pt-BR:
activerecord:
models:
Expand Down
3 changes: 3 additions & 0 deletions config/locales/pt-BR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ pt-BR:
sign up: 'Cadastrar'
log in: 'Entrar'
log out: 'Sair'
login_required: 'Você não está logado'
true: 'Sim'
false: 'Não'

1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
end

resources :job_categories, only: %i[index create]
resource :profile, only: %i[edit update show], controller: :profile, as: :user_profile
end
18 changes: 18 additions & 0 deletions db/migrate/20240119180251_create_personal_infos.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class CreatePersonalInfos < ActiveRecord::Migration[7.1]
def change
create_table :personal_infos do |t|
t.references :profile, null: false, foreign_key: true
t.string :street
t.string :city
t.string :state
t.string :phone
t.string :area
t.boolean :visibility
t.date :birth_date
t.string :zip_code
t.string :street_number

t.timestamps
end
end
end
17 changes: 17 additions & 0 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions spec/factories/personal_infos.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FactoryBot.define do
factory :personal_info do
profile
street { 'Avenida Campus Code' }
street_number { '1200' }
area { 'TreinaDev' }
city { 'São Paulo' }
zip_code { '36200123' }
state { 'SP' }
phone { '11999991234' }
birth_date { '1980-12-25' }
visibility { false }
end
end
4 changes: 4 additions & 0 deletions spec/models/personal_info_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require 'rails_helper'

RSpec.describe PersonalInfo, type: :model do
end
rozbr96 marked this conversation as resolved.
Show resolved Hide resolved
8 changes: 8 additions & 0 deletions spec/models/profile_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
require 'rails_helper'

RSpec.describe Profile, type: :model do
describe '#create_personal_info' do
it 'cria as informações pessoais ao criar um perfil' do
user = User.create(email: 'joaoalmeida@email', citizen_id_number: '38031825068',
password: '123456', full_name: 'João Almeida')

expect(user.profile.personal_info).to be_present
end
end
end
9 changes: 9 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,13 @@
end
end
end

describe '#create_profile' do
it 'cria um perfil após criação de usuário' do
user = User.create(email: 'joaoalmeida@email', citizen_id_number: '38031825068',
password: '123456', full_name: 'João Almeida')

expect(user.profile).to be_present
end
end
end
57 changes: 57 additions & 0 deletions spec/system/user_edits_personal_info_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
require 'rails_helper'

describe 'Usuário edita informações pessoais' do
context 'quando logado' do
it 'a partir da home' do
personal_info = create(:personal_info)

login_as personal_info.profile.user
visit root_path
click_on 'Meu Perfil'
click_on 'Editar Informações Pessoais'
expect(current_path).to eq edit_user_profile_path
end

it 'com sucesso' do
rozbr96 marked this conversation as resolved.
Show resolved Hide resolved
personal_info = create(:personal_info)
login_as personal_info.profile.user

visit edit_user_profile_path

fill_in 'Resumo Profissional', with: 'Eu estou tentando ser um dev melhor...'

fill_in 'Rua', with: 'Avenida Campus Code'
fill_in 'Número', with: '1230'
fill_in 'Bairro', with: 'TreinaDev'
fill_in 'Cidade', with: 'São Paulo'
fill_in 'Estado', with: 'SP'
fill_in 'CEP', with: '34123069'
fill_in 'Telefone', with: '11 4002 8922'
fill_in 'Data de Nascimento', with: '1980-12-25'
check 'Visível'

click_on 'Salvar'

expect(current_path).to eq user_profile_path
expect(page).to have_content 'Eu estou tentando ser um dev melhor...'
expect(page).to have_content 'Avenida Campus Code'
expect(page).to have_content '1230'
expect(page).to have_content 'TreinaDev'
expect(page).to have_content 'São Paulo'
expect(page).to have_content 'SP'
expect(page).to have_content '34123069'
expect(page).to have_content '11 4002 8922'
expect(page).to have_content '25/12/1980'
expect(page).to have_content 'Visível: Sim'
end
end

context 'quando não logado' do
it 'e é redirecionado para a tela de login' do
visit edit_user_profile_path

expect(current_path).to eq new_user_session_path
expect(page).to have_content 'Para continuar, faça login ou registre-se'
end
end
end
Loading