SELECT DISTINCT instead of using group hack, and fix a glitch with grafting
This commit is contained in:
parent
77e570cf9c
commit
5c937d628f
|
@ -12,7 +12,7 @@ module Ransack
|
||||||
def evaluate(search, opts = {})
|
def evaluate(search, opts = {})
|
||||||
viz = Visitor.new
|
viz = Visitor.new
|
||||||
relation = @object.where(viz.accept(search.base)).order(viz.accept(search.sorts))
|
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
|
end
|
||||||
|
|
||||||
def attribute_method?(str, klass = @klass)
|
def attribute_method?(str, klass = @klass)
|
||||||
|
@ -98,7 +98,7 @@ module Ransack
|
||||||
end
|
end
|
||||||
|
|
||||||
def join_dependency(relation)
|
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
|
relation.join_dependency
|
||||||
else
|
else
|
||||||
build_join_dependency(relation)
|
build_join_dependency(relation)
|
||||||
|
@ -112,7 +112,7 @@ module Ransack
|
||||||
'string_join'
|
'string_join'
|
||||||
when Hash, Symbol, Array
|
when Hash, Symbol, Array
|
||||||
'association_join'
|
'association_join'
|
||||||
when ActiveRecord::Associations::JoinDependency::JoinAssociation
|
when ::ActiveRecord::Associations::JoinDependency::JoinAssociation
|
||||||
'stashed_join'
|
'stashed_join'
|
||||||
when Arel::Nodes::Join
|
when Arel::Nodes::Join
|
||||||
'join_node'
|
'join_node'
|
||||||
|
|
|
@ -26,7 +26,7 @@ module Ransack
|
||||||
end
|
end
|
||||||
|
|
||||||
def ==(other)
|
def ==(other)
|
||||||
super && reflection.klass == other.reflection.klass
|
super && active_record == other.active_record
|
||||||
end
|
end
|
||||||
|
|
||||||
# This is a temporary hack. Jon's going to refactor this in AR to
|
# This is a temporary hack. Jon's going to refactor this in AR to
|
||||||
|
|
|
@ -18,11 +18,7 @@ module Ransack
|
||||||
def graft_with_ransack(*associations)
|
def graft_with_ransack(*associations)
|
||||||
associations.each do |association|
|
associations.each do |association|
|
||||||
join_associations.detect {|a| association == a} ||
|
join_associations.detect {|a| association == a} ||
|
||||||
(
|
build_polymorphic(association.reflection.name, association.find_parent_in(self) || join_base, association.join_type, association.reflection.klass)
|
||||||
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)
|
|
||||||
)
|
|
||||||
end
|
end
|
||||||
self
|
self
|
||||||
end
|
end
|
||||||
|
|
|
@ -22,7 +22,7 @@ module Ransack
|
||||||
end
|
end
|
||||||
|
|
||||||
def result(opts = {})
|
def result(opts = {})
|
||||||
@result ||= @context.evaluate(self, opts)
|
@context.evaluate(self, opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
def build(params)
|
def build(params)
|
||||||
|
|
|
@ -123,6 +123,13 @@ module Ransack
|
||||||
where = search.result.where_values.first
|
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'\)\)/
|
where.to_sql.should match /\(\("people"."name" = 'Ernie' OR "children_people"."name" = 'Ernie'\) AND \("people"."name" = 'Bert' OR "children_people"."name" = 'Bert'\)\)/
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
describe '#sorts=' do
|
describe '#sorts=' do
|
||||||
|
|
Loading…
Reference in New Issue