Fixed some bugs in tasks, apple feature:
* Update ordergroup stats when task is destroyed. * Removed assigned caching attribute in task object. * A lot of eager loading for tasks controller.
This commit is contained in:
parent
9919183cb0
commit
0dff5ea784
10 changed files with 52 additions and 46 deletions
|
|
@ -2,16 +2,6 @@ class Assignment < ActiveRecord::Base
|
|||
|
||||
belongs_to :user
|
||||
belongs_to :task
|
||||
|
||||
# after user is assigned mark task as assigned
|
||||
def after_create
|
||||
self.task.update_attribute(:assigned, true)
|
||||
end
|
||||
|
||||
# update assigned-attribute
|
||||
def after_destroy
|
||||
self.task.update_attribute(:assigned, false) if self.task.assignments.empty?
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -6,6 +6,9 @@
|
|||
# * account_balance (decimal)
|
||||
# * account_updated (datetime)
|
||||
class Ordergroup < Group
|
||||
|
||||
APPLE_MONTH_AGO = 6 # How many month back we will count tasks and orders sum
|
||||
|
||||
acts_as_paranoid # Avoid deleting the ordergroup for consistency of order-results
|
||||
serialize :stats
|
||||
|
||||
|
|
@ -56,11 +59,10 @@ class Ordergroup < Group
|
|||
end
|
||||
|
||||
def update_stats!
|
||||
time = 6.month.ago
|
||||
# Get hours for every job of each user in period
|
||||
jobs = users.sum { |u| u.tasks.done.sum(:duration, :conditions => ["updated_on > ?", time]) }
|
||||
jobs = users.sum { |u| u.tasks.done.sum(:duration, :conditions => ["updated_on > ?", APPLE_MONTH_AGO.month.ago]) }
|
||||
# Get group_order.price for every finished order in this period
|
||||
orders_sum = group_orders.includes(:order).merge(Order.finished).where('orders.ends >= ?', time).sum(:price)
|
||||
orders_sum = group_orders.includes(:order).merge(Order.finished).where('orders.ends >= ?', APPLE_MONTH_AGO.month.ago).sum(:price)
|
||||
|
||||
@readonly = false # Dirty hack, avoid getting RecordReadOnly exception when called in task after_save callback. A rails bug?
|
||||
update_attribute(:stats, {:jobs_size => jobs, :orders_sum => orders_sum})
|
||||
|
|
@ -79,12 +81,13 @@ class Ordergroup < Group
|
|||
# If the the option stop_ordering_under is set, the ordergroup is only allowed to participate in an order,
|
||||
# when the apples value is above the configured amount.
|
||||
# The restriction can be deactivated for each ordergroup.
|
||||
# Only ordergroups, which have participated in more than 5 order are affected
|
||||
# Only ordergroups, which have participated in more than 5 orders in total and more than 2 orders in apple time period
|
||||
def not_enough_apples?
|
||||
FoodsoftConfig[:stop_ordering_under].present? and
|
||||
!ignore_apple_restriction and
|
||||
apples < FoodsoftConfig[:stop_ordering_under] and
|
||||
group_orders.count > 5
|
||||
group_orders.count > 5 and
|
||||
group_orders.joins(:order).merge(Order.finished).where('orders.ends >= ?', APPLE_MONTH_AGO.month.ago).count > 2
|
||||
end
|
||||
|
||||
# Global average
|
||||
|
|
|
|||
|
|
@ -37,11 +37,12 @@ class Task < ActiveRecord::Base
|
|||
where(["tasks.due_date >= ? AND tasks.due_date <= ?", Time.now, number.days.from_now])
|
||||
end
|
||||
|
||||
# count tasks with no responsible person
|
||||
# count tasks with not enough responsible people
|
||||
# tasks for groups the user is not a member are ignored
|
||||
def self.unassigned_tasks_for(user)
|
||||
Task.undone.where(assigned: false).includes(:workgroup).all.select do |task|
|
||||
!task.workgroup or user.member_of?(task.workgroup)
|
||||
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
|
||||
|
||||
|
|
@ -54,7 +55,11 @@ class Task < ActiveRecord::Base
|
|||
end
|
||||
|
||||
def enough_users_assigned?
|
||||
assignments.find_all_by_accepted(true).size >= required_users ? true : false
|
||||
assignments.to_a.count(&:accepted) >= required_users ? true : false
|
||||
end
|
||||
|
||||
def still_required_users
|
||||
required_users - assignments.to_a.count(&:accepted)
|
||||
end
|
||||
|
||||
# Get users from comma seperated ids
|
||||
|
|
@ -95,12 +100,8 @@ class Task < ActiveRecord::Base
|
|||
@user_list ||= users.collect(&:id).join(", ")
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def update_ordergroup_stats
|
||||
if done
|
||||
Ordergroup.joins(:users).where(users: {id: user_ids}).each(&:update_stats!)
|
||||
end
|
||||
def update_ordergroup_stats(user_ids = self.user_ids)
|
||||
Ordergroup.joins(:users).where(users: {id: user_ids}).each(&:update_stats!)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue