-
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 branch 'main' into feat/user-account
- Loading branch information
Showing
22 changed files
with
289 additions
and
16 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 |
---|---|---|
@@ -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 |
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,4 @@ | ||
class JobCategory < ApplicationRecord | ||
validates :name, presence: true | ||
validates :name, uniqueness: { case_sensitive: false } | ||
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 |
---|---|---|
@@ -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> | ||
|
||
|
||
|
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,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> |
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,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. |
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,7 @@ | ||
pt-BR: | ||
activerecord: | ||
models: | ||
job_category: 'Categoria de Trabalho' | ||
attributes: | ||
job_category: | ||
name: 'Nome' |
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,3 @@ | ||
pt-BR: | ||
notices: | ||
job_category_created: Categoria de Trabalho criada com sucesso! |
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,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 |
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,5 @@ | ||
class AddRoleToUser < ActiveRecord::Migration[7.1] | ||
def change | ||
add_column :users, :role, :integer, default: 0 | ||
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,9 @@ | ||
class CreateJobCategories < ActiveRecord::Migration[7.1] | ||
def change | ||
create_table :job_categories do |t| | ||
t.string :name | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
5 changes: 5 additions & 0 deletions
5
db/migrate/20240118054350_add_unique_constraint_to_job_category_name.rb
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,5 @@ | ||
class AddUniqueConstraintToJobCategoryName < ActiveRecord::Migration[7.1] | ||
def change | ||
add_index :job_categories, :name, unique: true | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,5 @@ | ||
FactoryBot.define do | ||
factory :job_category do | ||
name { 'Web Design' } | ||
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 |
---|---|---|
|
@@ -4,5 +4,10 @@ | |
citizen_id_number { '92767398078' } | ||
email { '[email protected]' } | ||
password { '123456' } | ||
role { :user } | ||
|
||
trait :admin do | ||
role { :admin } | ||
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,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 |
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,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 |
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,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 |
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,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 |