未验证 提交 4fef7c2a 编写于 作者: E eileencodes 提交者: Aaron Patterson

Implement `scopes` method on each Reflection

Each reflection should be responsible for returning the scopes needed to
query against the db.
上级 1fc99557
......@@ -175,6 +175,10 @@ def join_keys(association_klass)
JoinKeys.new(foreign_key, active_record_primary_key)
end
def scopes
scope ? [scope] : []
end
def constraints
scope_chain.flatten
end
......@@ -815,6 +819,40 @@ def clear_association_scope_cache # :nodoc:
# There may be scopes on Person.comment_tags, Article.comment_tags and/or Comment.tags,
# but only Comment.tags will be represented in the #chain. So this method creates an array
# of scopes corresponding to the chain.
def scopes
@sc ||= if scope
source_reflection.scopes + through_reflection.scopes + [scope]
else
source_reflection.scopes + through_reflection.scopes
end
return @sc
scope_chain = source_reflection.scopes
scope_chain = through_reflection.scopes
# Add to it the scope from this reflection (if any)
scope_chain.first << scope if scope
through_scope_chain = through_reflection.scope_chain.map(&:dup)
if options[:source_type]
through_scope_chain.first << source_type_lambda
end
# Recursively fill out the rest of the array from the through reflection
scope_chain + through_scope_chain
end
def source_type_lambda
@source_type_lambda ||= begin
type = foreign_type
source_type = options[:source_type]
lambda { |object|
where(type => source_type)
}
end
end
def scope_chain
@scope_chain ||= begin
scope_chain = source_reflection.scope_chain.map(&:dup)
......@@ -825,11 +863,7 @@ def scope_chain
through_scope_chain = through_reflection.scope_chain.map(&:dup)
if options[:source_type]
type = foreign_type
source_type = options[:source_type]
through_scope_chain.first << lambda { |object|
where(type => source_type)
}
through_scope_chain.first << source_type_lambda
end
# Recursively fill out the rest of the array from the through reflection
......@@ -1008,6 +1042,14 @@ def initialize(reflection, previous_reflection)
@previous_reflection = previous_reflection
end
def scopes
if @previous_reflection.options[:source_type]
return super + [@previous_reflection.source_type_lambda]
else
return super
end
end
def klass
@reflection.klass
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册