SELECT DISTINCT instead of using group hack, and fix a glitch with grafting

This commit is contained in:
Ernie Miller 2011-04-13 20:15:57 -04:00
parent 77e570cf9c
commit 5c937d628f
5 changed files with 13 additions and 10 deletions

View File

@ -12,7 +12,7 @@ module Ransack
def evaluate(search, opts = {})
viz = Visitor.new
relation = @object.where(viz.accept(search.base)).order(viz.accept(search.sorts))
opts[:distinct] ? relation.group(@klass.arel_table[@klass.primary_key]) : relation
opts[:distinct] ? relation.select("DISTINCT #{@klass.quoted_table_name}.*") : relation
end
def attribute_method?(str, klass = @klass)
@ -98,7 +98,7 @@ module Ransack
end
def join_dependency(relation)
if relation.respond_to?(:join_dependency) # MetaWhere will enable this
if relation.respond_to?(:join_dependency) # Squeel will enable this
relation.join_dependency
else
build_join_dependency(relation)
@ -112,7 +112,7 @@ module Ransack
'string_join'
when Hash, Symbol, Array
'association_join'
when ActiveRecord::Associations::JoinDependency::JoinAssociation
when ::ActiveRecord::Associations::JoinDependency::JoinAssociation
'stashed_join'
when Arel::Nodes::Join
'join_node'

View File

@ -26,7 +26,7 @@ module Ransack
end
def ==(other)
super && reflection.klass == other.reflection.klass
super && active_record == other.active_record
end
# This is a temporary hack. Jon's going to refactor this in AR to

View File

@ -18,11 +18,7 @@ module Ransack
def graft_with_ransack(*associations)
associations.each do |association|
join_associations.detect {|a| association == a} ||
(
association.options[:polymorphic] ?
build_polymorphic(association.reflection.name, association.find_parent_in(self) || join_base, association.join_type, association.reflection.klass) :
build(association.reflection.name, association.find_parent_in(self) || join_base, association.join_type)
)
build_polymorphic(association.reflection.name, association.find_parent_in(self) || join_base, association.join_type, association.reflection.klass)
end
self
end

View File

@ -22,7 +22,7 @@ module Ransack
end
def result(opts = {})
@result ||= @context.evaluate(self, opts)
@context.evaluate(self, opts)
end
def build(params)

View File

@ -123,6 +123,13 @@ module Ransack
where = search.result.where_values.first
where.to_sql.should match /\(\("people"."name" = 'Ernie' OR "children_people"."name" = 'Ernie'\) AND \("people"."name" = 'Bert' OR "children_people"."name" = 'Bert'\)\)/
end
it 'returns distinct records when passed :distinct => true' do
search = Search.new(Person, :o => [{:comments_body_cont => 'e', :articles_comments_body_cont => 'e'}])
search.result.should have(920).items
search.result(:distinct => true).should have(330).items
search.result.all.uniq.should eq search.result(:distinct => true).all
end
end
describe '#sorts=' do