Skip to content

Commit

Permalink
Merge pull request #16 from TreinaDev/feature/tarefas
Browse files Browse the repository at this point in the history
Feature/tarefas
  • Loading branch information
Luckvc authored Jan 23, 2024
2 parents eac2cda + 0252b63 commit 585f117
Show file tree
Hide file tree
Showing 26 changed files with 629 additions and 8 deletions.
80 changes: 80 additions & 0 deletions app/controllers/tasks_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
class TasksController < ApplicationController
before_action :authenticate_user!, only: %i[new create show index edit update start finish cancel]
before_action :set_project, only: %i[new create index]
before_action :set_contributors, only: %i[new create edit update]
before_action :set_task, only: %i[show edit update start finish cancel]
before_action :check_user, only: %i[edit update]

def index
@tasks = @project.tasks
end

def new
@task = @project.tasks.build
end

def create
@task = @project.tasks.build(task_params)
@task.author_id = current_user.id

if @task.save
redirect_to task_path(@task), notice: t('.success')
else
flash.now[:alert] = t('.fail')
render :new, status: :unprocessable_entity
end
end

def show; end

def edit; end

def update
if @task.update(task_params)
redirect_to task_path(@task), notice: t('.success')
else
flash.now[:alert] = t('.fail')
render :edit, status: :unprocessable_entity
end
end

def start
@task.in_progress!
redirect_to task_path, notice: t('.success')
end

def finish
@task.finished!
redirect_to task_path, notice: t('.success')
end

def cancel
@task.cancelled!
redirect_to task_path, notice: t('.success')
end

private

def set_project
@project = Project.find(params[:project_id])
end

def set_contributors
# TODO
# Limitar para os colaboradores do projeto
@contributors = User.all
end

def set_task
@task = Task.find(params[:id])
end

def task_params
params.require(:task).permit(:title, :description, :due_date, :assigned_id)
end

def check_user
redirect_to root_path, alert: t('.fail') \
unless @task.author == current_user || @task.assigned == current_user || @task.project.user == current_user
end
end
1 change: 1 addition & 0 deletions app/models/project.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class Project < ApplicationRecord
belongs_to :user
has_many :user_roles, dependent: :destroy
has_many :tasks, dependent: :destroy

validates :title, :description, :category, presence: true
end
17 changes: 17 additions & 0 deletions app/models/task.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Task < ApplicationRecord
belongs_to :project
belongs_to :author, class_name: 'User'
belongs_to :assigned, class_name: 'User', optional: true

enum status: { uninitialized: 0, in_progress: 3, finished: 5, expired: 7, cancelled: 9 }

validates :title, presence: true

validate :due_date_is_future

private

def due_date_is_future
errors.add(:due_date, ' deve ser futuro.') if due_date.present? && due_date < Time.zone.today.to_date
end
end
2 changes: 2 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ class User < ApplicationRecord
has_one :profile, dependent: :destroy
has_many :projects, dependent: :destroy
has_many :user_roles, dependent: :destroy
has_many :authored_tasks, foreign_key: 'author_id', dependent: :nullify, inverse_of: :task
has_many :assigned_tasks, foreign_key: 'assigned_id', dependent: :nullify, inverse_of: :task

validates :cpf, presence: true
validates :cpf, uniqueness: true
Expand Down
1 change: 1 addition & 0 deletions app/views/projects/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@

<div>
<%= link_to t(:edit_project_btn), edit_project_path(@project) %>
<%= link_to Task.model_name.human.pluralize, project_tasks_path(@project) %>
</div>
32 changes: 32 additions & 0 deletions app/views/tasks/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<%= form_with(model: [@project, @task]) do |f| %>
<% if @task.errors.any? %>
<div class='text-danger'>
<h3><%= I18n.t('.errors_title') %>:</h3>
<ul>
<% @task.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="mb-3">
<%= f.label :title, class: 'form-label' %>
<%= f.text_field :title, class: 'form-control' %>
</div>
<div class="mb-3">
<%= f.label :description, class: 'form-label' %>
<%= f.text_area :description, class: 'form-control' %>
</div>
<div class="mb-3">
<%= f.label :due_date, class: 'form-label' %>
<%= f.date_field :due_date, value: f.object.try(:strftime,"%d/%m/%Y"), class: 'form-control' %>
</div>
<div class="mb-3">
<%= f.label :assigned_id, class: 'form-label' %>
<%= f.collection_select :assigned_id, @contributors, :id, :email, :include_blank => true, class: 'form-control' %>
</div>
<div>
<%= f.submit I18n.t('tasks.save'), class: 'btn btn-primary'%>
</div>
<% end %>
3 changes: 3 additions & 0 deletions app/views/tasks/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<h1><%= I18n.t('tasks.edit_task')%></h1>

<%= render 'form' %>
11 changes: 11 additions & 0 deletions app/views/tasks/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<h1><%= Task.model_name.human.pluralize %></h1>

<%= link_to I18n.t('tasks.new_task'), new_project_task_path(@project) %>
<br>
<br>
<ul>
<% @tasks.each do |task| %>
<li><h4><%= link_to task.title, task_path(task) %>
<span style="font-size: small;"><%= I18n.l task.due_date if task.due_date %></span></h4></li>
<% end %>
</ul>
3 changes: 3 additions & 0 deletions app/views/tasks/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<h1><%= I18n.t('tasks.create_task')%></h1>

<%= render 'form' %>
37 changes: 37 additions & 0 deletions app/views/tasks/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<h1><%= Task.model_name.human %>: <%= @task.title %></h1>

<dt><%= Task.human_attribute_name :description %></dt>
<dd><%= @task.description %></dd>

<dt><%= Task.human_attribute_name :author %></dt>
<dd><%= @task.author.email %></dd>

