mongoid 0.3.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Rakefile CHANGED
@@ -12,12 +12,12 @@ begin
12
12
  gem.email = "durran@gmail.com"
13
13
  gem.homepage = "http://github.com/durran/mongoid"
14
14
  gem.authors = ["Durran Jordan"]
15
- gem.add_dependency "durran-validatable"
16
- gem.add_dependency "mislav-will_paginate"
17
- gem.add_dependency "activesupport"
18
- gem.add_dependency "mongodb-mongo"
15
+ gem.add_dependency("durran-validatable", "1.7.5")
16
+ gem.add_dependency("mislav-will_paginate", "2.3.11")
17
+ gem.add_dependency("activesupport", "2.3.4")
18
+ gem.add_dependency("mongodb-mongo", "0.14.1")
19
19
  end
20
-
20
+ Jeweler::GemcutterTasks.new
21
21
  rescue LoadError
22
22
  puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
23
23
  end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.0
1
+ 0.3.1
@@ -19,7 +19,8 @@ module Mongoid #:nodoc:
19
19
  # Create an association to a parent Document.
20
20
  # Get an aggregate count for the supplied group of fields and the
21
21
  # selector that is provided.
22
- def aggregate(fields, selector)
22
+ def aggregate(fields, params = {})
23
+ selector = params[:conditions]
23
24
  collection.group(fields, selector, { :count => 0 }, AGGREGATE_REDUCE)
24
25
  end
25
26
 
@@ -48,25 +49,28 @@ module Mongoid #:nodoc:
48
49
  # Model.find(:first, :attribute => "value")
49
50
  # Model.find(:all, :attribute => "value")
50
51
  def find(*args)
51
- type, selector = args[0], args[1]
52
- conditions = selector[:conditions] if selector
52
+ type, params = args[0], args[1]
53
53
  case type
54
- when :all then find_all(conditions)
55
- when :first then find_first(conditions)
54
+ when :all then find_all(params)
55
+ when :first then find_first(params)
56
56
  else find_first(Mongo::ObjectID.from_string(type.to_s))
57
57
  end
58
58
  end
59
59
 
60
60
  # Find a single Document given the passed selector, which is a Hash of attributes that
61
61
  # must match the Document in the database exactly.
62
- def find_first(selector = nil)
63
- new(collection.find_one(selector))
62
+ def find_first(params = {})
63
+ case params
64
+ when Hash then new(collection.find_one(params[:conditions]))
65
+ else new(collection.find_one(params))
66
+ end
64
67
  end
65
68
 
66
69
  # Find all Documents given the passed selector, which is a Hash of attributes that
67
70
  # must match the Document in the database exactly.
68
- def find_all(selector = nil)
69
- collection.find(selector).collect { |doc| new(doc) }
71
+ def find_all(params = {})
72
+ selector = params.delete(:conditions)
73
+ collection.find(selector, params).collect { |doc| new(doc) }
70
74
  end
71
75
 
72
76
  # Defines all the fields that are accessable on the Document
@@ -83,7 +87,8 @@ module Mongoid #:nodoc:
83
87
 
84
88
  # Find all Documents given the supplied criteria, grouped by the fields
85
89
  # provided.
86
- def group_by(fields, selector)
90
+ def group_by(fields, params = {})
91
+ selector = params[:condition]
87
92
  collection.group(fields, selector, { :group => [] }, GROUP_BY_REDUCE).collect do |docs|
88
93
  docs["group"] = docs["group"].collect { |attrs| new(attrs) }; docs
89
94
  end
@@ -102,7 +107,7 @@ module Mongoid #:nodoc:
102
107
  # Adds timestamps on the Document in the form of the fields 'created_on'
103
108
  # and 'last_modified'
104
109
  def has_timestamps
105
- fields :created_on, :last_modified
110
+ fields :created_at, :last_modified
106
111
  class_eval do
107
112
  before_save :update_timestamps
108
113
  end
@@ -117,11 +122,12 @@ module Mongoid #:nodoc:
117
122
  # Find all documents in paginated fashion given the supplied arguments.
118
123
  # If no parameters are passed just default to offset 0 and limit 20.
119
124
  def paginate(params = {})
125
+ selector = params[:conditions]
120
126
  WillPaginate::Collection.create(
121
127
  params[:page] || 1,
122
128
  params[:per_page] || 20,
123
129
  0) do |pager|
124
- results = collection.find(params[:conditions], { :limit => pager.per_page, :offset => pager.offset })
130
+ results = collection.find(selector, { :limit => pager.per_page, :offset => pager.offset })
125
131
  pager.total_entries = results.count
126
132
  pager.replace(results.collect { |doc| new(doc) })
127
133
  end
@@ -189,11 +195,6 @@ module Mongoid #:nodoc:
189
195
  @attributes = attributes.symbolize_keys!; save; true
