supernova 0.3.6 → 0.3.8

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.6
1
+ 0.3.8
@@ -103,8 +103,9 @@ class Supernova::SolrCriteria < Supernova::Criteria
103
103
  doc
104
104
  end
105
105
 
106
+ # called in build doc, all hashes have strings as keys!!!
106
107
  def convert_doc_attributes(hash)
107
- hash.inject({}) do |ret, (key, value)|
108
+ converted_hash = hash.inject({}) do |ret, (key, value)|
108
109
  if key == "id"
109
110
  ret["id"] = value.to_s.split("/").last
110
111
  else
@@ -112,6 +113,18 @@ class Supernova::SolrCriteria < Supernova::Criteria
112
113
  end
113
114
  ret
114
115
  end
116
+ self.select_fields.each do |select_field|
117
+ converted_hash[select_field.to_s] = nil if !converted_hash.has_key?(select_field.to_s)
118
+ end
119
+ converted_hash
120
+ end
121
+
122
+ def select_fields
123
+ if self.search_options[:select].present?
124
+ self.search_options[:select]
125
+ else
126
+ self.search_options[:named_scope_class].respond_to?(:select_fields) ? self.search_options[:named_scope_class].select_fields : []
127
+ end
115
128
  end
116
129
 
117
130
  def set_first_responding_attribute(doc, solr_key, value)
@@ -11,6 +11,12 @@ class Supernova::SolrIndexer
11
11
  @field_definitions ||= {}
12
12
  end
13
13
 
14
+ def select_fields
15
+ field_definitions.map do |key, attributes|
16
+ attributes[:virtual] != true ? key : nil
17
+ end.compact
18
+ end
19
+
14
20
  def has(key, attributes)
15
21
  field_definitions[key] = attributes.is_a?(Hash) ? attributes : { :type => attributes }
16
22
  end
@@ -255,29 +255,42 @@ describe Supernova::SolrCriteria do
255
255
  end
256
256
 
257
257
  describe "#build_doc" do
258
+ class OfferIndex < Supernova::SolrIndexer
259
+ has :enabled, :type => :boolean
260
+ has :popularity, :type => :integer
261
+ has :is_deleted, :type => :boolean, :virtual => true
262
+ clazz Offer
263
+ end
264
+
265
+
258
266
  { "Offer" => Offer, "Host" => Host }.each do |type, clazz|
259
267
  it "returns the #{clazz} for #{type.inspect}" do
260
268
  criteria.build_doc("type" => type).should be_an_instance_of(clazz)
261
269
  end
262
270
  end
263
271
 
264
- { :popularity => 10, :enabled => false, :id => 1 }.each do |key, value|
265
- it "sets #{key} to #{value}" do
266
- doc = criteria.build_doc("type" => "Offer", "some_other" => "Test", "id" => "offers/1", "enabled" => false, "popularity" => 10)
267
- doc.send(key).should == value
268
- end
272
+ it "calls convert_doc_attributes" do
273
+ row = { "type" => "Offer", "id" => "offers/1" }
274
+ criteria.should_receive(:convert_doc_attributes).with(row).and_return row
275
+ criteria.build_doc(row)
269
276
  end
270
277
 
271
- it "updates the attributes hash when defined" do
272
- atts = { "type" => "Offer", "some_other" => "Test", "id" => "offers/1", "enabled" => false, "popularity" => 10 }
273
- doc = criteria.build_doc(atts)
274
- doc.instance_variable_get("@attributes").should == { "popularity" => 10, "enabled" => false, "id" => "1", "some_other" => "Test"}
275
- end
276
-
277
- it "returns a Hash when type does not response to " do
278
+ it "returns the original hash when no type given" do
278
279
  type = double("type")
280
+ row = { "id" => "offers/1", "type" => type }
279
281
  type.should_receive(:respond_to?).with(:constantize).and_return false
280
- criteria.build_doc("type" => type).should be_an_instance_of(Hash)
282
+ criteria.should_not_receive(:convert_doc_attributes)
283
+ criteria.build_doc(row).should == row
284
+ end
285
+
286
+ it "assigns the attributes returned from convert_doc_attributes to attributes when instance variable exists" do
287
+ str = String.new
288
+ str.instance_variable_set("@attributes", {})
289
+ atts = { :title => "Hello" }
290
+ row = { "type" => "Offer" }
291
+ criteria.should_receive(:convert_doc_attributes).with(row).and_return atts
292
+ doc = criteria.build_doc(row)
293
+ doc.instance_variable_get("@attributes").should == atts
281
294
  end
282
295
 
283
296
  it "sets the original solr_doc" do
@@ -293,25 +306,69 @@ describe Supernova::SolrCriteria do
293
306
  criteria.build_doc(docs.first).should_not be_a_new_record
294
307
  end
295
308
 
296
- it "uses attribute_mapping when defined" do
309
+ it "returns an offer and sets all given parameters" do
297
310
  criteria.attribute_mapping(:enabled => { :type => :boolean }, :popularity => { :type => :integer })
298
311
  doc = criteria.build_doc("type" => "Offer", "id" => "offers/1", "enabled_b" => true, "popularity_i" => 10)
299
312
  doc.should be_an_instance_of(Offer)
300
313
  doc.popularity.should == 10
301
314
  end
302
315
 
