support using a scope as a condition

just like Rails 3

Change-Id: I1a93a63a61ca553676533d303f03f2ee773b0371
Reviewed-on: https://gerrit.instructure.com/25702
Tested-by: Jenkins <jenkins@instructure.com>
Reviewed-by: Cody Cutrer <cody@instructure.com>
Product-Review: Cody Cutrer <cody@instructure.com>
QA-Review: Cody Cutrer <cody@instructure.com>
This commit is contained in:
Cody Cutrer 2013-10-28 10:17:08 -06:00
parent a86b39fc79
commit 2ce439e40c
7 changed files with 37 additions and 12 deletions

View File

@ -332,7 +332,7 @@ class AssignmentsApiController < ApplicationController
if Array(params[:include]).include?('submission')
submissions = Hash[
@context.submissions.except(:includes).
where(:assignment_id => @assignments).
where(:assignment_id => @assignments.except(:order)).
for_user(@current_user).
map { |s| [s.assignment_id,s] }
]

View File

@ -372,7 +372,7 @@ class ContextController < ApplicationController
@users.each_with_index{|u, i| @users_hash[u.id] = u; @users_order_hash[u.id] = i }
@current_user_services = {}
@current_user.user_services.each{|s| @current_user_services[s.service] = s }
@services = UserService.for_user(@users).sort_by{|s| @users_order_hash[s.user_id] || SortLast}
@services = UserService.for_user(@users.except(:select, :order)).sort_by{|s| @users_order_hash[s.user_id] || SortLast}
@services = @services.select{|service|
!UserService.configured_service?(service.service) || feature_and_service_enabled?(service.service.to_sym)
}

View File

@ -1226,7 +1226,7 @@ class Assignment < ActiveRecord::Base
representative
}.compact
else
visible_students
visible_students.to_a
end
end

View File

@ -466,10 +466,10 @@ class Course < ActiveRecord::Base
scope :not_deleted, where("workflow_state<>'deleted'")
scope :with_enrollments, lambda {
where("EXISTS (#{Enrollment.active.select("1").where("enrollments.course_id=courses.id").to_sql})")
where("EXISTS (?)", Enrollment.active.where("enrollments.course_id=courses.id"))
}
scope :without_enrollments, lambda {
where("NOT EXISTS (#{Enrollment.active.select("1").where("enrollments.course_id=courses.id").to_sql})")
where("NOT EXISTS (?)", Enrollment.active.where("enrollments.course_id=courses.id"))
}
scope :completed, lambda {
joins(:enrollment_term).
@ -484,12 +484,12 @@ class Course < ActiveRecord::Base
scope :by_teachers, lambda { |teacher_ids|
teacher_ids.empty? ?
none :
where("EXISTS (#{Enrollment.active.select("1").where("enrollments.course_id=courses.id AND enrollments.type='TeacherEnrollment' AND enrollments.user_id IN (?)", teacher_ids).to_sql})")
where("EXISTS (?)", Enrollment.active.where("enrollments.course_id=courses.id AND enrollments.type='TeacherEnrollment' AND enrollments.user_id IN (?)", teacher_ids))
}
scope :by_associated_accounts, lambda{ |account_ids|
account_ids.empty? ?
none :
where("EXISTS (#{CourseAccountAssociation.select("1").where("course_account_associations.course_id=courses.id AND course_account_associations.account_id IN (?)", account_ids).to_sql})")
where("EXISTS (?)", CourseAccountAssociation.where("course_account_associations.course_id=courses.id AND course_account_associations.account_id IN (?)", account_ids))
}
scope :deleted, where(:workflow_state => 'deleted')

View File

@ -245,7 +245,7 @@ class User < ActiveRecord::Base
PageView.for_user(self, options)
end
scope :of_account, lambda { |account| where("EXISTS (#{account.user_account_associations.select("1").where("user_account_associations.user_id=users.id").to_sql})") }
scope :of_account, lambda { |account| where("EXISTS (?)", account.user_account_associations.where("user_account_associations.user_id=users.id")) }
scope :recently_logged_in, lambda {
includes(:pseudonyms).
where("pseudonyms.current_login_at>?", 1.month.ago).
@ -261,7 +261,7 @@ class User < ActiveRecord::Base
end
}
scope :name_like, lambda { |name|
where("#{wildcard('users.name', 'users.short_name', name)} OR EXISTS (#{Pseudonym.select("1").where(wildcard('pseudonyms.sis_user_id', 'pseudonyms.unique_id', name)).where("pseudonyms.user_id=users.id").active.to_sql})")
where("#{wildcard('users.name', 'users.short_name', name)} OR EXISTS (?)", Pseudonym.where(wildcard('pseudonyms.sis_user_id', 'pseudonyms.unique_id', name)).where("pseudonyms.user_id=users.id").active)
}
scope :active, where("users.workflow_state<>'deleted'")

View File

@ -164,6 +164,24 @@ ActiveRecord::Base.class_eval do
send(callback, *methods)
end
alias_method_chain :before_validation, :rails3_compatibility
def quote_bound_value_with_relations(value)
if ActiveRecord::Associations::AssociationCollection === value
with_exclusive_scope do
value = value.scoped
end
end
if ActiveRecord::NamedScope::Scope === value
with_exclusive_scope do
unless value.scope(:find, :select)
value = value.select("#{value.quoted_table_name}.#{value.primary_key}")
end
return value.to_sql
end
end
quote_bound_value_without_relations(value)
end
alias_method_chain :quote_bound_value, :relations
end
# support 0 arguments
@ -197,7 +215,14 @@ ActiveRecord::NamedScope::Scope.class_eval do
# Instead, just take the easy way out and let with_scope do all
# the hard work
def unspin
with_exclusive_scope { self.scope(:find) }
scope = proxy_scope
scope = scope.proxy_scope while (ActiveRecord::NamedScope::Scope === scope)
scope.send(:with_exclusive_scope) { self.scope(:find) }
end
def is_a?(klass)
# no, it's not a freaking Hash, and don't instantiate a gazillion things to find that out
super || klass >= Array
end
end

View File

@ -46,7 +46,7 @@ module Mutable
if self.respond_to? :submissions
stream_items = StreamItem.select([:id, :context_type, :context_id]).
where(:asset_type => 'Submission', :asset_id => submissions).
includes(:context)
includes(:context).to_a
stream_item_contexts = stream_items.map { |si| [si.context_type, si.context_id] }
associated_shards = stream_items.inject([]) { |result, si| result | si.associated_shards }
Shard.with_each_shard(associated_shards) do
@ -61,7 +61,7 @@ module Mutable
submissions = submissions(:include => {:hidden_submission_comments => :author})
stream_items = StreamItem.select([:id, :context_type, :context_id]).
where(:asset_type => 'Submission', :asset_id => submissions).
includes(:context)
includes(:context).to_a
stream_item_contexts = stream_items.map { |si| [si.context_type, si.context_id] }
associated_shards = stream_items.inject([]) { |result, si| result | si.associated_shards }
Shard.with_each_shard(associated_shards) do