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

Feature/adiciona user role a tarefa #80

Merged
merged 19 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
36cdf06
WIP: altera model task trocando author por user_role
decoporteira Feb 5, 2024
1cd7c33
Adiciona testes para que o lider de projeto possa editar tarefas e co…
decoporteira Feb 6, 2024
4c84737
Adiciona validação se o usuário é admin de um projeto e adiciona test…
decoporteira Feb 6, 2024
770fa9d
Merge branch 'main' of github.com:TreinaDev/td11-cola-bora into featu…
decoporteira Feb 6, 2024
a02a1f0
Refatora validação para o admin editar tarefas
decoporteira Feb 6, 2024
5b05fc8
Remove rescue_from ActiveRecord::RecordNotFound de ApplicationControl…
decoporteira Feb 6, 2024
eb85aab
Remove função self.get_user_role(project, user) que foi substituída p…
decoporteira Feb 6, 2024
980020a
Remove função self.get_user_role(project, user) que foi substituída p…
decoporteira Feb 6, 2024
5d492a1
Merge branch 'feature/adiciona-user-role-a-tarefa' of github.com:Trei…
decoporteira Feb 6, 2024
36e5ca4
Altera relação entre User > Task e User > Metting
decoporteira Feb 7, 2024
8ebb723
Altera Factory Task e User_Role e refatora testes.
decoporteira Feb 7, 2024
561b4ea
Refatora seeds.rb
decoporteira Feb 7, 2024
bfb7033
Merge branch 'main' of github.com:TreinaDev/td11-cola-bora into featu…
decoporteira Feb 7, 2024
68dd1bd
Refatora testes do Calendário.
decoporteira Feb 7, 2024
b702be5
Apaga arquivo de teste inutilizado.
decoporteira Feb 7, 2024
02accf6
Refator testes de project_spec.rb
decoporteira Feb 7, 2024
9b9fa95
Alterar rotas de Post para Patch dentro do resources de Tasks
decoporteira Feb 7, 2024
f8af6b3
Apaga método autenticate_user de Tasks_Controller e renomeia método c…
decoporteira Feb 7, 2024
5ad2a74
Refatora action create de Tasks e altera títulos de testes de edição …
decoporteira Feb 7, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ class ApplicationController < ActionController::Base
before_action :authenticate_user!
before_action :set_beginning_of_week

rescue_from ActiveRecord::RecordNotFound, with: :not_found

protected

def set_beginning_of_week
Expand All @@ -15,10 +13,6 @@ def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:cpf])
end

def not_found
redirect_to root_path, alert: t('.not_found')
end

def after_sign_in_path_for(resource)
stored_location_for(resource) || projects_path
end
Expand Down
35 changes: 16 additions & 19 deletions app/controllers/tasks_controller.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
class TasksController < ApplicationController
before_action :set_project, only: %i[new create index]
before_action :set_contributors, only: %i[new create edit update]
before_action :set_project
before_action :set_task, only: %i[show edit update start finish cancel]
before_action :check_user, only: %i[edit update]
before_action :check_contributor
before_action :can_edit_task, only: %i[edit update]

def index
@tasks = @project.tasks
end

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

def create
@task = @project.tasks.build(task_params)
@task.author_id = current_user.id
@task.user_role = UserRole.find_by(user: current_user, project: @project)

if @task.save
redirect_to task_path(@task), notice: t('.success')
redirect_to [@project, @task], notice: t('.success')
adoniranfranceh marked this conversation as resolved.
Show resolved Hide resolved
else
flash.now[:alert] = t('.fail')
render :new, status: :unprocessable_entity
Expand All @@ -30,7 +30,7 @@ def edit; end

def update
if @task.update(task_params)
redirect_to task_path(@task), notice: t('.success')
redirect_to [@project, @task], notice: t('.success')
else
flash.now[:alert] = t('.fail')
render :edit, status: :unprocessable_entity
Expand All @@ -39,17 +39,17 @@ def update

def start
@task.in_progress!
redirect_to task_path, notice: t('.success')
redirect_to project_task_path(@project), notice: t('.success')
decoporteira marked this conversation as resolved.
Show resolved Hide resolved
end

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

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

private
Expand All @@ -58,12 +58,6 @@ 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
Expand All @@ -72,8 +66,11 @@ 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
def check_contributor
redirect_to root_path, alert: t('.not_contributor') unless @project.member?(current_user)
end

def can_edit_task
redirect_to project_task_path(@project, @task), alert: t('.fail') unless current_user.can_edit?(@task)
end
end
1 change: 1 addition & 0 deletions app/models/meeting.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class Meeting < ApplicationRecord
belongs_to :user_role
delegate :user, to: :user_role
belongs_to :project

validates :title, :datetime, :duration, :address, presence: true
Expand Down
5 changes: 5 additions & 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 :users, through: :user_roles
has_many :tasks, dependent: :destroy
has_many :invitations, dependent: :destroy
has_many :documents, dependent: :destroy
Expand All @@ -11,6 +12,10 @@ class Project < ApplicationRecord

after_create :set_leader_on_create

def admin?(user)
member?(user) && user_roles.find_by(user:).admin?
end

def member?(user)
user_roles.find_by(user:).present?
end
Expand Down
3 changes: 2 additions & 1 deletion app/models/task.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class Task < ApplicationRecord
belongs_to :project
belongs_to :author, class_name: 'User'
belongs_to :user_role
adoniranfranceh marked this conversation as resolved.
Show resolved Hide resolved
delegate :user, to: :user_role
belongs_to :assigned, class_name: 'User', optional: true

enum status: { uninitialized: 0, in_progress: 3, finished: 5, expired: 7, cancelled: 9 }
Expand Down
5 changes: 2 additions & 3 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ class User < ApplicationRecord

