Class: TasksController

Inherits:
ApplicationController show all
Defined in:
app/controllers/tasks_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

current

Methods included from PathHelper

#finance_group_transactions_path

Instance Method Details

#acceptObject

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


84
85
86
87
88
89
90
91
92
# File 'app/controllers/tasks_controller.rb', line 84

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
  redirect_to user_tasks_path, :notice => I18n.t('tasks.accept.notice')
end

#archiveObject

Shows all tasks, which are already done


106
107
108
# File 'app/controllers/tasks_controller.rb', line 106

def archive
  @tasks = Task.done.page(params[:page]).per(@per_page).order('tasks.updated_on DESC').includes(assignments: :user)
end

#createObject


19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/controllers/tasks_controller.rb', line 19

def create
  @task = Task.new(current_user_id: current_user.id)
  @task.created_by = current_user
  @task.attributes=(task_params)
  if params[:periodic]
    @task.periodic_task_group = PeriodicTaskGroup.new
  end
  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')
  else
    render :template => "tasks/new"
  end
end

#destroyObject


67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/controllers/tasks_controller.rb', line 67

def destroy
  task = Task.find(params[:id])
  # Save user_ids to update apple statistics after destroy
  user_ids = task.user_ids
  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')
end

#editObject


38
39
40
41
42
# File 'app/controllers/tasks_controller.rb', line 38

def edit
  @task = Task.find(params[:id])
  @periodic = !!params[:periodic]
  @task.current_user_id = current_user.id
end

#indexObject

auto_complete_for :user, :nick


5
6
7
8
# File 'app/controllers/tasks_controller.rb', line 5

def index
  @non_group_tasks = Task.non_group.order('due_date', 'name').includes(assignments: :user)
  @groups = Workgroup.order(:name).includes(open_tasks: {assignments: :user})
end

#newObject


15
16
17
# File 'app/controllers/tasks_controller.rb', line 15

def new
  @task = Task.new(current_user_id: current_user.id)
end

#rejectObject

deletes assignment between current_user and given taskcurrent_user_id: current_user.id


95
96
97
98
# File 'app/controllers/tasks_controller.rb', line 95

def reject
  Task.find(params[:id]).users.delete(current_user)
  redirect_to :action => "index"
end

#set_doneObject


100
101
102
103
# File 'app/controllers/tasks_controller.rb', line 100

def set_done
  Task.find(params[:id]).update_attribute :done, true
  redirect_to tasks_url, :notice => I18n.t('tasks.set_done.notice')
end

#showObject


34
35
36
# File 'app/controllers/tasks_controller.rb', line 34

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

#updateObject


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/controllers/tasks_controller.rb', line 44

def update
  @task = Task.find(params[:id])
  task_group = @task.periodic_task_group
  was_periodic = @task.periodic?
  prev_due_date = @task.due_date
  @task.current_user_id = current_user.id
  @task.attributes=(task_params)
  if @task.errors.empty? && @task.save
    task_group.update_tasks_including(@task, prev_due_date) if params[:periodic]
    flash[:notice] = I18n.t('tasks.update.notice')
    if was_periodic && !@task.periodic?
      flash[:notice] = I18n.t('tasks.update.notice_converted')
    end
    if @task.workgroup
      redirect_to workgroup_tasks_url(workgroup_id: @task.workgroup_id)
    else
      redirect_to tasks_url
    end
  else
    render :template => "tasks/edit"
  end
end

#userObject


10
11
12
13
# File 'app/controllers/tasks_controller.rb', line 10

def user
  @unaccepted_tasks = Task.unaccepted_tasks_for(current_user)
  @accepted_tasks = Task.accepted_tasks_for(current_user)
end

#workgroupObject

shows workgroup (normal group) to edit weekly_tasks_template


111
112
113
114
115
116
# File 'app/controllers/tasks_controller.rb', line 111

def workgroup
  @group = Group.find(params[:workgroup_id])
  if @group.is_a? Ordergroup
    redirect_to tasks_url, :alert => I18n.t('tasks.error_not_found')
  end
end