Skip to content

Commit

Permalink
Merge branch 'main' into feat/user-account
Browse files Browse the repository at this point in the history
  • Loading branch information
rozbr96 committed Jan 19, 2024
2 parents c43abfc + 3deffb6 commit 6d93c4f
Show file tree
Hide file tree
Showing 22 changed files with 289 additions and 16 deletions.
28 changes: 28 additions & 0 deletions app/controllers/job_categories_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class JobCategoriesController < ApplicationController
before_action :authenticate_user!, :authorize!

def index
@job_category = JobCategory.new
@job_categories = JobCategory.all
end

def create
job_category_params = params.require(:job_category).permit(:name)
@job_category = JobCategory.new(job_category_params)
if @job_category.save
redirect_to job_categories_path, notice: t('notices.job_category_created')
else
@job_categories = JobCategory.all
flash.now[:alert] = t('alerts.job_category_fail')
render 'index', status: :internal_server_error
end
end

private

def authorize!
return if current_user.admin?

redirect_to root_path, alert: t('alerts.unauthorized')
end
end
4 changes: 4 additions & 0 deletions app/models/job_category.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class JobCategory < ApplicationRecord
validates :name, presence: true
validates :name, uniqueness: { case_sensitive: false }
end
1 change: 1 addition & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ class User < ApplicationRecord
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 }
end
33 changes: 33 additions & 0 deletions app/views/job_categories/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<h1>Cadastro de categorias de trabalho</h1>

<% if @job_category.errors.any? %>
<ul>
<% @job_category.errors.each do |error| %>
<li class="text-danger"><%= error.full_message %></li>
<% end %>
</ul>
<% end %>
<%= form_with model: @job_category do |f| %>
<%= f.label :name, 'Nome da categoria', class: "form-label" %>
<div class="row">
<div class="col-md-4">
<%= f.text_field :name, class: "form-control", placeholder: "Ex: Web Design, Modelagem 3D" %>
</div>
<div class="col">
<%= f.submit 'Criar', class: "btn btn-primary" %>
</div>
</div>
<% end %>

<div class="mt-5">
<h2>Categorias de trabalho cadastradas</h2>
<ul>
<% @job_categories.each do |category| %>
<li><%= category.name %></li>
<% end %>
</ul>
</div>



13 changes: 3 additions & 10 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,14 @@
</head>

<body>
<header>
<%= render 'shared/navbar' %>
</header>
<div class="container">
<nav>
<ul>
<% 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>

<div>
<%= alert %>
<%= notice %>
</div>

<%= yield %>
</div>
</body>
Expand Down
14 changes: 14 additions & 0 deletions app/views/shared/_navbar.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<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>
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
4 changes: 4 additions & 0 deletions config/locales/alerts.pt-BR.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pt-BR:
alerts:
unauthorized: 'Você não têm permissão para realizar essa ação.'
job_category_fail: Não foi possível cadastrar Categoria de Trabalho.
7 changes: 7 additions & 0 deletions config/locales/job_category.pt-BR.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pt-BR:
activerecord:
models:
job_category: 'Categoria de Trabalho'
attributes:
job_category:
name: 'Nome'
3 changes: 3 additions & 0 deletions config/locales/notices.pt-BR.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pt-BR:
notices:
job_category_created: Categoria de Trabalho criada com sucesso!
5 changes: 2 additions & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
Rails.application.routes.draw do
devise_for :users
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

# Defines the root path route ("/")
root to: "home#index"

resources :job_categories, only: %i[index create]
end
5 changes: 5 additions & 0 deletions db/migrate/20240117131132_add_role_to_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddRoleToUser < ActiveRecord::Migration[7.1]
def change
add_column :users, :role, :integer, default: 0
end
end
9 changes: 9 additions & 0 deletions db/migrate/20240117135059_create_job_categories.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreateJobCategories < ActiveRecord::Migration[7.1]
def change
create_table :job_categories do |t|
t.string :name

t.timestamps
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddUniqueConstraintToJobCategoryName < ActiveRecord::Migration[7.1]
def change
add_index :job_categories, :name, unique: true
end
end
24 changes: 23 additions & 1 deletion db/schema.rb

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

5 changes: 5 additions & 0 deletions spec/factories/job_categories.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FactoryBot.define do
factory :job_category do
name { 'Web Design' }
end
end
5 changes: 5 additions & 0 deletions spec/factories/users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,10 @@
citizen_id_number { '92767398078' }
email { '[email protected]' }
password { '123456' }
role { :user }