delegate :full_name, to: :profile

def can_edit?(meeting)
user_role = UserRole.get_user_role(meeting.project, self)
meeting.user_role.user == self || user_role.leader? || user_role.admin?
def can_edit?(item)
item.user == self || item.project.leader?(self) || item.project.admin?(self)
end
decoporteira marked this conversation as resolved.
Show resolved Hide resolved

def all_projects
Expand Down
4 changes: 0 additions & 4 deletions app/models/user_role.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ class UserRole < ApplicationRecord

default_scope { where(active: true) }

def self.get_user_role(project, user)
UserRole.find_by(user:, project:)
end

private

def only_one_leader_per_project
Expand Down
7 changes: 2 additions & 5 deletions app/views/calendars/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<%= render 'shared/project_header', project: @project %>

<%= form_with url: project_calendars_path(@project),
class: "d-flex row justify-content-end mb-3",
id: "filter-form",
Expand All @@ -16,19 +15,17 @@
class: 'btn btn-sm btn-outline-secondary' %>
</div>
<% end %>

<%= month_calendar(events: @events) do |date, events| %>
<div id="day-<%= date %>">
<%= date.day %>

<% events.each do |event| %>
<div class="event-<%= event.class.to_s.underscore %>">
<% if event.is_a? Meeting %>
<%= link_to event.title, project_meeting_path(@project, event), class: 'btn btn-sm btn-purple' %>
<% elsif event.is_a? Task %>
<%= link_to event.title, task_path(event), class: 'btn btn-sm btn-green' %>
<%= link_to event.title, project_task_path(@project, event), class: 'btn btn-sm btn-green' %>
<% end %>
</div>
<% end %>
</div>
<% end %>
<% end %>
9 changes: 4 additions & 5 deletions app/views/tasks/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<%= form_with(model: [@project, @task]) do |f| %>
<% if @task.errors.any? %>
<%= form_with(model: [@project, @task]) do |f| %>
<% if @task.errors.any? %>
<div class='text-danger'>
<h3><%= I18n.t('.errors_title') %>:</h3>
<ul>
Expand All @@ -9,7 +9,6 @@
</ul>
</div>
<% end %>

<div class="mb-3">
<%= f.label :title, class: 'form-label' %>
<%= f.text_field :title, class: 'form-control' %>
Expand All @@ -24,10 +23,10 @@
</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' %>
<%= f.collection_select :assigned_id, @project.users, :id, :email, :include_blank => true, class: 'form-control' %>
</div>
<div>
<%= f.submit I18n.t('tasks.save'), class: 'btn btn-primary'%>
<%= link_to t(:back), project_tasks_path(@task.project), class: 'btn btn-secondary' %>
</div>
<% end %>
<% end %>
2 changes: 1 addition & 1 deletion app/views/tasks/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<tbody>
<% @tasks.each do |task| %>
<tr>
<td><%= link_to task.title, task_path(task) %></td>
<td><%= link_to task.title, project_task_path(@project, task) %></td>
<td><%= task.assigned ? task.assigned.email : t('tasks.no_assigned') %></td>
<td><%= task.due_date ? task.due_date : t('tasks.no_due_date') %></td>
<td><%= t(task.status) %></td>
Expand Down
23 changes: 8 additions & 15 deletions app/views/tasks/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,50 +1,43 @@
<%= render 'shared/project_header', project: @task.project %>

<h1><%= Task.model_name.human %>: <%= @task.title %></h1>
<dl>
<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 :user_role %></dt>
<dd><%= @task.user_role.user.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 %>
</dl>

<br>
<div class="d-flex mb-3">
<%= button_to I18n.t('tasks.start_task'),
start_task_path(@task),
start_project_task_path(@project, @task), method: :patch,
class: 'btn btn-primary me-2' if @task.uninitialized? %>
<%= button_to I18n.t('tasks.finish_task'),
finish_task_path(@task),
finish_project_task_path(@task), method: :patch,
class: 'btn btn-primary me-2' if @task.in_progress? %>
<%= button_to I18n.t('tasks.cancel_task'),
cancel_task_path(@task),
cancel_project_task_path(@project, @task), method: :patch,
data: { turbo_confirm: t(:task_cancel_confirmation) },
class: 'btn btn-danger me-2' if @task.uninitialized? || @task.in_progress?%>
class: 'btn btn-danger me-2' if @task.uninitialized? || @task.in_progress? %>
<%= link_to I18n.t('tasks.edit_task'),
edit_task_path(@task),
edit_project_task_path(@project, @task),
class: 'btn btn-secondary' unless @task.finished? || @task.cancelled? %>
</div>

<div>
<%= link_to I18n.t('.back'),
project_tasks_path(@task.project),
class: 'btn btn-secondary' %>
</div>
</div>
94 changes: 47 additions & 47 deletions config/locales/models/tasks.pt-BR.yml
Original file line number Diff line number Diff line change
@@ -1,48 +1,48 @@
pt-BR:
uninitialized: Não iniciada
in_progress: Em andamento
task_empty_state: Sem tarefas registradas
finished: Finalizada
expired: Expirada
cancelled: Cancelada
task_cancel_confirmation: Cancelar tarefa?
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
uninitialized: Não iniciada
in_progress: Em andamento
task_empty_state: Sem tarefas registradas
finished: Finalizada
expired: Expirada
cancelled: Cancelada
task_cancel_confirmation: Cancelar tarefa?
activerecord:
models:
task:
one: Tarefa
other: Tarefas
attributes:
task:
title: Título
description: Descrição
due_date: Prazo
project_id: Projeto
user_role_id: Autor
user_role: 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
Loading
Loading