<dt><%= Task.human_attribute_name :status %></dt>
<dd><%= I18n.t(@task.status) %></dd>

<dt><%= Task.human_attribute_name :assigned %></dt>
<% if @task.assigned %>
<dd><%= @task.assigned.email %></dd>
<% else %>
<dd><%= I18n.t('tasks.no_assigned') %></dd>
<% end %>

<dt><%= Task.human_attribute_name :due_date %></dt>
<% if @task.due_date %>
<dd><%= I18n.l @task.due_date %></dd>
<% else %>
<dd><%= I18n.t('tasks.no_due_date') %></dd>
<% end %>

<br>
<%= button_to I18n.t('tasks.start_task'), start_task_path(@task), class: 'btn btn-primary' if @task.uninitialized? %>

<%= button_to I18n.t('tasks.finish_task'), finish_task_path(@task), class: 'btn btn-primary' if @task.in_progress? %>
<br>

<%= button_to I18n.t('tasks.cancel_task'), cancel_task_path(@task), class: 'btn btn-danger' if @task.uninitialized? || @task.in_progress? %>
<br>

<%= link_to I18n.t('tasks.edit_task'), edit_task_path(@task) %>

<%= link_to I18n.t('.back'), project_tasks_path(@task.project) %>
3 changes: 3 additions & 0 deletions config/locales/general.pt-BR.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pt-BR:
errors_title: Erros
back: Voltar
46 changes: 46 additions & 0 deletions config/locales/tasks.pt-BR.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
pt-BR:
uninitialized: Não iniciada
in_progress: Em andamento
finished: Finalizada
expired: Expirada
cancelled: Cancelada
activerecord:
models:
task:
one: Tarefa
other: Tarefas
attributes:
task:
title: Título
description: Descrição
due_date: Prazo
project_id: Projeto
author_id: Autor
author: Autor
assigned_id: Responsável
assigned: Responsável
status: Status
tasks:
create:
success: Tarefa criada com sucesso.
fail: Não foi possível criar a tarefa.
update:
success: Tarefa editada com sucesso.
fail: Não foi possível editar a tarefa.
start:
success: Tarefa iniciada.
finish:
success: Tarefa finalizada.
cancel:
success: Tarefa cancelada.
check_user:
fails: 'Você não possui acesso a esta tarefa.'
edit_task: Editar Tarefa
start_task: Iniciar Tarefa
finish_task: Finalizar Tarefa
cancel_task: Cancelar Tarefa
new_task: Nova Tarefa
create_task: Criar Tarefa
no_due_date: Sem prazo
no_assigned: Sem responsável
save: Salvar
9 changes: 9 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@

resources :profiles, only: %i[edit update show]
resources :projects, only: %i[new create show index edit destroy] do
resources :tasks, only: %i[index new create]
get 'my_projects', on: :collection
end

resources :tasks, only: %i[show edit update] do
member do
post 'start'
post 'finish'
post 'cancel'
end
end
end
12 changes: 12 additions & 0 deletions db/migrate/20240118131937_create_tasks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CreateTasks < ActiveRecord::Migration[7.1]
def change
create_table :tasks do |t|
t.string :title, null: false
t.text :description
t.references :project, null: false, foreign_key: true
t.date :due_date

t.timestamps
end
end
end
6 changes: 6 additions & 0 deletions db/migrate/20240118133253_add_author_and_assigned_to_tasks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddAuthorAndAssignedToTasks < ActiveRecord::Migration[7.1]
def change
add_reference :tasks, :author, foreign_key: { to_table: :users }
add_reference :tasks, :assigned, foreign_key: { to_table: :users }
end
end
5 changes: 5 additions & 0 deletions db/migrate/20240122115325_add_status_to_task.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddStatusToTask < ActiveRecord::Migration[7.1]
def change
add_column :tasks, :status, :integer, default: 0
end
end
20 changes: 19 additions & 1 deletion db/schema.rb

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

29 changes: 23 additions & 6 deletions db/seeds.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
ash = FactoryBot.create(:user, email: '[email protected]', cpf: '837.513.746-47')
FactoryBot.create(:profile, user: ash, first_name: 'Ash', last_name: 'Ketchum',
ash.profile.update(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')
brock.profile.update(first_name: 'Brock', last_name: 'Harrison',
work_experience: 'Treinador 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: '')
misty.profile.update(first_name: 'Mysty', last_name: '',
work_experience: 'Treinadora Pokemon de agua', education: 'Escola Pokemon')


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')
pokemon_project = FactoryBot.create(:project, user: brock, title: 'Líder de Ginásio',
description: 'Me tornar líder do estádio de pedra.',
category: 'Auto Ajuda')

FactoryBot.create(:task, project: pokemon_project, title:'Pegar um geodude',
description:'Para completar o meu time de pedra, preciso de um geodude, vamos captura-lo',
assigned: brock, due_date: 2.months.from_now.to_date, author: brock)

FactoryBot.create(:task, project: pokemon_project, title:'Parar a equipe rocket',
description:'A equipe rocket está aprontando novamente, temos que para-los',
assigned: ash, due_date: Time.zone.tomorrow, author: ash)

FactoryBot.create(:task, project: pokemon_project, title:'Derrotar um Charmander',
description: 'Lutar contra outro treinador com um Charmander.',
assigned: ash, due_date: 1.week.from_now, author: ash)
10 changes: 10 additions & 0 deletions spec/factories/tasks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FactoryBot.define do
factory :task do
title { 'Bugfix' }
description { 'Fix de um bug' }
project
author { User.last }
assigned { User.last }
due_date { 10.days.from_now.to_date }
end
end
Loading

0 comments on commit 585f117

Please sign in to comment.