From c156fa4a7ac6e1a8d730791c49bf4403aa0f3af7 Mon Sep 17 00:00:00 2001 From: Ernie Miller Date: Thu, 4 Aug 2011 17:10:33 -0400 Subject: [PATCH] Don't let _ be contained in polymorphic class. Fixes #12 --- lib/ransack/adapters/active_record/3.0/context.rb | 2 +- lib/ransack/adapters/active_record/context.rb | 2 +- lib/ransack/context.rb | 2 +- spec/ransack/search_spec.rb | 10 ++++++++++ 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/ransack/adapters/active_record/3.0/context.rb b/lib/ransack/adapters/active_record/3.0/context.rb index c98d2e8..87d0aa8 100644 --- a/lib/ransack/adapters/active_record/3.0/context.rb +++ b/lib/ransack/adapters/active_record/3.0/context.rb @@ -135,7 +135,7 @@ module Ransack found_association = @join_dependency.join_associations.detect do |assoc| assoc.reflection.name == name && assoc.parent == parent && - (!klass || assoc.klass == klass) + (!klass || assoc.reflection.klass == klass) end unless found_association @join_dependency.send(:build_polymorphic, name.to_sym, parent, Arel::Nodes::OuterJoin, klass) diff --git a/lib/ransack/adapters/active_record/context.rb b/lib/ransack/adapters/active_record/context.rb index d44ca33..36ebde2 100644 --- a/lib/ransack/adapters/active_record/context.rb +++ b/lib/ransack/adapters/active_record/context.rb @@ -140,7 +140,7 @@ module Ransack found_association = @join_dependency.join_associations.detect do |assoc| assoc.reflection.name == name && assoc.parent == parent && - (!klass || assoc.klass == klass) + (!klass || assoc.reflection.klass == klass) end unless found_association @join_dependency.send(:build_polymorphic, name.to_sym, parent, Arel::Nodes::OuterJoin, klass) diff --git a/lib/ransack/context.rb b/lib/ransack/context.rb index 6db53fd..05f1151 100644 --- a/lib/ransack/context.rb +++ b/lib/ransack/context.rb @@ -95,7 +95,7 @@ module Ransack end def unpolymorphize_association(str) - if (match = str.match(/_of_(.+?)_type$/)) + if (match = str.match(/_of_([^_]+?)_type$/)) [match.pre_match, Kernel.const_get(match.captures.first)] else [str, nil] diff --git a/spec/ransack/search_spec.rb b/spec/ransack/search_spec.rb index ede0307..3eb0d8c 100644 --- a/spec/ransack/search_spec.rb +++ b/spec/ransack/search_spec.rb @@ -31,6 +31,16 @@ module Ransack condition.value.should eq 'Ernie' end + it 'creates Conditions for multiple polymorphic belongs_to association attributes' do + search = Search.new(Note, :notable_of_Person_type_name_or_notable_of_Article_type_title_eq => 'Ernie') + condition = search.base[:notable_of_Person_type_name_or_notable_of_Article_type_title_eq] + condition.should be_a Nodes::Condition + condition.predicate.name.should eq 'eq' + condition.attributes.first.name.should eq 'notable_of_Person_type_name' + condition.attributes.last.name.should eq 'notable_of_Article_type_title' + condition.value.should eq 'Ernie' + end + it 'discards empty conditions' do search = Search.new(Person, :children_name_eq => '') condition = search.base[:children_name_eq]