Class: Task

Inherits:
ApplicationRecord show all
Defined in:
app/models/task.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#current_user_idObject

Returns the value of attribute current_user_id


13
14
15
# File 'app/models/task.rb', line 13

def current_user_id
  @current_user_id
end

Class Method Details

.accepted_tasks_for(user) ⇒ Object

Find all accepted tasks, which aren't done


32
33
34
# File 'app/models/task.rb', line 32

def self.accepted_tasks_for(user)
  user.tasks.undone.where(assignments: {accepted: true})
end

.next_assigned_tasks_for(user, number = FoodsoftConfig[:tasks_period_days].to_i) ⇒ Object

find all tasks in the period (or another number of days)


38
39
40
41
# File 'app/models/task.rb', line 38

def self.next_assigned_tasks_for(user, number = FoodsoftConfig[:tasks_period_days].to_i)
  user.tasks.undone.where(assignments: {accepted: true}).
      where(["tasks.due_date >= ? AND tasks.due_date <= ?", Time.now, number.days.from_now])
end

.next_unassigned_tasks_for(user, max = 2) ⇒ Object


52
53
54
55
56
57
58
59
60
# File 'app/models/task.rb', line 52

def self.next_unassigned_tasks_for(user, max = 2)
  periodic_task_group_count = {}
  self.unassigned_tasks_for(user).reject do |item|
    next false unless item.periodic_task_group
    count = periodic_task_group_count[item.periodic_task_group] || 0
    periodic_task_group_count[item.periodic_task_group] = count + 1
    count >= max
  end
end

.unaccepted_tasks_for(user) ⇒ Object

Find all tasks, for which the current user should be responsible but which aren't accepted yet


27
28
29
# File 'app/models/task.rb', line 27

def self.unaccepted_tasks_for(user)
  user.tasks.undone.where(assignments: {accepted: false})
end

.unassigned_tasks_for(user) ⇒ Object

count tasks with not enough responsible people tasks for groups the user is not a member are ignored


45
46
47
48
49
50
# File 'app/models/task.rb', line 45

def self.unassigned_tasks_for(user)
  undone.includes(:assignments, workgroup: :memberships).select do |task|
    !task.enough_users_assigned? and
        (!task.workgroup or task.workgroup.memberships.detect { |m| m.user_id == user.id })
  end
end

Instance Method Details

#enough_users_assigned?Boolean

Returns:

  • (Boolean)

74
75
76
# File 'app/models/task.rb', line 74

def enough_users_assigned?
  assignments.to_a.count(&:accepted) >= required_users ? true : false
end

#exclude_from_periodic_task_groupObject


121
122
123
124
# File 'app/models/task.rb', line 121

def exclude_from_periodic_task_group
  self.periodic_task_group = nil
  true
end

#is_accepted?(user) ⇒ Boolean

Returns:

  • (Boolean)

70
71
72
# File 'app/models/task.rb', line 70

def is_accepted?(user)
  self.assignments.detect {|ass| ass.user_id == user.id && ass.accepted }
end

#is_assigned?(user) ⇒ Boolean

Returns:

  • (Boolean)

66
67
68
# File 'app/models/task.rb', line 66

def is_assigned?(user)
  self.assignments.detect {|ass| ass.user_id == user.id }
end

#periodic?Boolean

Returns:

  • (Boolean)

62
63
64
# File 'app/models/task.rb', line 62

def periodic?
  not periodic_task_group.nil?
end

#still_required_usersObject


78
79
80
# File 'app/models/task.rb', line 78

def still_required_users
  required_users - assignments.to_a.count(&:accepted)
end

#update_ordergroup_stats(user_ids = self.user_ids) ⇒ Object


117
118
119
# File 'app/models/task.rb', line 117

def update_ordergroup_stats(user_ids = self.user_ids)
  Ordergroup.joins(:users).where(users: {id: user_ids}).each(&:update_stats!)
end

#user_listObject


113
114
115
# File 'app/models/task.rb', line 113

def user_list
  @user_list ||= users.collect(&:id).join(", ")
end

#user_list=(ids) ⇒ Object

Get users from comma seperated ids and makes the users responsible for the task TODO: check for maximal number of users


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'app/models/task.rb', line 85

def user_list=(ids)
  list = ids.split(",").map(&:to_i)
  new_users = (list - users.collect(&:id)).uniq
  old_users = users.reject { |user| list.include?(user.id) }

  self.class.transaction do
    # delete old assignments
    if old_users.any?
      assignments.where(user_id: old_users.map(&:id)).each(&:destroy)
    end
    # create new assignments
    new_users.each do |id|
      user = User.find(id)
      if user.blank?
        errors.add(:user_list)
      else
        if  id == current_user_id.to_i
          # current_user will accept, when he puts himself to the list of users
          self.assignments.build :user => user, :accepted => true
        else
          # normal assignement
          self.assignments.build :user => user
        end
      end
    end
  end
end