2009-01-06 11:49:19 +01:00
|
|
|
class Task < ActiveRecord::Base
|
|
|
|
has_many :assignments, :dependent => :destroy
|
|
|
|
has_many :users, :through => :assignments
|
2009-01-15 12:14:01 +01:00
|
|
|
belongs_to :workgroup
|
|
|
|
|
2009-02-06 20:51:14 +01:00
|
|
|
named_scope :non_group, :conditions => { :workgroup_id => nil, :done => false }
|
2009-03-23 11:23:41 +01:00
|
|
|
named_scope :done, :conditions => {:done => true}, :order => "due_date DESC"
|
2009-02-10 15:07:47 +01:00
|
|
|
named_scope :upcoming, lambda { |*args| {:conditions => ["done = 0 AND due_date = ?", (args.first || 7.days.from_now)]} }
|
2009-01-06 11:49:19 +01:00
|
|
|
|
|
|
|
# form will send user in string. responsibilities will added later
|
|
|
|
attr_protected :users
|
|
|
|
|
|
|
|
validates_length_of :name, :minimum => 3
|
2011-05-13 22:43:23 +02:00
|
|
|
validates_numericality_of :duration, :required_users, :only_integer => true, :greater_than => 1
|
2009-08-01 13:41:22 +02:00
|
|
|
|
|
|
|
after_save :update_ordergroup_stats
|
2009-01-06 11:49:19 +01:00
|
|
|
|
|
|
|
def is_assigned?(user)
|
|
|
|
self.assignments.detect {|ass| ass.user_id == user.id }
|
|
|
|
end
|
|
|
|
|
|
|
|
def is_accepted?(user)
|
|
|
|
self.assignments.detect {|ass| ass.user_id == user.id && ass.accepted }
|
|
|
|
end
|
|
|
|
|
|
|
|
def enough_users_assigned?
|
|
|
|
assignments.find_all_by_accepted(true).size >= required_users ? true : false
|
|
|
|
end
|
|
|
|
|
|
|
|
# extracts nicknames from a comma seperated string
|
|
|
|
# and makes the users responsible for the task
|
|
|
|
def user_list=(string)
|
|
|
|
@user_list = string.split(%r{,\s*})
|
|
|
|
new_users = @user_list - users.collect(&:nick)
|
|
|
|
old_users = users.reject { |user| @user_list.include?(user.nick) }
|
|
|
|
|
|
|
|
logger.debug "New users: #{new_users}"
|
|
|
|
logger.debug "Old users: #{old_users}"
|
|
|
|
|
|
|
|
self.class.transaction do
|
|
|
|
# delete old assignments
|
|
|
|
if old_users.any?
|
|
|
|
assignments.find(:all, :conditions => ["user_id IN (?)", old_users.collect(&:id)]).each(&:destroy)
|
|
|
|
end
|
|
|
|
# create new assignments
|
|
|
|
new_users.each do |nick|
|
|
|
|
user = User.find_by_nick(nick)
|
|
|
|
if user.blank?
|
|
|
|
errors.add(:user_list)
|
|
|
|
else
|
|
|
|
if user == User.current_user
|
|
|
|
# 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
|
|
|
|
|
|
|
|
def user_list
|
|
|
|
@user_list ||= users.collect(&:nick).join(", ")
|
|
|
|
end
|
2009-08-01 13:41:22 +02:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def update_ordergroup_stats
|
|
|
|
if done
|
2011-05-07 21:54:00 +02:00
|
|
|
users.each { |u| u.ordergroup.update_stats! if u.ordergroup }
|
2009-08-01 13:41:22 +02:00
|
|
|
end
|
|
|
|
end
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
2011-05-07 20:50:39 +02:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: tasks
|
|
|
|
#
|
2011-05-07 21:55:24 +02:00
|
|
|
# id :integer(4) not null, primary key
|
2011-05-07 20:50:39 +02:00
|
|
|
# name :string(255) default(""), not null
|
|
|
|
# description :string(255)
|
|
|
|
# due_date :date
|
2011-05-07 21:55:24 +02:00
|
|
|
# done :boolean(1) default(FALSE)
|
|
|
|
# workgroup_id :integer(4)
|
|
|
|
# assigned :boolean(1) default(FALSE)
|
2011-05-07 20:50:39 +02:00
|
|
|
# created_on :datetime not null
|
|
|
|
# updated_on :datetime not null
|
2011-05-07 21:55:24 +02:00
|
|
|
# required_users :integer(4) default(1)
|
|
|
|
# weekly :boolean(1)
|
|
|
|
# duration :integer(4) default(1)
|
2011-05-07 20:50:39 +02:00
|
|
|
#
|
|
|
|
|