foodsoft/app/controllers/tasks_controller.rb

106 lines
2.9 KiB
Ruby
Raw Normal View History

# encoding: utf-8
2009-01-06 11:49:19 +01:00
class TasksController < ApplicationController
#auto_complete_for :user, :nick
def index
@non_group_tasks = Task.non_group.includes(assignments: :user)
@groups = Workgroup.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 new
@task = Task.new(current_user_id: current_user.id)
2009-01-06 11:49:19 +01:00
end
def create
@task = Task.new(params[:task])
2013-06-21 13:48:48 +02:00
if params[:periodic]
@task.periodic_task_group = PeriodicTaskGroup.new
end
2011-05-14 19:02:52 +02:00
if @task.save
2013-04-12 00:00:02 +02:00
redirect_to tasks_url, :notice => I18n.t('tasks.create.notice')
2009-01-06 11:49:19 +01:00
else
render :template => "tasks/new"
end
end
def show
@task = Task.find(params[:id])
end
def edit
@task = Task.find(params[:id])
@task.current_user_id = current_user.id
2009-01-06 11:49:19 +01:00
end
def update
@task = Task.find(params[:id])
@task.attributes=(params[:task])
if @task.errors.empty? && @task.save
2013-04-12 00:00:02 +02:00
flash[:notice] = I18n.t('tasks.update.notice')
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"
end
end
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)
2013-04-12 00:00:02 +02:00
redirect_to tasks_url, :notice => I18n.t('tasks.destroy.notice')
2009-01-06 11:49:19 +01:00
end
# 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)
end
2013-04-12 00:00:02 +02:00
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 task
def reject
Task.find(params[:id]).users.delete(current_user)
redirect_to :action => "index"
end
2012-10-08 21:52:03 +02:00
def set_done
Task.find(params[:id]).update_attribute :done, true
2013-04-12 00:00:02 +02:00
redirect_to tasks_url, :notice => I18n.t('tasks.set_done.notice')
2009-01-06 11:49:19 +01:00
end
# 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
# shows workgroup (normal group) to edit weekly_tasks_template
def workgroup
@group = Group.find(params[:workgroup_id])
if @group.is_a? Ordergroup
2013-04-12 01:08:35 +02:00
redirect_to tasks_url, :alert => I18n.t('tasks.error_not_found')
2009-01-06 11:49:19 +01:00
end
end
end