190
196
  end
191
197
 
192
- def update_timestamps
193
- self.created_on = Time.now
194
- self.last_modified = Time.now
195
- end
196
-
197
198
  private
198
199
 
199
200
  class << self
@@ -216,6 +217,12 @@ module Mongoid #:nodoc:
216
217
  @attributes[name.to_sym]
217
218
  end
218
219
 
220
+ # Update the timestamps on this document.
221
+ def update_timestamps
222
+ self.created_at = Time.now
223
+ self.last_modified = Time.now
224
+ end
225
+
219
226
  # Write to the attributes hash.
220
227
  def write_attribute(name, value)
221
228
  @attributes[name.to_sym] = value
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mongoid}
8
- s.version = "0.3.0"
8
+ s.version = "0.3.1"
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"]
@@ -67,20 +67,20 @@ Gem::Specification.new do |s|
67
67
  s.specification_version = 3
68
68
 
69
69
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
70
- s.add_runtime_dependency(%q<durran-validatable>, [">= 0"])
71
- s.add_runtime_dependency(%q<mislav-will_paginate>, [">= 0"])
72
- s.add_runtime_dependency(%q<activesupport>, [">= 0"])
73
- s.add_runtime_dependency(%q<mongodb-mongo>, [">= 0"])
70
+ s.add_runtime_dependency(%q<durran-validatable>, ["= 1.7.5"])
71
+ s.add_runtime_dependency(%q<mislav-will_paginate>, ["= 2.3.11"])
72
+ s.add_runtime_dependency(%q<activesupport>, ["= 2.3.4"])
73
+ s.add_runtime_dependency(%q<mongodb-mongo>, ["= 0.14.1"])
74
74
  else
75
- s.add_dependency(%q<durran-validatable>, [">= 0"])
76
- s.add_dependency(%q<mislav-will_paginate>, [">= 0"])
77
- s.add_dependency(%q<activesupport>, [">= 0"])
78
- s.add_dependency(%q<mongodb-mongo>, [">= 0"])
75
+ s.add_dependency(%q<durran-validatable>, ["= 1.7.5"])
76
+ s.add_dependency(%q<mislav-will_paginate>, ["= 2.3.11"])
77
+ s.add_dependency(%q<activesupport>, ["= 2.3.4"])
78
+ s.add_dependency(%q<mongodb-mongo>, ["= 0.14.1"])
79
79
  end
80
80
  else
81
- s.add_dependency(%q<durran-validatable>, [">= 0"])
82
- s.add_dependency(%q<mislav-will_paginate>, [">= 0"])
83
- s.add_dependency(%q<activesupport>, [">= 0"])
84
- s.add_dependency(%q<mongodb-mongo>, [">= 0"])
81
+ s.add_dependency(%q<durran-validatable>, ["= 1.7.5"])
82
+ s.add_dependency(%q<mislav-will_paginate>, ["= 2.3.11"])
83
+ s.add_dependency(%q<activesupport>, ["= 2.3.4"])
84
+ s.add_dependency(%q<mongodb-mongo>, ["= 0.14.1"])
85
85
  end
86
86
  end
@@ -34,7 +34,7 @@ describe Mongoid::Document do
34
34
  context "finding all documents" do
35
35
 
36
36
  it "returns an array of documents based on the selector provided" do
37
- documents = Person.find(:all, :title => "Test")
37
+ documents = Person.find(:all, :conditions => { :title => "Test"})
38
38
  documents[0].title.should == "Test"
39
39
  end
40
40
 
@@ -12,6 +12,21 @@ describe Mongoid::Document do
12
12
  Person.instance_variable_set(:@collection, nil)
13
13
  end
14
14
 
15
+ describe "#aggregate" do
16
+
17
+ before do
18
+ @reduce = "function(obj, prev) { prev.count++; }"
19
+ end
20
+
21
+ it "returns documents grouped by the supplied fields" do
22
+ results = [{ "title" => "Sir", "count" => 30 }]
23
+ @collection.expects(:group).with([:title], nil, {:count => 0}, @reduce).returns(results)
24
+ grouped = Person.aggregate([:title], {})
25
+ grouped.first["count"].should == 30
26
+ end
27
+
28
+ end
29
+
15
30
  describe "#belongs_to" do
16
31
 
17
32
  it "creates a reader for the association" do
@@ -138,13 +153,27 @@ describe Mongoid::Document do
138
153
  end
139
154
 
140
155
  it "delegates to find_all" do
141
- @collection.expects(:find).with(:test => "Test").returns(@cursor)
156
+ @collection.expects(:find).with({:test => "Test"}, {}).returns(@cursor)
142
157
  @cursor.expects(:collect).returns(@people)
143
158
  Person.find(:all, :conditions => { :test => "Test" })
