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.



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

def current_user_id
  @current_user_id
end

Class Method Details

.accepted_tasks_for(user) ⇒ Object

Find all accepted tasks, which aren’t done



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

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)



36
37
38
39
# File 'app/models/task.rb', line 36

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



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

def self.next_unassigned_tasks_for(user, max = 2)
  periodic_task_group_count = {}
  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



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

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



43
44
45
46
47
48
# File 'app/models/task.rb', line 43

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)


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

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

#exclude_from_periodic_task_groupObject



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

def exclude_from_periodic_task_group
  self.periodic_task_group = nil
  true
end

#is_accepted?(user) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#is_assigned?(user) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#periodic?Boolean

Returns:

  • (Boolean)


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

def periodic?
  !periodic_task_group.nil?
end

#still_required_usersObject



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

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

#update_ordergroup_stats(user_ids = self.user_ids) ⇒ Object



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

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

#user_listObject



108
109
110
# File 'app/models/task.rb', line 108

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



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

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