316
+ it "sets selected parameters even when nil" do
317
+ doc = criteria.select(:enabled, :popularity).build_doc("type" => "Offer", "id" => "offers/1")
318
+ doc.enabled.should be_nil
319
+ doc.popularity.should be_nil
320
+ end
321
+
322
+ it "it sets parameters to nil when no select given and not present" do
323
+ doc = OfferIndex.search_scope.build_doc("type" => "Offer", "id" => "offers/1")
324
+ doc.should be_an_instance_of(Offer)
325
+ doc.popularity.should be_nil
326
+ end
327
+
328
+ it "does not set virtual parameters to nil" do
329
+ OfferIndex.search_scope.build_doc("type" => "Offer", "id" => "offers/1").attributes.should_not have_key(:is_deleted)
330
+ OfferIndex.search_scope.build_doc("type" => "Offer", "id" => "offers/1").attributes.should_not have_key("is_deleted")
331
+ end
332
+ end
333
+
334
+ describe "#select_fields" do
335
+ it "returns the fields from search_options when defined" do
336
+ criteria.select(:enabled).select_fields.should == [:enabled]
337
+ end
338
+
339
+ it "returns the select_fields from named_search_scope when assigned and responding to" do
340
+ fields = double("fields")
341
+ nsc = double("scope", :select_fields => fields)
342
+ criteria.named_scope_class(nsc)
343
+ criteria.select_fields.should == fields
344
+ end
345
+
346
+ it "returns an empty array by default" do
347
+ criteria.select_fields.should be_empty
348
+ end
349
+ end
350
+
351
+ describe "#convert_doc_attributes" do
352
+ { "popularity" => 10, "enabled" => false, "id" => "1" }.each do |key, value|
353
+ it "sets #{key} to #{value}" do
354
+ criteria.convert_doc_attributes("type" => "Offer", "some_other" => "Test", "id" => "offers/1", "enabled" => false, "popularity" => 10)[key].should == value
355
+ end
356
+ end
357
+
358
+ { "popularity" => 10, "enabled" => true, "id" => "1" }.each do |field, value|
359
+ it "uses sets #{field} to #{value}" do
360
+ criteria.attribute_mapping(:enabled => { :type => :boolean }, :popularity => { :type => :integer })
361
+ criteria.convert_doc_attributes("type" => "Offer", "id" => "offers/1", "enabled_b" => true, "popularity_i" => 10)[field].should == value
362
+ end
363
+ end
364
+
365
+
303
366
  class MongoOffer
304
367
  attr_accessor :id
305
368
  end
306
369
 
307
370
  it "would also work with mongoid ids" do
308
- criteria.build_doc("type" => "MongoOffer", "id" => "offers/4df08c30f3b0a72e7c227a55").id.should == "4df08c30f3b0a72e7c227a55"
309
- end
310
-
311
- it "uses OpenStruct when type is not given" do
312
- doc = criteria.build_doc("id" => "offers/4df08c30f3b0a72e7c227a55")
313
- doc.should be_an_instance_of(Hash)
314
- doc["id"].should == "offers/4df08c30f3b0a72e7c227a55"
371
+ criteria.convert_doc_attributes("type" => "MongoOffer", "id" => "offers/4df08c30f3b0a72e7c227a55")["id"].should == "4df08c30f3b0a72e7c227a55"
315
372
  end
316
373
  end
317
374
 
@@ -406,6 +406,22 @@ describe Supernova::SolrIndexer do
406
406
  end
407
407
  end
408
408
 
409
+ describe "SolrIndexer.select_fields" do
410
+ it "returns the keys of the field definitions" do
411
+ Supernova::SolrIndexer.should_receive(:field_definitions).and_return(
412
+ { :title => { :type => :string }, :popularity => { :type => :integer } }
413
+ )
414
+ Supernova::SolrIndexer.select_fields.map(&:to_s).sort.should == %w(popularity title)
415
+ end
416
+
417
+ it "does not include virtual attributes" do
418
+ Supernova::SolrIndexer.should_receive(:field_definitions).and_return(
419
+ { :title => { :type => :string }, :popularity => { :type => :integer }, :is_deleted => { :virtual => true, :type => :integer } }
420
+ )
421
+ Supernova::SolrIndexer.select_fields.map(&:to_s).sort.should == %w(popularity title)
422
+ end
423
+ end
424
+
409
425
  describe "#method_missing" do
410
426
  it "returns a new supernova criteria" do
411
427
  indexer_clazz.where(:a => 1).should be_an_instance_of(Supernova::SolrCriteria)
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{supernova}
8
- s.version = "0.3.6"
8
+ s.version = "0.3.8"
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"]
12
- s.date = %q{2011-06-13}
12
+ s.date = %q{2011-06-15}
13
13
  s.description = %q{Unified search scopes}
14
14
  s.email = %q{tobias.schwab@dynport.de}
15
15
  s.extra_rdoc_files = [
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: 31
4
+ hash: 3
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 3
9
- - 6
10
- version: 0.3.6
9
+ - 8
10
+ version: 0.3.8
11
11
  platform: ruby
12
12
  authors:
13
13
  - Tobias Schwab
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-06-13 00:00:00 Z
18
+ date: 2011-06-15 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  requirement: &id001 !ruby/object:Gem::Requirement