Skip to content
This repository
Browse code

Support chaining before and after select() calls

  • Loading branch information...
commit b75cc50b7d496a0d04a257d26a7ef4d581a218a4 1 parent 8cdbcef
authored October 24, 2012
19  lib/pg_search/scope_options.rb
@@ -11,7 +11,24 @@ def initialize(config)
11 11
     end
12 12
 
13 13
     def apply(scope)
14  
-      scope.select("#{quoted_table_name}.*, (#{rank}) AS pg_search_rank").where(conditions).order("pg_search_rank DESC, #{order_within_rank}").joins(joins)
  14
+      base_scope = scope.select_values.empty?
  15
+
  16
+      if base_scope
  17
+        all_columns = "#{quoted_table_name}.*"
  18
+
  19
+        custom_select = Module.new do
  20
+          define_method :select do |*args, &block|
  21
+            self.select_values -= [all_columns]
  22
+            super(*args, &block)
  23
+          end
  24
+        end
  25
+      end
  26
+
  27
+      scope = scope.select("(#{rank}) AS pg_search_rank")
  28
+      scope = scope.select(all_columns).extend(custom_select) if base_scope
  29
+      scope = scope.where(conditions).order("pg_search_rank DESC, #{order_within_rank}").joins(joins)
  30
+
  31
+      scope
15 32
     end
16 33
 
17 34
     private
51  spec/pg_search_spec.rb
@@ -136,6 +136,57 @@
136 136
         ModelWithPgSearch.pg_search_scope :search_content, :against => :content
137 137
       end
138 138
 
  139
+      context "when chained after a select() scope" do
  140
+        it "honors the select" do
  141
+          included = ModelWithPgSearch.create!(content: 'foo', title: 'bar')
  142
+          excluded = ModelWithPgSearch.create!(content: 'bar', title: 'foo')
  143
+
  144
+          results = ModelWithPgSearch.select('id, title').search_content('foo')
  145
+
  146
+          results.should include(included)
  147
+          results.should_not include(excluded)
  148
+
  149
+          results.first.attributes.key?('content').should be_false
  150
+
  151
+          results.select { |record| record.title == "bar" }.should == [included]
  152
+          results.select { |record| record.title != "bar" }.should be_empty
  153
+        end
  154
+      end
  155
+
  156
+      context "when chained before a select() scope" do
  157
+        it "honors the select" do
  158
+          included = ModelWithPgSearch.create!(content: 'foo', title: 'bar')
  159
+          excluded = ModelWithPgSearch.create!(content: 'bar', title: 'foo')
  160
+
  161
+          results = ModelWithPgSearch.search_content('foo').select('id, title')
  162
+
  163
+          results.should include(included)
  164
+          results.should_not include(excluded)
  165
+
  166
+          results.first.attributes.key?('content').should be_false
  167
+
  168
+          results.select { |record| record.title == "bar" }.should == [included]
  169
+          results.select { |record| record.title != "bar" }.should be_empty
  170
+        end
  171
+      end
  172
+
  173
+      context "when surrouned by select() scopes" do
  174
+        it "honors the select" do
  175
+          included = ModelWithPgSearch.create!(content: 'foo', title: 'bar')
  176
+          excluded = ModelWithPgSearch.create!(content: 'bar', title: 'foo')
  177
+
  178
+          results = ModelWithPgSearch.select('id').search_content('foo').select('title')
  179
+
  180
+          results.should include(included)
  181
+          results.should_not include(excluded)
  182
+
  183
+          results.first.attributes.key?('content').should be_false
  184
+
  185
+          results.select { |record| record.title == "bar" }.should == [included]
  186
+          results.select { |record| record.title != "bar" }.should be_empty
  187
+        end
  188
+      end
  189
+
139 190
       it "returns an empty array when a blank query is passed in" do
140 191
         ModelWithPgSearch.create!(:content => 'foo')
141 192
 

0 notes on commit b75cc50

Please sign in to comment.
Something went wrong with that request. Please try again.