supernova 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.2.1
@@ -48,6 +48,15 @@ class Supernova::Criteria
48
48
  def with(filters)
49
49
  merge_filters :with, filters
50
50
  end
51
+
52
+ def without(filters)
53
+ self.filters[:without] ||= Hash.new
54
+ filters.each do |key, value|
55
+ self.filters[:without][key] ||= Array.new
56
+ self.filters[:without][key] << value if !self.filters[:without][key].include?(value)
57
+ end
58
+ self
59
+ end
51
60
 
52
61
  def select(fields)
53
62
  merge_search_options :select, fields
@@ -4,6 +4,11 @@ class Supernova::SolrCriteria < Supernova::Criteria
4
4
  def to_params
5
5
  solr_options = { :fq => [], :q => "*:*" }
6
6
  solr_options[:fq] += self.filters[:with].map { |key, value| "#{key}:#{value}" } if self.filters[:with]
7
+ if self.filters[:without]
8
+ self.filters[:without].each do |key, values|
9
+ solr_options[:fq] += values.map { |value| "!#{key}:#{value}" }
10
+ end
11
+ end
7
12
  solr_options[:sort] = self.search_options[:order] if self.search_options[:order]
8
13
  solr_options[:q] = self.filters[:search] if self.filters[:search]
9
14
 
@@ -15,7 +15,7 @@ class Supernova::ThinkingSphinxCriteria < Supernova::Criteria
15
15
  end
16
16
 
17
17
  def to_params
18
- sphinx_options = { :match_mode => :boolean, :with => {}, :conditions => {} }
18
+ sphinx_options = { :match_mode => :boolean, :with => {}, :conditions => {}, :without => {} }
19
19
  sphinx_options[:order] = self.search_options[:order] if self.search_options[:order]
20
20
  sphinx_options[:limit] = self.search_options[:limit] if self.search_options[:limit]
21
21
  sphinx_options[:select] = self.search_options[:select] if self.search_options[:select]
@@ -25,6 +25,7 @@ class Supernova::ThinkingSphinxCriteria < Supernova::Criteria
25
25
  sphinx_options[:classes] = [self.clazz] if self.clazz
26
26
  sphinx_options[:conditions].merge!(self.filters[:conditions]) if self.filters[:conditions]
27
27
  sphinx_options[:with].merge!(normalize_with_filter(self.filters[:with])) if self.filters[:with]
28
+ sphinx_options[:without].merge!(normalize_with_filter(self.filters[:without])) if self.filters[:without]
28
29
  sphinx_options.merge!(self.search_options[:custom_options]) if self.search_options[:custom_options]
29
30
  if self.search_options[:geo_center] && self.search_options[:geo_distance]
30
31
  sphinx_options[:geo] = [self.search_options[:geo_center][:lat].to_radians, self.search_options[:geo_center][:lng].to_radians]
@@ -68,6 +68,12 @@ describe "Solr" do
68
68
  new_criteria.with(:user_id => 1, :enabled => true).to_a.total_entries.should == 0
69
69
  end
70
70
 
71
+ it "uses without correctly" do
72
+ new_criteria.without(:user_id => 1).to_a.map(&:id).should == [2]
73
+ new_criteria.without(:user_id => 2).to_a.map(&:id).should == [1]
74
+ new_criteria.without(:user_id => 2).without(:user_id => 1).to_a.map(&:id).should == []
75
+ end
76
+
71
77
  it "uses the correct orders" do
72
78
  new_criteria.order("id desc").to_a.map(&:id).should == [2, 1]
73
79
  new_criteria.order("id asc").to_a.map(&:id).should == [1, 2]
@@ -31,6 +31,12 @@ describe "ThinkingSphinx" do
31
31
  Offer.for_user_ids(2, 1).to_a.to_a.sort_by(&:id) == [@offer1, @offer2]
32
32
  end
33
33
 
34
+ it "correctly filters out unwanted records" do
35
+ Offer.search_scope.without(:user_id => 2).to_a.to_a.sort_by(&:id).should == [@offer1]
36
+ Offer.search_scope.without(:user_id => 1).to_a.to_a.sort_by(&:id).should == [@offer2]
37
+ Offer.search_scope.without(:user_id => 1).without(:user_id => 2).to_a.to_a.sort_by(&:id).should == []
38
+ end
39
+
34
40
  it "returns the corect ids" do
35
41
  Offer.for_user_ids(2).ids.to_a.to_a.should == [2]
36
42
  end
@@ -26,7 +26,8 @@ describe "Supernova::Criteria" do
26
26
  [:select, %w(stars)],
27
27
  [:near, "test"],
28
28
  [:within, 10],
29
- [:options, {}]
29
+ [:options, {}],
30
+ [:without, {}]
30
31
  ].each do |args|
31
32
  it "returns the scope itself for #{args.first}" do
32
33
  scope.send(*args).should == scope
@@ -92,6 +93,16 @@ describe "Supernova::Criteria" do
92
93
  scope.paginate(:page => 9, :per_page => 2).search_options[:pagination].should == { :page => 9, :per_page => 2 }
93
94
  end
94
95
 
96
+ describe "#without" do
97
+ it "sets the correct without filter" do
98
+ scope.without(:user_id => 1).filters[:without].should == { :user_id => [1] }
99
+ end
100
+
101
+ it "combines multiple without filters" do
102
+ scope.without(:user_id => 1).without(:user_id => 1).without(:user_id => 2).filters[:without].should == { :user_id => [1, 2] }
103
+ end
104
+ end
105
+
95
106
  it "to_parameters raises an implement in subclass error" do
96
107
  lambda {
97
108
  scope.to_parameters
@@ -48,6 +48,11 @@ describe Supernova::SolrCriteria do
48
48
  criteria.to_params[:fq].should == []
49
49
  end
50
50
 
51
+ it "adds all without filters" do
52
+ criteria.without(:user_id => 1).to_params[:fq].should == ["!user_id:1"]
53
+ criteria.without(:user_id => 1).without(:user_id => 1).without(:user_id => 2).to_params[:fq].sort.should == ["!user_id:1", "!user_id:2"]
54
+ end
55
+
51
56
  describe "with a nearby search" do
52
57
  let(:nearby_criteria) { Supernova::SolrCriteria.new.near(47, 11).within(10.kms) }
53
58
 
@@ -40,6 +40,11 @@ describe "Supernova::ThinkingSphinxCriteria" do
40
40
  scope.limit(88).to_params.at(1)[:limit].should == 88
41
41
  end
42
42
 
43
+ it "sets the correct without parameters" do
44
+ scope.without(:user_id => 9).to_params.at(1)[:without].should == { :user_id => [9] }
45
+ scope.without(:user_id => 9).without(:user_id => 9).without(:user_id => 1).to_params.at(1)[:without].should == { :user_id => [9, 1] }
46
+ end
47
+
43
48
  it "calls sphinx with select fields" do
44
49
  scope.select(%w(id title name)).to_params.at(1)[:select].should == %w(id title name)
45
50
  end
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{supernova}
8
- s.version = "0.2.0"
8
+ s.version = "0.2.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Tobias Schwab"]
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: supernova
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 21
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 0
10
- version: 0.2.0
9
+ - 1
10
+ version: 0.2.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Tobias Schwab