Class: Task

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
ActiveModel::MassAssignmentSecurity
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


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

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)


41
42
43
44
# File 'app/models/task.rb', line 41

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


55
56
57
58
59
60
61
62
63
# File 'app/models/task.rb', line 55

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


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

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


48
49
50
51
52
53
# File 'app/models/task.rb', line 48

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


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

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

#exclude_from_periodic_task_groupObject


124
125
126
127
# File 'app/models/task.rb', line 124

def exclude_from_periodic_task_group
  self.periodic_task_group = nil
  true
end

#is_accepted?(user) ⇒ Boolean


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

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

#is_assigned?(user) ⇒ Boolean


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

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

#periodic?Boolean


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

def periodic?
  not periodic_task_group.nil?
end

#still_required_usersObject


81
82
83
# File 'app/models/task.rb', line 81

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

#update_ordergroup_stats(user_ids = self.user_ids) ⇒ Object


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

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

#user_listObject


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

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


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

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