lib/searchlogic/named_scopes/or_conditions.rb in searchlogic-2.3.5 vs lib/searchlogic/named_scopes/or_conditions.rb in searchlogic-2.3.6
- old
+ new
@@ -65,18 +65,20 @@
# is what will be used. Once we are able to get a consistent list of conditions we can easily
# create a scope for it.
def interpolate_or_conditions(parts)
conditions = []
last_condition = nil
-
+
parts.reverse.each do |part|
if details = condition_details(part)
# We are a searchlogic defined scope
conditions << "#{details[:column]}_#{details[:condition]}"
last_condition = details[:condition]
- elsif details = association_condition_details(part)
- # pending, need to find the last condition
+ elsif association_details = association_condition_details(part, last_condition)
+ path = full_association_path(part, last_condition, association_details[:association])
+ conditions << "#{path[:path].join("_").to_sym}_#{path[:column]}_#{path[:condition]}"
+ last_condition = path[:condition] || nil
elsif local_condition?(part)
# We are a custom scope
conditions << part
elsif column_names.include?(part)
# we are a column, use the last condition
@@ -89,12 +91,29 @@
else
raise UnknownConditionError.new("The condition '#{part}' is not a valid condition, we could not find any scopes that match this.")
end
end
- conditions
+ conditions.reverse
end
+
+ def full_association_path(part, last_condition, given_assoc)
+ path = [given_assoc.to_sym]
+ part.sub!(/^#{given_assoc}_/, "")
+ klass = self
+ while klass = klass.send(:reflect_on_association, given_assoc.to_sym)
+ klass = klass.klass
+ if details = klass.send(:association_condition_details, part, last_condition)
+ path << details[:association]
+ part = details[:condition]
+ given_assoc = details[:association]
+ elsif details = klass.send(:condition_details, part, nil)
+ return { :path => path, :column => details[:column], :condition => details[:condition] }
+ end
+ end
+ { :path => path, :column => part, :condition => last_condition }
+ end
def create_or_condition(scopes, args)
named_scope scopes.join("_or_"), lambda { |*args|
merge_scopes_with_or(scopes.collect { |scope| [scope, *args] })
}
@@ -107,10 +126,12 @@
scope = scopes.inject(scoped({})) do |scope, info|
scope_name, *args = info
scope.send(scope_name, *args)
end
- scope.scope(:find).merge(:conditions => "(" + conditions.join(") OR (") + ")")
+ options = scope.scope(:find)
+ options.delete(:readonly) unless scope.proxy_options.key?(:readonly)
+ options.merge(:conditions => "(" + conditions.join(") OR (") + ")")
end
end
end
-end
\ No newline at end of file
+end