foodsoft/app/controllers/tasks_controller.rb

121 lines
3.6 KiB
Ruby
Raw Permalink Normal View History

2009-01-06 11:49:19 +01:00
class TasksController < ApplicationController
# auto_complete_for :user, :nick
2009-01-06 11:49:19 +01:00
def index
2016-02-24 20:58:32 +01:00
@non_group_tasks = Task.non_group.order('due_date', 'name').includes(assignments: :user)
@groups = Workgroup.order(:name).includes(open_tasks: { assignments: :user })
2009-01-06 11:49:19 +01:00
end
def user
@unaccepted_tasks = Task.unaccepted_tasks_for(current_user)
@accepted_tasks = Task.accepted_tasks_for(current_user)
2009-01-06 11:49:19 +01:00
end
def show
@task = Task.find(params[:id])
end
2009-01-06 11:49:19 +01:00
def new
@task = Task.new(current_user_id: current_user.id)
2009-01-06 11:49:19 +01:00
end
def edit
@task = Task.find(params[:id])
@periodic = !!params[:periodic]
@task.current_user_id = current_user.id
end
2009-01-06 11:49:19 +01:00
def create
@task = Task.new(current_user_id: current_user.id)
2020-03-06 13:04:03 +01:00
@task.created_by = current_user
@task.attributes = (task_params)
@task.periodic_task_group = PeriodicTaskGroup.new if params[:periodic]
2011-05-14 19:02:52 +02:00
if @task.save
@task.periodic_task_group.create_tasks_for_upfront_days if params[:periodic]
redirect_to tasks_url, notice: I18n.t('tasks.create.notice')
2009-01-06 11:49:19 +01:00
else
render template: 'tasks/new'
2009-01-06 11:49:19 +01:00
end
end
2009-01-06 11:49:19 +01:00
def update
@task = Task.find(params[:id])
task_group = @task.periodic_task_group
was_periodic = @task.periodic?
2015-05-01 21:31:06 +02:00
prev_due_date = @task.due_date
@task.current_user_id = current_user.id
@task.attributes = (task_params)
if @task.errors.empty? && @task.save
2015-05-01 21:31:06 +02:00
task_group.update_tasks_including(@task, prev_due_date) if params[:periodic]
2013-04-12 00:00:02 +02:00
flash[:notice] = I18n.t('tasks.update.notice')
flash[:notice] = I18n.t('tasks.update.notice_converted') if was_periodic && !@task.periodic?
if @task.workgroup
redirect_to workgroup_tasks_url(workgroup_id: @task.workgroup_id)
2009-01-06 11:49:19 +01:00
else
redirect_to tasks_url
2009-01-06 11:49:19 +01:00
end
else
render template: 'tasks/edit'
2009-01-06 11:49:19 +01:00
end
end
2009-01-06 11:49:19 +01:00
def destroy
task = Task.find(params[:id])
# Save user_ids to update apple statistics after destroy
user_ids = task.user_ids
2013-06-21 22:04:36 +02:00
if params[:periodic]
task.periodic_task_group.exclude_tasks_before(task)
task.periodic_task_group.destroy
else
task.destroy
end
task.update_ordergroup_stats(user_ids)
redirect_to tasks_url, notice: I18n.t('tasks.destroy.notice')
2009-01-06 11:49:19 +01:00
end
2009-01-06 11:49:19 +01:00
# assign current_user to the task and set the assignment to "accepted"
# if there is already an assignment, only accepted will be set to true
def accept
task = Task.find(params[:id])
if ass = task.is_assigned?(current_user)
ass.update_attribute(:accepted, true)
else
task.assignments.create(user: current_user, accepted: true)
2009-01-06 11:49:19 +01:00
end
redirect_to user_tasks_path, notice: I18n.t('tasks.accept.notice')
2009-01-06 11:49:19 +01:00
end
# deletes assignment between current_user and given taskcurrent_user_id: current_user.id
2009-01-06 11:49:19 +01:00
def reject
Task.find(params[:id]).users.delete(current_user)
redirect_to action: 'index'
2009-01-06 11:49:19 +01:00
end
2012-10-08 21:52:03 +02:00
def set_done
Task.find(params[:id]).update_attribute :done, true
redirect_to tasks_url, notice: I18n.t('tasks.set_done.notice')
2009-01-06 11:49:19 +01:00
end
2009-01-06 11:49:19 +01:00
# Shows all tasks, which are already done
def archive
@tasks = Task.done.page(params[:page]).per(@per_page).order('tasks.updated_on DESC').includes(assignments: :user)
2009-01-06 11:49:19 +01:00
end
2009-01-06 11:49:19 +01:00
# shows workgroup (normal group) to edit weekly_tasks_template
def workgroup
@group = Group.find(params[:workgroup_id])
return unless @group.is_a? Ordergroup
redirect_to tasks_url, alert: I18n.t('tasks.error_not_found')
2009-01-06 11:49:19 +01:00
end
private
def task_params
params
.require(:task)
.permit(:name, :description, :duration, :user_list, :required_users, :workgroup_id, :due_date, :done)
end
2009-01-06 11:49:19 +01:00
end