Permalink
Browse files

Fixed association condition interpreter from erroneously catching "OR…

…" conditions & added failing case "chaining conditions with n-depth associations", with resolution for issue.
  • Loading branch information...
n00bish authored and binarylogic committed Oct 25, 2009
1 parent 6915df9 commit b2ebf53c8310bc41cb5b3fbef17946423bad8c71
View
@@ -37,4 +37,4 @@ class << self; include Searchlogic::ActiveRecord::Consistency; end
if defined?(ActionController)
require "searchlogic/rails_helpers"
ActionController::Base.helper(Searchlogic::RailsHelpers)
end
end
@@ -13,10 +13,15 @@ module NamedScopes
# ActiveRecord hides this internally in a Proc, so we have to try and pull it out with this
# method.
def named_scope_options(name)
key = scopes.key?(name.to_sym) ? name.to_sym : primary_condition_name(name)
if key
eval("options", scopes[key].binding)
elsif name.to_s.downcase.match("_or_")
condition = find_applied_condition(name)
newname = name.to_s.gsub(/_or_/, "_#{condition}_or_").to_sym
named_scope_options(newname) unless name == newname
else
nil
end
@@ -64,4 +64,4 @@ def named_scope_options(name) # :nodoc:
end
end
end
end
end
@@ -8,7 +8,7 @@ def condition?(name) # :nodoc:
private
def association_condition?(name)
!association_condition_details(name).nil?
!association_condition_details(name).nil? unless name.to_s.downcase.match("_or_")
end
def method_missing(name, *args, &block)
@@ -24,20 +24,18 @@ def association_condition_details(name, last_condition = nil)
assocs = reflect_on_all_associations.reject { |assoc| assoc.options[:polymorphic] }.sort { |a, b| b.name.to_s.size <=> a.name.to_s.size }
return nil if assocs.empty?
[nil, last_condition].uniq.map do |condition|
name_with_condition = [name, condition].compact.join('_')
if name_with_condition.to_s =~ /^(#{assocs.collect(&:name).join("|")})_(\w+)$/
association_name = $1
condition = $2
association = reflect_on_association(association_name.to_sym)
klass = association.klass
if klass.condition?(condition)
{:association => $1, :condition => $2}
else
nil
end
name_with_condition = [name, last_condition].compact.join('_')
if name_with_condition.to_s =~ /^(#{assocs.collect(&:name).join("|")})_(\w+)$/
association_name = $1
condition = $2
association = reflect_on_association(association_name.to_sym)
klass = association.klass
if klass.condition?(condition)
{:association => $1, :condition => $2}
else
nil
end
end.detect{ |details| !details.nil? }
end
end
def create_association_condition(association, condition, args)
@@ -101,4 +99,4 @@ def prepare_named_scope_options(options, association)
end
end
end
end
end
@@ -40,4 +40,4 @@ def create_association_ordering_condition(association, order_as, condition, args
end
end
end
end
end
@@ -76,6 +76,12 @@ def method_missing(name, *args, &block)
super
end
end
def find_applied_condition(name)
if name.to_s =~ /(#{(PRIMARY_CONDITIONS + ALIAS_CONDITIONS).join("|")})$/
$1
end
end
def condition_details(name, *args)
if args.size > 0 and !args.first.nil?
@@ -45,4 +45,4 @@ def create_ordering_conditions(column)
end
end
end
end
end
View
@@ -104,14 +104,20 @@
search.username_gt.should == "bjohnson"
end
it "should allow chainging conditions" do
it "should allow chaining conditions" do
user = User.create(:username => "bjohnson", :age => 20)
User.create(:username => "bjohnson", :age => 5)
search = User.search
search.username_equals("bjohnson").age_gt(10)
search.all.should == [user]
end
it "should allow chaining conditions with n-depth associations" do
search = User.search
search.company_conglomerate_name_or_company_conglomerate_description_like("ben")
search.proxy_options.should == User.company_conglomerate_name_or_company_conglomerate_description_like("ben").proxy_options
end
it "should allow setting association conditions" do
search = User.search
search.orders_total_gt = 10

0 comments on commit b2ebf53

Please sign in to comment.