144
159
  end
145
160
 
146
161
  end
147
162
 
163
+ context "when sorting" do
164
+
165
+ before do
166
+ @cursor = mock
167
+ @people = []
168
+ end
169
+
170
+ it "adds the sort parameters for the collection call" do
171
+ @collection.expects(:find).with({ :test => "Test" }, { :sort => { :test => -1 }}).returns(@cursor)
172
+ @cursor.expects(:collect).returns(@people)
173
+ Person.find(:all, :conditions => { :test => "Test" }, :sort => { :test => -1 })
174
+ end
175
+ end
176
+
148
177
  end
149
178
 
150
179
  describe "#find_first" do
@@ -157,7 +186,7 @@ describe Mongoid::Document do
157
186
 
158
187
  it "finds the first document from the collection and instantiates it" do
159
188
  @collection.expects(:find_one).with(:test => "Test").returns(@attributes)
160
- Person.find_first(:test => "Test").attributes.should == @attributes
189
+ Person.find_first(:conditions => {:test => "Test"}).attributes.should == @attributes
161
190
  end
162
191
 
163
192
  end
@@ -183,9 +212,9 @@ describe Mongoid::Document do
183
212
  context "when a selector is provided" do
184
213
 
185
214
  it "finds from the collection and instantiate objects for each returned" do
186
- @collection.expects(:find).with(:test => "Test").returns(@cursor)
215
+ @collection.expects(:find).with({ :test => "Test" }, {}).returns(@cursor)
187
216
  @cursor.expects(:collect).returns(@people)
188
- Person.find_all(:test => "Test")
217
+ Person.find_all(:conditions => {:test => "Test"})
189
218
  end
190
219
 
191
220
  end
@@ -193,7 +222,7 @@ describe Mongoid::Document do
193
222
  context "when a selector is not provided" do
194
223
 
195
224
  it "finds from the collection and instantiate objects for each returned" do
196
- @collection.expects(:find).with(nil).returns(@cursor)
225
+ @collection.expects(:find).with(nil, {}).returns(@cursor)
197
226
  @cursor.expects(:collect).returns(@people)
198
227
  Person.find_all
199
228
  end
@@ -210,7 +239,7 @@ describe Mongoid::Document do
210
239
 
211
240
  it "returns documents grouped by the supplied fields" do
212
241
  results = [{ "title" => "Sir", "group" => [{ "title" => "Sir", "age" => 30 }] }]
213
- @collection.expects(:group).with([:title], {}, { :group => [] }, @reduce).returns(results)
242
+ @collection.expects(:group).with([:title], nil, { :group => [] }, @reduce).returns(results)
214
243
  grouped = Person.group_by([:title], {})
215
244
  people = grouped.first["group"]
216
245
  people.first.should be_a_kind_of(Person)
@@ -299,7 +328,7 @@ describe Mongoid::Document do
299
328
  it "adds created_on and last_modified to the document" do
300
329
  @collection.expects(:save).with(@tester.attributes)
301
330
  @tester.save
302
- @tester.created_on.should_not be_nil
331
+ @tester.created_at.should_not be_nil
303
332
  @tester.last_modified.should_not be_nil
304
333
  end
305
334
 
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.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Durran Jordan
@@ -18,9 +18,9 @@ dependencies:
18
18
  version_requirement:
19
19
  version_requirements: !ruby/object:Gem::Requirement
20
20
  requirements:
21
- - - ">="
21
+ - - "="
22
22
  - !ruby/object:Gem::Version
23
- version: "0"
23
+ version: 1.7.5
24
24
  version:
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: mislav-will_paginate
@@ -28,9 +28,9 @@ dependencies:
28
28
  version_requirement:
29
29
  version_requirements: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ">="
31
+ - - "="
32
32
  - !ruby/object:Gem::Version
33
- version: "0"
33
+ version: 2.3.11
34
34
  version:
35
35
  - !ruby/object:Gem::Dependency
36
36
  name: activesupport
@@ -38,9 +38,9 @@ dependencies:
38
38
  version_requirement:
39
39
  version_requirements: !ruby/object:Gem::Requirement
40
40
  requirements:
41
- - - ">="
41
+ - - "="
42
42
  - !ruby/object:Gem::Version
43
- version: "0"
43
+ version: 2.3.4
44
44
  version:
45
45
  - !ruby/object:Gem::Dependency
46
46
  name: mongodb-mongo
@@ -48,9 +48,9 @@ dependencies:
48
48
  version_requirement:
49
49
  version_requirements: !ruby/object:Gem::Requirement
50
50
  requirements:
51
- - - ">="
51
+ - - "="
52
52
  - !ruby/object:Gem::Version
53
- version: "0"
53
+ version: 0.14.1
54
54
  version:
55
55
  description:
56
56
  email: durran@gmail.com