Skip to content

Commit

Permalink
Passing arrays to group no longer creates multiple groups, but treats…
Browse files Browse the repository at this point in the history
… each element as a condition
  • Loading branch information
binarylogic committed Nov 30, 2008
1 parent 7367882 commit 4554a56
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 41 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.rdoc
@@ -1,3 +1,7 @@
== 1.5.8 released 2008-11-30

* Removed array support for groups, arrays will set conditions, not create multiple groups. If you need to create multiple groups you will have to pass an array with multiple {:group => my_groups} hashes.

== 1.5.7 released 2008-11-30

* Fixed how grouping works, you can now specify "and_group" and "or_group", just like you can with conditions.
Expand Down
16 changes: 7 additions & 9 deletions README.rdoc
Expand Up @@ -239,33 +239,31 @@ Group with a block:
search.conditions.group do |group|
group.first_name_like = "Ben"
end
search.conditions.group do |group|
search.conditions.or_group do |group|
group.last_name_like = "Johnson"
group.group do |sub_group|
sub_group.email_ends_with = "binarylogic.com"
end
end
# => id > 2 AND (first_name like '%Ben%') AND (last_name like '%Johnson%' AND (email like '%binarylogic.com'))
# => id > 2 AND (first_name like '%Ben%') OR (last_name like '%Johnson%' AND (email like '%binarylogic.com'))

Group with a hash:

search = User.new_search(:conditions => [
{:id_gt => 2},
{:group => {:first_name_like => "Ben"}},
{:group => [
{:first_name_like => "Ben"},
[
{:last_name_like => "Johnson"},
{:group => {:email_ends_with => "binarylogic.com"}}
]
{:last_name_like => "Johnson"},
{:group => {:email_ends_with => "binarylogic.com"}}
]}
])
# => id > 2 AND (first_name like '%Ben%') AND (last_name like '%Johnson%' AND (email like '%binarylogic.com'))

I want to end this by saying Searchlog was never meant to replace SQL, name_scopes, etc. If you need to perform complex searching there is nothing wrong with resorting to a named scope or using the traditional search methods. In fact, search logic plays nice with named_scopes anyways, so you can combine the 2 if needed:
I want to end this by saying Searchlogic was never meant to replace SQL, name_scopes, etc. If you need to perform complex searching there is nothing wrong with resorting to a named scope or using the traditional search methods. In fact, search logic plays nice with named_scopes anyways, so you can combine the 2 if needed:

@search = User.my_awesome_scope.another_cool_scope.new_search

The only reason I added this was to allow searchlogic to extend a little further into those advanced searches. If you need to scope conditions by using parenthesis, etc. you should resort to named_scopes or the traditional search method.
You decide when a named scope makes the most sense. If you are creating a named scope that is specific to the search form you are building you should consider adding the conditions right into the form.

== Scoped searching

Expand Down
34 changes: 10 additions & 24 deletions lib/searchlogic/conditions/groups.rb
Expand Up @@ -10,40 +10,26 @@ def self.included(klass)
end

# Creates a new group object to set condition off of. See examples at top of class on how to use this.
def group(conditions = nil, forward_arrays = false, &block)
if conditions.is_a?(Array) && !forward_arrays
group_objects = []
conditions.each { |condition| group_objects << group(condition, true, &block) }
group_objects
else
obj = self.class.new
obj.conditions = conditions unless conditions.nil?
yield obj if block_given?
objects << obj
obj
end
def group(conditions = nil, &block)
obj = self.class.new
obj.conditions = conditions unless conditions.nil?
yield obj if block_given?
objects << obj
obj
end
alias_method :group=, :group

def and_group(*args, &block)
obj = group(*args, &block)
case obj
when Array
obj.each { |o| o.group_any = false }
else
obj
end
obj.explicit_any = false
obj
end
alias_method :and_group=, :and_group

def or_group(*args, &block)
obj = group(*args, &block)
case obj
when Array
obj.each { |o| o.group_any = true }
else
obj
end
obj.explicit_any = true
obj
end
alias_method :or_group=, :or_group

Expand Down
14 changes: 6 additions & 8 deletions test/conditions_tests/groups_test.rb
Expand Up @@ -37,15 +37,13 @@ def test_group_hash
now = Time.now
conditions = Searchlogic::Cache::AccountConditions.new([
{:id_gt => 3},
{:group => {:name_like => "Binary"}},
{:group => [
{:name_like => "Binary"},
[
{:id_gt => 5},
{:group => {
:id_lt => 20,
:created_at_after => now
}}
]
{:id_gt => 5},
{:group => [
{:id_lt => 20},
{:created_at_after => now}
]}
]}
])
assert_equal ["\"accounts\".\"id\" > ? AND (\"accounts\".\"name\" LIKE ?) AND (\"accounts\".\"id\" > ? AND (\"accounts\".\"id\" < ? AND \"accounts\".\"created_at\" > ?))", 3, "%Binary%", 5, 20, now], conditions.sanitize
Expand Down

0 comments on commit 4554a56

Please sign in to comment.