Skip to content

Commit

Permalink
Don't let _ be contained in polymorphic class. Fixes #12
Browse files Browse the repository at this point in the history
  • Loading branch information
Ernie Miller committed Aug 4, 2011
1 parent 119d470 commit c156fa4
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/ransack/adapters/active_record/3.0/context.rb
Expand Up @@ -135,7 +135,7 @@ def build_or_find_association(name, parent = @base, klass = nil)
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)
Expand Down
2 changes: 1 addition & 1 deletion lib/ransack/adapters/active_record/context.rb
Expand Up @@ -140,7 +140,7 @@ def build_or_find_association(name, parent = @base, klass = nil)
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)
Expand Down
2 changes: 1 addition & 1 deletion lib/ransack/context.rb
Expand Up @@ -95,7 +95,7 @@ def association_path(str, base = @base)
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]
Expand Down
10 changes: 10 additions & 0 deletions spec/ransack/search_spec.rb
Expand Up @@ -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]
Expand Down

0 comments on commit c156fa4

Please sign in to comment.