mongoid 1.0.3 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/HISTORY CHANGED
@@ -1,3 +1,18 @@
1
+ === 1.0.4
2
+ - Modified criteria to use the querying class
3
+ when instantiating documents if there are no
4
+ subclasses.
5
+
6
+ - Floats that are empty strings or nil get
7
+ defaulted to 0.0
8
+
9
+ === 1.0.3
10
+ - Small performance improvements on finders
11
+
12
+ - Float.set allows setting of non-numeric string
13
+ in order for validates_numericality_of to fail
14
+ properly.
15
+
1
16
  === 1.0.2
2
17
  - Named scopes get added functionality:
3
18
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.3
1
+ 1.0.4
@@ -185,7 +185,7 @@ module Mongoid #:nodoc:
185
185
  true
186
186
  ).collect do |docs|
187
187
  docs["group"] = docs["group"].collect do |attrs|
188
- attrs["_type"].constantize.instantiate(attrs)
188
+ instantiate(attrs)
189
189
  end
190
190
  docs
191
191
  end
@@ -234,8 +234,11 @@ module Mongoid #:nodoc:
234
234
  # type: One of :all, :first:, or :last
235
235
  # klass: The class to execute on.
236
236
  def initialize(klass)
237
- @selector = klass.hereditary ? { :_type => { "$in" => klass._types } } : {}
238
- @options, @klass = {}, klass
237
+ @selector, @options, @klass = {}, {}, klass
238
+ if klass.hereditary
239
+ @selector = { :_type => { "$in" => klass._types } }
240
+ @hereditary = true
241
+ end
239
242
  end
240
243
 
241
244
  # Return the last result for the +Criteria+. Essentially does a find_one on
@@ -251,7 +254,7 @@ module Mongoid #:nodoc:
251
254
  sorting = [[:_id, :asc]] unless sorting
252
255
  opts[:sort] = sorting.collect { |option| [ option[0], option[1].invert ] }
253
256
  attributes = @klass.collection.find_one(@selector, opts)
254
- attributes ? attributes["_type"].constantize.instantiate(attributes) : nil
257
+ attributes ? instantiate(attributes) : nil
255
258
  end
256
259
 
257
260
  # Adds a criterion to the +Criteria+ that specifies the maximum number of
@@ -392,7 +395,7 @@ module Mongoid #:nodoc:
392
395
  # <tt>Criteria.select(:name).where(:name = "Chrissy").one</tt>
393
396
  def one
394
397
  attributes = @klass.collection.find_one(@selector, process_options)
395
- attributes ? attributes["_type"].constantize.instantiate(attributes) : nil
398
+ attributes ? instantiate(attributes) : nil
396
399
  end
397
400
 
398
401
  alias :first :one
@@ -565,23 +568,12 @@ module Mongoid #:nodoc:
565
568
  attributes = @klass.collection.find(@selector, process_options)
566
569
  if attributes
567
570
  @count = attributes.count
568
- attributes.collect { |doc| doc["_type"].constantize.instantiate(doc) }
571
+ attributes.collect { |doc| instantiate(doc) }
569
572
  else
570
573
  []
571
574
  end
572
575
  end
573
576
 
574
- # Filters the field list. If no fields have been supplied, then it will be
575
- # empty. If fields have been defined then _type will be included as well.
576
- def process_options
577
- fields = @options[:fields]
578
- if fields && fields.size > 0 && !fields.include?(:_type)
579
- fields << :_type
580
- @options[:fields] = fields
581
- end
582
- @options.dup
583
- end
584
-
585
577
  # Filters the unused options out of the options +Hash+. Currently this
586
578
  # takes into account the "page" and "per_page" options that would be passed
587
579
  # in if using will_paginate.
@@ -607,6 +599,22 @@ module Mongoid #:nodoc:
607
599
  collection.first[start.to_s]
608
600
  end
609
601
 
602
+ # If hereditary instantiate by _type otherwise use the klass.
603
+ def instantiate(attrs)
604
+ @hereditary ? attrs["_type"].constantize.instantiate(attrs) : @klass.instantiate(attrs)
605
+ end
606
+
607
+ # Filters the field list. If no fields have been supplied, then it will be
608
+ # empty. If fields have been defined then _type will be included as well.
609
+ def process_options
610
+ fields = @options[:fields]
611
+ if fields && fields.size > 0 && !fields.include?(:_type)
612
+ fields << :_type
613
+ @options[:fields] = fields
614
+ end
615
+ @options.dup
616
+ end
617
+
610
618
  # Update the selector setting the operator on the value for each key in the
611
619
  # supplied attributes +Hash+.
612
620
  def update_selector(attributes, operator)
@@ -4,6 +4,7 @@ module Mongoid #:nodoc:
4
4
  module Float #:nodoc:
5
5
  module Conversions #:nodoc:
6
6
  def set(value)
7
+ return 0.0 if value.blank?
7
8
  value =~ /\d/ ? value.to_f : value
8
9
  end
9
10
  def get(value)
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mongoid}
8
- s.version = "1.0.3"
8
+ s.version = "1.0.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Durran Jordan"]
@@ -85,6 +85,7 @@ Gem::Specification.new do |s|
85
85
  "spec/integration/mongoid/commands_spec.rb",
86
86
  "spec/integration/mongoid/criteria_spec.rb",
87
87
  "spec/integration/mongoid/document_spec.rb",
88
+ "spec/integration/mongoid/extensions_spec.rb",
88
89
  "spec/integration/mongoid/finders_spec.rb",
89
90
  "spec/integration/mongoid/inheritance_spec.rb",
90
91
  "spec/integration/mongoid/named_scope_spec.rb",
@@ -153,6 +154,7 @@ Gem::Specification.new do |s|
153
154
  "spec/integration/mongoid/commands_spec.rb",
