mongoid 0.6.2 → 0.6.3

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.6.2
1
+ 0.6.3
@@ -21,10 +21,6 @@ module Mongoid #:nodoc:
21
21
  @document
22
22
  end
23
23
 
24
- # Delegate equality to the parent document.
25
- def ==(other)
26
- @document == other
27
- end
28
24
  end
29
25
  end
30
26
  end
@@ -1,6 +1,8 @@
1
1
  module Mongoid #:nodoc:
2
2
  module Associations #:nodoc:
3
3
  module Decorator #:nodoc:
4
+ EXTRAS = [:save, :save!, :==]
5
+
4
6
  def self.included(base)
5
7
  base.class_eval do
6
8
  attr_reader :document
@@ -10,7 +12,8 @@ module Mongoid #:nodoc:
10
12
  # since we can ask the class for its methods and get an
11
13
  # accurate list.
12
14
  def decorate!
13
- document.public_methods(false).each do |method|
15
+ meths = document.public_methods(false) + EXTRAS
16
+ meths.each do |method|
14
17
  (class << self; self; end).class_eval do
15
18
  define_method method do |*args|
16
19
  document.send method, *args
@@ -20,10 +20,6 @@ module Mongoid #:nodoc:
20
20
  decorate!
21
21
  end
22
22
 
23
- # Equality delegates to the document.
24
- def ==(other)
25
- @document == other
26
- end
27
23
  end
28
24
  end
29
25
  end
@@ -117,6 +117,11 @@ module Mongoid #:nodoc:
117
117
  before_save :generate_key
118
118
  end
119
119
 
120
+ # Find the last +Document+ in the collection by reverse id
121
+ def last
122
+ find(:first, :conditions => {}, :sort => [[:_id, :asc]])
123
+ end
124
+
120
125
  # Returns the primary key field of the +Document+
121
126
  def primary_key
122
127
  @primary_key
@@ -217,7 +222,7 @@ module Mongoid #:nodoc:
217
222
 
218
223
  # Reloads the +Document+ attributes from the database.
219
224
  def reload
220
- @attributes = HashWithIndifferentAccess.new(collection.find_one(id))
225
+ @attributes = HashWithIndifferentAccess.new(collection.find_one(:_id => id))
221
226
  end
222
227
 
223
228
  # Returns the id of the Document
@@ -252,7 +257,7 @@ module Mongoid #:nodoc:
252
257
  values = primary_key.collect { |key| @attributes[key] }
253
258
  @attributes[:_id] = values.join(" ").parameterize.to_s
254
259
  else
255
- @attributes[:_id] = Mongo::ObjectID.new unless id
260
+ @attributes[:_id] = Mongo::ObjectID.new.to_s unless id
256
261
  end
257
262
  end
258
263
  end
@@ -3,7 +3,7 @@ module Mongoid #:nodoc:
3
3
  module Boolean #:nodoc:
4
4
  module Conversions #:nodoc:
5
5
  def set(value)
6
- value == "true"
6
+ value.to_s == "true"
7
7
  end
8
8
  def get(value)
9
9
  value
@@ -6,7 +6,7 @@ module Mongoid #:nodoc:
6
6
  value.to_time.utc
7
7
  end
8
8
  def get(value)
9
- value.to_date
9
+ value ? value.to_date : value
10
10
  end
11
11
  end
12
12
  end
@@ -19,7 +19,7 @@ module Mongoid #:nodoc:
19
19
 
20
20
  module ClassMethods
21
21
  def set(value)
22
- value.attributes
22
+ value.respond_to?(:attributes) ? value.attributes : value
23
23
  end
24
24
 
25
25
  def get(value)
@@ -26,7 +26,7 @@ module Mongoid #:nodoc:
26
26
  # Used for setting an object in the attributes hash. If nil is provided the
27
27
  # default will get returned if it exists.
28
28
  def set(object)
29
- object ? type.set(object) : default
29
+ object.nil? ? default : type.set(object)
30
30
  end
31
31
 
32
32
  # Used for retrieving the object out of the attributes hash.
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mongoid}
8
- s.version = "0.6.2"
8
+ s.version = "0.6.3"
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"]
@@ -62,7 +62,7 @@ describe Mongoid::Document do
62
62
 
63
63
  it "persists a new record to the database" do
64
64
  person = Person.create(:test => "Test")
65
- person.id.should be_a_kind_of(Mongo::ObjectID)
65
+ person.id.should be_a_kind_of(String)
66
66
  person.attributes[:test].should == "Test"
67
67
  end
68
68
 
@@ -23,7 +23,7 @@ describe Mongoid::Associations::BelongsToAssociation do
23
23
  context "when finding by id" do
24
24
 
25
25
  it "returns the document in the array with that id" do
26
- name = @association.find(Mongo::ObjectID.new)
26
+ name = @association.find(Mongo::ObjectID.new.to_s)
27
27
  name.should == @parent
28
28
  end
29
29
 
@@ -55,9 +55,9 @@ describe Mongoid::Associations::Decorator do
55
55
  @decorated = Decorated.new(@person)
56
56
  end
57
57
 
58
- it "adds all the documents public methods to the class" do
58
+ it "adds all the documents methods to the class" do
59
59
  @decorated.decorate!
60
- @decorated.should respond_to(:title, :terms, :age, :addresses, :name)
60
+ @decorated.should respond_to(:title, :terms, :age, :addresses, :name, :save)
61
61
  end
62
62
 
63
63
  end
