Skip to content

Commit

Permalink
Remove duplicates regardless of where searchgasm is used
Browse files Browse the repository at this point in the history
  • Loading branch information
binarylogic committed Oct 15, 2008
1 parent 4fb5152 commit 0f1c1ab
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 21 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.rdoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
== 1.4.1 released 2008-10-08

* Extracted english text into configuration to support alternate languages.
* Extracted english text into configuration to support alternate languages. I18n support.

== 1.4.0 released 2008-10-08

Expand Down
22 changes: 6 additions & 16 deletions lib/searchgasm/search/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,24 +83,9 @@ def inspect
# Merges all joins together, including the scopes joins for
def joins
all_joins = (safe_to_array(conditions.auto_joins) + safe_to_array(order_by_auto_joins) + safe_to_array(priority_order_by_auto_joins) + safe_to_array(@joins)).uniq
# For partial backwards compatibility, delete if the scope contains conflicts, AR 2.2 does this for you
scope_joins = safe_to_array(scope && scope[:joins])
all_joins.delete_if { |j| scope_joins.include?(j) } unless scope_joins.blank?
all_joins.size <= 1 ? all_joins.first : all_joins
end
=begin
def joins
auto_joins = joins_to_sql_array((safe_to_array(conditions.auto_joins) + safe_to_array(order_by_auto_joins) + safe_to_array(priority_order_by_auto_joins)).uniq)
scope_joins = joins_to_sql_array(scope && scope[:joins])
include_joins = joins_to_sql_array(include)
#raise auto_joins.inspect if conditions.respond_to?(:dogs) && conditions.dogs.id == 1
auto_joins.delete_if { |auto_join| scope_joins.include?(auto_join) || include_joins.include?(auto_join) }
#raise auto_joins.inspect if auto_joins.size > 1
end
=end

def limit=(value)
@set_limit = true
@limit = value.blank? || value == 0 ? nil : value.to_i
Expand Down Expand Up @@ -134,6 +119,11 @@ def sanitize(searching = true)
find_options
end

def select
@select ||= "DISTINCT #{klass.connection.quote_table_name(klass.table_name)}.*" if !joins.blank? && Config.remove_duplicates?
@select
end

def scope
@scope ||= {}
end
Expand Down
4 changes: 1 addition & 3 deletions lib/searchgasm/search/searching.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ def #{method}(*args)
args[0] = klass.primary_key if [nil, :all].include?(args[0])
end
args << options
result = klass.#{method}(*args)
result.uniq! if result.is_a?(Array) && Config.remove_duplicates?
result
klass.#{method}(*args)
end
end
end_eval
Expand Down
2 changes: 1 addition & 1 deletion test/test_search_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def test_sanitize
search.conditions.users.id_greater_than = 2
search.page = 3
search.readonly = true
assert_equal({:joins => :users, :offset => 4, :readonly => true, :conditions => ["(\"accounts\".\"name\" LIKE ?) AND (\"users\".\"id\" > ?)", "%Binary%", 2], :limit => 2 }, search.sanitize)
assert_equal({:joins => :users, :offset => 4, :select => "DISTINCT \"accounts\".*", :readonly => true, :conditions => ["(\"accounts\".\"name\" LIKE ?) AND (\"users\".\"id\" > ?)", "%Binary%", 2], :limit => 2 }, search.sanitize)
end

def test_scope
Expand Down

0 comments on commit 0f1c1ab

Please sign in to comment.