154
155
  "spec/integration/mongoid/criteria_spec.rb",
155
156
  "spec/integration/mongoid/document_spec.rb",
157
+ "spec/integration/mongoid/extensions_spec.rb",
156
158
  "spec/integration/mongoid/finders_spec.rb",
157
159
  "spec/integration/mongoid/inheritance_spec.rb",
158
160
  "spec/integration/mongoid/named_scope_spec.rb",
@@ -84,15 +84,36 @@ describe Mongoid::Associations do
84
84
 
85
85
  context "multiple levels nested" do
86
86
 
87
- before do
88
- @person.phone_numbers.create(:number => "4155551212")
87
+ context "when a has-many to has_one" do
88
+
89
+ before do
90
+ @person.phone_numbers.create(:number => "4155551212")
91
+ end
92
+
93
+ it "persists all the associations properly" do
94
+ from_db = Person.find(@person.id)
95
+ phone = from_db.phone_numbers.first
96
+ phone.create_country_code(:code => 1)
97
+ from_db.phone_numbers.first.country_code.code.should == 1
98
+ end
99
+
89
100
  end
90
101
 
91
- it "persists all the associations properly" do
92
- from_db = Person.find(@person.id)
93
- phone = from_db.phone_numbers.first
94
- phone.create_country_code(:code => 1)
95
- from_db.phone_numbers.first.country_code.code.should == 1
102
+ context "when a has-many to has-many" do
103
+
104
+ before do
105
+ @address = Address.new(:street => "Upper Street")
106
+ @person.addresses << @address
107
+ end
108
+
109
+ it "bubbles the child association up to root" do
110
+ location = Location.new(:name => "Home")
111
+ @address.locations << location
112
+ location.stubs(:valid?).returns(false)
113
+ @person.save
114
+ @person.addresses.first.locations.first.should == location
115
+ end
116
+
96
117
  end
97
118
 
98
119
  end
@@ -0,0 +1,26 @@
1
+ require "spec_helper"
2
+
3
+ describe Mongoid::Extensions do
4
+
5
+ context "setting floating point numbers" do
6
+
7
+ context "when value is an empty string" do
8
+
9
+ let(:person) { Person.new }
10
+
11
+ before do
12
+ Person.validates_numericality_of :blood_alcohol_content, :allow_blank => true
13
+ end
14
+
15
+ after do
16
+ Person.validations.clear
17
+ end
18
+
19
+ it "does not set the value" do
20
+ person.save.should be_true
21
+ end
22
+
23
+ end
24
+ end
25
+
26
+ end
@@ -37,6 +37,7 @@ class Person
37
37
  field :aliases, :type => Array
38
38
  field :map, :type => Hash
39
39
  field :score, :type => Integer
40
+ field :blood_alcohol_content, :type => Float
40
41
 
41
42
  attr_reader :rescored
42
43
 
@@ -149,9 +150,16 @@ class Address
149
150
  field :state
150
151
  field :post_code
151
152
  key :street
153
+ has_many :locations
152
154
  belongs_to :addressable, :inverse_of => :addresses
153
155
  end
154
156
 
157
+ class Location
158
+ include Mongoid::Document
159
+ field :name
160
+ belongs_to :address, :inverse_of => :locations
161
+ end
162
+
155
163
  class Name
156
164
  include Mongoid::Document
157
165
  field :first_name
@@ -4,18 +4,46 @@ describe Mongoid::Extensions::Float::Conversions do
4
4
 
5
5
  describe "#set" do
6
6
 
7
- context "when the string is a number" do
7
+ context "when the value is a number" do
8
8
 
9
- it "converts the string to a float" do
10
- Float.set("3.45").should == 3.45
9
+ it "converts the number to a float" do
10
+ Float.set(3.45).should == 3.45
11
11
  end
12
12
 
13
13
  end
14
14
 
15
15
  context "when the string is not a number" do
16
16
 
17
- it "returns the string" do
18
- Float.set("foo").should == "foo"
17
+ context "when the string is non numerical" do
18
+
19
+ it "returns the string" do
20
+ Float.set("foo").should == "foo"
21
+ end
22
+
23
+ end
24
+
25
+ context "when the string is numerical" do
26
+
27
+ it "returns the float value for the string" do
28
+ Float.set("3.45").should == 3.45
29
+ end
30
+
31
+ end
32
+
33
+ context "when the string is empty" do
34
+
35
+ it "returns 0.0" do
36
+ Float.set("").should == 0.0
37
+ end
38
+
39
+ end
40
+
41
+ context "when the string is nil" do
42
+
43
+ it "returns 0.0" do
44
+ Float.set(nil).should == 0.0
45
+ end
46
+
19
47
  end
20
48
 
21
49
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Durran Jordan
@@ -151,6 +151,7 @@ files:
151
151
  - spec/integration/mongoid/commands_spec.rb
152
152
  - spec/integration/mongoid/criteria_spec.rb
153
153
  - spec/integration/mongoid/document_spec.rb
154
+ - spec/integration/mongoid/extensions_spec.rb
154
155
  - spec/integration/mongoid/finders_spec.rb
155
156
  - spec/integration/mongoid/inheritance_spec.rb
156
157
  - spec/integration/mongoid/named_scope_spec.rb
@@ -241,6 +242,7 @@ test_files:
241
242
  - spec/integration/mongoid/commands_spec.rb
242
243
  - spec/integration/mongoid/criteria_spec.rb
243
244
  - spec/integration/mongoid/document_spec.rb
245
+ - spec/integration/mongoid/extensions_spec.rb
244
246
  - spec/integration/mongoid/finders_spec.rb
245
247
  - spec/integration/mongoid/inheritance_spec.rb
246
248
  - spec/integration/mongoid/named_scope_spec.rb