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
4 changes: 4 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@ 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

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

after_create :'create_profile!'

private

def validate_citizen_id_number
errors.add(:citizen_id_number, 'inválido') unless CPF.valid?(citizen_id_number)
end

enum role: { user: 0, admin: 10 }
rozbr96 marked this conversation as resolved.
Show resolved Hide resolved
end
17 changes: 12 additions & 5 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,19 @@
<header>
<%= render 'shared/navbar' %>
</header>
<div class="container">
<div>
<%= alert %>
<%= notice %>
</div>
<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 %>
</div>
<% end %>
<%= yield %>
</div>
</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>
45 changes: 32 additions & 13 deletions app/views/shared/_navbar.html.erb
Original file line number Diff line number Diff line change
@@ -1,14 +1,33 @@
<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 %>

<% unless user_signed_in? %>
<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>
<li class="nav-item">
<%= link_to 'Cadastrar Usuário', new_user_registration_path, class: 'nav-link', 'data-turbo': 'false' %>
</li>
<% end %>
</ul>
</div>
</div>
</nav>
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'
7 changes: 7 additions & 0 deletions config/locales/profile.pt-BR.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pt-BR:
activerecord:
models:
profile: 'Perfil'
attributes:
profile:
cover_letter: 'Resumo Pessoal'
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 @@ -3,4 +3,5 @@
root to: "home#index"

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
9 changes: 6 additions & 3 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
5 changes: 0 additions & 5 deletions spec/factories/users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,5 @@
citizen_id_number { '92767398078' }
email { '[email protected]' }
password { '123456' }
role { :user }

trait :admin do
role { :admin }
end
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
7 changes: 7 additions & 0 deletions spec/models/profile_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
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
profile = create(:profile)

expect(profile.personal_info).to be_present
end
rozbr96 marked this conversation as resolved.
Show resolved Hide resolved
end
end
8 changes: 8 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,12 @@
end
end
end

describe '#create_profile' do
it 'cria um perfil após criação de usuário' do
user = create(:user)

expect(user.profile).to be_present
rozbr96 marked this conversation as resolved.
Show resolved Hide resolved
end
end
end
Loading