Don't let _ be contained in polymorphic class. Fixes #12

This commit is contained in:
Ernie Miller 2011-08-04 17:10:33 -04:00
parent 119d470304
commit c156fa4a7a
4 changed files with 13 additions and 3 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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]

View File

@ -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]