trait :admin do
role { :admin }
end
end
end
20 changes: 20 additions & 0 deletions spec/models/job_category_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require 'rails_helper'

RSpec.describe JobCategory, type: :model do
describe '#valid?' do
it 'nome não pode ficar em branco' do
job_category = build(:job_category, name: '')

expect(job_category).not_to be_valid
expect(job_category.errors[:name]).to include('não pode ficar em branco')
end

it 'nome deve ser único' do
create(:job_category, name: 'Web Design')
job_category = build(:job_category, name: 'web design')

expect(job_category).not_to be_valid
expect(job_category.errors[:name]).to include('já está em uso')
end
end
end
2 changes: 1 addition & 1 deletion spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
add_filter 'channels'
add_filter 'mailers'
end

require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'
require_relative '../config/environment'
Expand All @@ -24,6 +23,7 @@
Capybara.disable_animation = true

RSpec.configure do |config|
include Warden::Test::Helpers
config.use_transactional_fixtures = true

config.before(:each, type: :system) do
Expand Down
34 changes: 34 additions & 0 deletions spec/requests/admin_create_job_category_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require 'rails_helper'

describe 'Usuário cria categoria de trabalho' do
it 'com sucesso' do
admin = create(:user, :admin)

login_as admin
post job_categories_path, params: { job_category: { name: 'Web Design' } }

expect(response).to redirect_to(job_categories_path)
expect(JobCategory.count).to eq(1)
expect(JobCategory.last.name).to eq('Web Design')
expect(flash[:notice]).to eq('Categoria de Trabalho criada com sucesso!')
end

it 'e deve estar logado' do
post job_categories_path, params: { job_category: { name: 'Web Design' } }

expect(response).to redirect_to(new_user_session_path)
expect(JobCategory.count).to eq(0)
expect(flash[:alert]).to eq('Para continuar, faça login ou registre-se.')
end

it 'e deve ser administrador' do
user = create(:user)

login_as user
post job_categories_path, params: { job_category: { name: 'Web Design' } }

expect(response).to redirect_to(root_path)
expect(JobCategory.count).to eq(0)
expect(flash[:alert]).to eq('Você não têm permissão para realizar essa ação.')
end
end
57 changes: 57 additions & 0 deletions spec/system/admin_create_job_category_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
require 'rails_helper'

describe 'Usuário cria categoria de trabalho' do
it 'com sucesso' do
admin = create(:user, :admin)

login_as admin
visit job_categories_path
fill_in 'Nome da categoria', with: 'Web Design'
click_on 'Criar'

expect(page).to have_current_path(job_categories_path)
expect(page).to have_content('Categoria de Trabalho criada com sucesso!')
expect(page).to have_content('Web Design')
end

it 'e deve estar logado' do
visit job_categories_path

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

it 'e deve ser administrador' do
user = create(:user)

login_as user
visit job_categories_path

expect(page).to have_current_path(root_path)
expect(page).to have_content('Você não têm permissão para realizar essa ação.')
end

it 'a partir do menu' do
user = create(:user, :admin)

login_as user
visit root_path
click_on 'Categorias de trabalho'

expect(page).to have_current_path(job_categories_path)
expect(page).to have_content('Cadastro de categorias de trabalho')
expect(page).to have_field('Nome da categoria')
expect(page).to have_button('Criar')
end

it 'com dados faltando' do
admin = create(:user, :admin)

login_as admin
visit job_categories_path
click_on 'Criar'

expect(page).to have_content('Não foi possível cadastrar Categoria de Trabalho.')
expect(page).to have_content('Nome não pode ficar em branco')
end
end
25 changes: 25 additions & 0 deletions spec/system/admin_views_job_category_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'rails_helper'

describe 'Usuário vê categorias de trabalho' do
it 'a partir da home' do
admin = create(:user, :admin)
create(:job_category, name: 'Web Design')
create(:job_category, name: 'Professor')

login_as admin
visit root_path
click_on 'Categorias de trabalho'

expect(page).to have_content 'Web Design'
expect(page).to have_content 'Professor'
end

it 'e não vê o link se não for administrador' do
user = create(:user)

login_as user
visit root_path

expect(page).not_to have_link 'Categorias de trabalho'
end
end

0 comments on commit 6d93c4f

Please sign in to comment.