Skip to content

Commit

Permalink
Merge pull request #80 from TreinaDev/feature/adiciona-user-role-a-ta…
Browse files Browse the repository at this point in the history
…refa

Feature/adiciona user role a tarefa
  • Loading branch information
decoporteira authored Feb 7, 2024
2 parents b84b9ec + 5ad2a74 commit 5e62169
Show file tree
Hide file tree
Showing 29 changed files with 269 additions and 206 deletions.
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')
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')
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
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

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

0 comments on commit 5e62169

Please sign in to comment.