Skip to content

Commit

Permalink
Merge pull request sunspot#334 from culturecode/empty-array-support
Browse files Browse the repository at this point in the history
Added support for empty arrays in restrictions.
  • Loading branch information
brutuscat committed Mar 3, 2013
2 parents ab52e09 + a2fc2c3 commit 96e9673
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 2 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,25 @@ end
# All examples in "positive" also work negated using `without`
```

#### Empty Restrictions

```ruby
# Passing an empty array is equivalent to a no-op, allowing you to replace this...
Post.search do
with(:category_ids, id_list) if id_list.present?
end

# ...with this
Post.search do
with(:category_ids, id_list)
end
```


# All examples in "positive" also work negated using `without`
```
#### Disjunctions and Conjunctions
```ruby
Expand Down
29 changes: 27 additions & 2 deletions sunspot/lib/sunspot/query/restriction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -273,10 +273,23 @@ def to_solr_conditional
# Results must have field with value included in given collection
#
class AnyOf < Base

def negated?
if @value.empty?
false
else
super
end
end

private

def to_solr_conditional
"(#{@value.map { |v| solr_value v } * ' OR '})"
if @value.empty?
"[* TO *]"
else
"(#{@value.map { |v| solr_value v } * ' OR '})"
end
end
end

Expand All @@ -285,10 +298,22 @@ def to_solr_conditional
# collection (only makes sense for fields with multiple values)
#
class AllOf < Base
def negated?
if @value.empty?
false
else
super
end
end

private

def to_solr_conditional
"(#{@value.map { |v| solr_value v } * ' AND '})"
if @value.empty?
"[* TO *]"
else
"(#{@value.map { |v| solr_value v } * ' AND '})"
end
end
end

Expand Down
38 changes: 38 additions & 0 deletions sunspot/spec/integration/scoped_search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,44 @@ def self.test_field_type(name, attribute, field, *values)
end.results.should == posts[0..1]
end

it 'should return results, ignoring any restriction in a disjunction that has been passed an empty array' do
posts = (1..3).map { |i| Post.new(:blog_id => i)}
Sunspot.index!(posts)
Sunspot.search(Post) do
with(:blog_id, [])
end.results.should == posts
end

it 'should return results, ignoring any restriction in a negative disjunction that has been passed an empty array' do
posts = (1..3).map { |i| Post.new(:blog_id => i)}
Sunspot.index!(posts)
Sunspot.search(Post) do
without(:blog_id, [])
end.results.should == posts
end

it 'should return results, ignoring any restriction in a conjunction that has been passed an empty array' do
posts = (1..3).map { |i| Post.new(:blog_id => i)}
Sunspot.index!(posts)
Sunspot.search(Post) do
all_of do
with(:blog_id, 1)
with(:blog_id, [])
end
end.results.should == posts[0..0]
end

it 'should return results, ignoring any restriction in a negative conjunction that has been passed an empty array' do
posts = (1..3).map { |i| Post.new(:blog_id => i)}
Sunspot.index!(posts)
Sunspot.search(Post) do
all_of do
with(:blog_id, 1)
without(:blog_id, [])
end
end.results.should == posts[0..0]
end

it 'should return results that match a nested conjunction in a disjunction' do
posts = [
Post.new(:title => 'No', :blog_id => 1),
Expand Down

0 comments on commit 96e9673

Please sign in to comment.