@@ -2,6 +2,7 @@ require File.join(File.dirname(__FILE__), "/../../spec_helper.rb")
2
2
 
3
3
  class Person < Mongoid::Document
4
4
  field :title
5
+ field :terms, :type => Boolean, :default => false
5
6
  has_many :addresses
6
7
  has_many :phone_numbers, :class_name => "Phone"
7
8
  has_one :name
@@ -154,4 +154,4 @@ describe Mongoid::Commands do
154
154
 
155
155
  end
156
156
 
157
- end
157
+ end
@@ -195,13 +195,13 @@ describe Mongoid::Criteria do
195
195
  describe "#id" do
196
196
 
197
197
  it "adds the _id query to the selector" do
198
- id = Mongo::ObjectID.new
198
+ id = Mongo::ObjectID.new.to_s
199
199
  @criteria.id(id)
200
200
  @criteria.selector.should == { :_id => id }
201
201
  end
202
202
 
203
203
  it "returns self" do
204
- id = Mongo::ObjectID.new
204
+ id = Mongo::ObjectID.new.to_s
205
205
  @criteria.id(id.to_s).should == @criteria
206
206
  end
207
207
 
@@ -413,7 +413,7 @@ describe Mongoid::Criteria do
413
413
  context "with a single argument" do
414
414
 
415
415
  it "creates a criteria for an object id" do
416
- id = Mongo::ObjectID.new
416
+ id = Mongo::ObjectID.new.to_s
417
417
  criteria = Mongoid::Criteria.translate(id)
418
418
  criteria.selector.should == { :_id => id }
419
419
  end
@@ -208,7 +208,7 @@ describe Mongoid::Document do
208
208
  context "when an id is passed in" do
209
209
 
210
210
  before do
211
- @id = Mongo::ObjectID.new
211
+ @id = Mongo::ObjectID.new.to_s
212
212
  end
213
213
 
214
214
  it "delegates to criteria" do
@@ -344,6 +344,19 @@ describe Mongoid::Document do
344
344
 
345
345
  end
346
346
 
347
+ describe "#last" do
348
+
349
+ before do
350
+ @attributes = { :_id => 1, :title => "Sir" }
351
+ @collection.expects(:find_one).with({}, :sort => [[:_id, :asc]]).returns(@attributes)
352
+ end
353
+
354
+ it "finds the last document by the id" do
355
+ Person.last.should == Person.new(@attributes)
356
+ end
357
+
358
+ end
359
+
347
360
  describe "#new" do
348
361
 
349
362
  context "with no attributes" do
@@ -513,8 +526,8 @@ describe Mongoid::Document do
513
526
 
514
527
  before do
515
528
  @attributes = { "title" => "Herr" }
516
- @person = Person.new(:_id => Mongo::ObjectID.new)
517
- @collection.expects(:find_one).with(@person.id).returns(@attributes)
529
+ @person = Person.new(:_id => Mongo::ObjectID.new.to_s)
530
+ @collection.expects(:find_one).with(:_id => @person.id).returns(@attributes)
518
531
  end
519
532
 
520
533
  it "reloads the object attribtues from the database" do
@@ -536,7 +549,7 @@ describe Mongoid::Document do
536
549
  describe "#to_param" do
537
550
 
538
551
  it "returns the id" do
539
- id = Mongo::ObjectID.new
552
+ id = Mongo::ObjectID.new.to_s
540
553
  Person.new(:_id => id).to_param.should == id.to_s
541
554
  end
542
555
 
@@ -557,6 +570,19 @@ describe Mongoid::Document do
557
570
 
558
571
  end
559
572
 
573
+ context "when field has a default value" do
574
+
575
+ before do
576
+ @person = Person.new
577
+ end
578
+
579
+ it "should allow overwriting of the default value" do
580
+ @person.terms = true
581
+ @person.terms.should be_true
582
+ end
583
+
584
+ end
585
+
560
586
  end
561
587
 
562
588
  describe "#write_attributes" do
@@ -36,8 +36,20 @@ describe Mongoid::Extensions::Date::Conversions do
36
36
 
37
37
  describe "#get" do
38
38
 
39
- it "converts the time back to a date" do
40
- Date.get(@time).should == @time.to_date
39
+ context "when value is nil" do
40
+
41
+ it "returns nil" do
42
+ Date.get(nil).should be_nil
43
+ end
44
+
45
+ end
46
+
47
+ context "when value is not nil" do
48
+
49
+ it "converts the time back to a date" do
50
+ Date.get(@time).should == @time.to_date
51
+ end
52
+
41
53
  end
42
54
 
43
55
  end
@@ -30,13 +30,17 @@ describe Mongoid::Extensions::Object::Conversions do
30
30
 
31
31
  describe "#set" do
32
32
 
33
- before do
34
- @attributes = { "_id" => "test", "title" => "Sir", "age" => 100 }
35
- @person = Person.new(@attributes)
36
- end
33
+ context "when object has attributes" do
34
+
35
+ before do
36
+ @attributes = { "_id" => "test", "title" => "Sir", "age" => 100 }
37
+ @person = Person.new(@attributes)
38
+ end
39
+
40
+ it "converts the object to a hash" do
41
+ Person.set(@person).should == @attributes
42
+ end
37
43
 
38
- it "converts the object to a hash" do
39
- Person.set(@person).should == @attributes
40
44
  end
41
45
 
42
46
  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: 0.6.2
4
+ version: 0.6.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Durran Jordan