Permalink
Browse files

Subs are now harder better faster stronger

  • Loading branch information...
1 parent 46ea701 commit 0f8ab9f8ade6487344f4a8a8e153bbf8b146f685 @iamringo iamringo committed Aug 7, 2009
@@ -8,8 +8,8 @@ def index
#@upcoming_shifts = current_user.shifts.select{|shift| shift.scheduled? and shift.end > Time.now and !(shift.submitted?) and @department.locations.include?(shift.location)}.sort_by(&:start)[0..3]
@active_shifts = Shift.find(:all, :conditions => {:signed_in => true, :department_id => current_department.id}, :order => :start)
@upcoming_shifts = Shift.find(:all, :conditions => ['"user_id" = ? and "end" > ? and "department_id" = ?', current_user.id, Time.now, current_department.id], :order => :start, :limit => 5)
- @subs_you_requested = SubRequest.find(:all, :conditions => ["end > ?",Time.now]).select{|sub| sub.shift.user == current_user}.sort_by(&:start)
- @subs_you_can_take = current_user.available_sub_requests
+ @subs_you_requested = SubRequest.find(:all, :conditions => ["end > ? AND user_id = ?", Time.now, current_user.id]).sort_by(&:start)
+ @subs_you_can_take = current_user.available_sub_requests([@department])
@most_recent_payform= current_user.payforms.sort_by(&:date).last
@watched_objects = current_user.user_config.watched_data_objects.split(", ").map{|id| DataObject.find(id)}.flatten
@@ -22,6 +22,7 @@ def edit
def create
@sub_request = SubRequest.new(params[:sub_request])
@sub_request.shift = Shift.find(params[:shift_id])
+ @sub_request.user = @sub_request.shift.user
@sub_request.save! #TODO: need to save before adding polymorphisms -- sorry!
params[:list_of_logins].split(",").each do |l|
l = l.split("||")
@@ -40,7 +41,7 @@ def update
#TODO This should probably be in a transaction, so that
#if the update fails all sub sources don't get deleted...
return unless require_owner_or_dept_admin(@sub_request.shift, current_department)
- @sub_request.user_sources = []
+ UserSinksUserSource.delete_all("user_sink_type= \"SubRequest\" AND user_sink_id = \"#{@sub_request.id}\"")
params[:list_of_logins].split(",").each do |l|
l = l.split("||")
@sub_request.user_sources << l[0].constantize.find(l[1]) if l.length == 2
@@ -75,4 +76,3 @@ def take
end
end
end
-
@@ -1,5 +1,6 @@
class SubRequest < ActiveRecord::Base
belongs_to :shift
+ belongs_to :user
validates_presence_of :reason
validates_presence_of :shift
@@ -82,4 +83,3 @@ def user_does_not_have_concurrent_sub_request
end
end
-
View
@@ -16,6 +16,7 @@ class User < ActiveRecord::Base
has_many :notices, :as => :author
has_many :notices, :as => :remover
has_one :punch_clock
+ has_many :sub_requests
# New user configs are created by a user observer, after create
has_one :user_config, :dependent => :destroy
@@ -175,8 +176,17 @@ def users
[self]
end
- def available_sub_requests #TODO: this could probalby be optimized
- SubRequest.all.select{|sr| sr.substitutes.include?(self)}
+ def available_sub_requests(departments = self.departments) #TODO: this could probalby be optimized even more
+ ActiveRecord::Base.transaction do
+ a = UserSinksUserSource.find(:all, :conditions => ["user_sink_type = \"SubRequest\" AND user_source_type = \"User\" AND user_source_id = \"#{self.id}\""])
+ b = departments.collect do |department|
+ UserSinksUserSource.find(:all, :conditions => ["user_sink_type = \"SubRequest\" AND user_source_type = \"Department\" AND user_source_id = \"#{department.id}\""])
+ end
+ c = self.roles.collect do |role|
+ UserSinksUserSource.find(:all, :conditions => ["user_sink_type = \"SubRequest\" AND user_source_type = \"Role\" AND user_source_id = \"#{role.id}\""])
+ end
+ (a+b.flatten+c.flatten).collect {|u| SubRequest.find(u.user_sink_id)}
+ end
end
def restrictions #TODO: this could probalby be optimized
@@ -230,4 +240,3 @@ def departments_not_empty
end
end
-
@@ -7,6 +7,7 @@ def self.up
t.datetime :mandatory_end
t.string :reason
t.references :shift
+ t.references :user #Speeds up database queries!
t.timestamps
Oops, something went wrong.

0 comments on commit 0f8ab9f

Please sign in to comment.