mongoid 1.0.0 → 1.0.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/HISTORY +12 -0
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/lib/mongoid.rb +2 -1
- data/lib/mongoid/components.rb +1 -0
- data/lib/mongoid/criteria.rb +17 -0
- data/lib/mongoid/document.rb +21 -16
- data/lib/mongoid/extensions/string/inflections.rb +12 -0
- data/lib/mongoid/named_scopes.rb +94 -0
- data/mongoid.gemspec +10 -5
- data/spec/integration/mongoid/document_spec.rb +28 -0
- data/spec/integration/mongoid/inheritance_spec.rb +2 -2
- data/spec/integration/mongoid/named_scopes_spec.rb +25 -0
- data/spec/unit/mongoid/commands_spec.rb +1 -0
- data/spec/unit/mongoid/criteria_spec.rb +13 -0
- data/spec/unit/mongoid/document_spec.rb +6 -6
- data/spec/unit/mongoid/extensions/string/inflections_spec.rb +36 -0
- data/spec/unit/mongoid/named_scopes_spec.rb +86 -0
- metadata +8 -3
data/HISTORY
CHANGED
@@ -1,3 +1,15 @@
|
|
1
|
+
=== 1.0.1
|
2
|
+
- Documents now have named_scopes similar to
|
3
|
+
ActiveRecord named scopes. Please see rdoc or
|
4
|
+
specs for examples.
|
5
|
+
|
6
|
+
- Document#to_json properly works in all cases.
|
7
|
+
|
8
|
+
- ActiveSupport calls for inflections have been
|
9
|
+
moved into the String::Inflections module.
|
10
|
+
|
11
|
+
- Updated dependency on Validatable to 2.0.1
|
12
|
+
|
1
13
|
=== 1.0.0
|
2
14
|
- Validations cleanup: Only before_validation and
|
3
15
|
after_validation callbacks are supported now.
|
data/Rakefile
CHANGED
@@ -14,7 +14,7 @@ begin
|
|
14
14
|
|
15
15
|
gem.add_dependency("activesupport", ">= 2.2.2")
|
16
16
|
gem.add_dependency("mongo", ">= 0.18.2")
|
17
|
-
gem.add_dependency("durran-validatable", ">= 2.0.
|
17
|
+
gem.add_dependency("durran-validatable", ">= 2.0.1")
|
18
18
|
gem.add_dependency("leshill-will_paginate", ">= 2.3.11")
|
19
19
|
|
20
20
|
gem.add_development_dependency("rspec", ">= 1.2.9")
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.0.
|
1
|
+
1.0.1
|
data/lib/mongoid.rb
CHANGED
@@ -23,7 +23,7 @@ require "rubygems"
|
|
23
23
|
|
24
24
|
gem "activesupport", ">= 2.2.2"
|
25
25
|
gem "mongo", ">= 0.18.2"
|
26
|
-
gem "durran-validatable", ">= 2.0.
|
26
|
+
gem "durran-validatable", ">= 2.0.1"
|
27
27
|
gem "leshill-will_paginate", ">= 2.3.11"
|
28
28
|
|
29
29
|
require "delegate"
|
@@ -45,6 +45,7 @@ require "mongoid/commands"
|
|
45
45
|
require "mongoid/config"
|
46
46
|
require "mongoid/complex_criterion"
|
47
47
|
require "mongoid/criteria"
|
48
|
+
require "mongoid/named_scopes"
|
48
49
|
require "mongoid/extensions"
|
49
50
|
require "mongoid/errors"
|
50
51
|
require "mongoid/fields/field"
|
data/lib/mongoid/components.rb
CHANGED
data/lib/mongoid/criteria.rb
CHANGED
@@ -103,6 +103,23 @@ module Mongoid #:nodoc:
|
|
103
103
|
@count ||= @klass.collection.find(@selector, process_options).count
|
104
104
|
end
|
105
105
|
|
106
|
+
# Translate the supplied argument hash
|
107
|
+
#
|
108
|
+
# Options:
|
109
|
+
#
|
110
|
+
# criteria_conditions: Hash of criteria keys, and parameter values
|
111
|
+
#
|
112
|
+
# Example:
|
113
|
+
#
|
114
|
+
# <tt>criteria.translate(:where => { :field => "value"}, :limit => 20)</tt>
|
115
|
+
#
|
116
|
+
# Returns <tt>self</tt>
|
117
|
+
def criteria(criteria_conditions = {})
|
118
|
+
criteria_conditions.inject(self) do |criteria, (key, value)|
|
119
|
+
criteria.send(key, value)
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
106
123
|
# Iterate over each +Document+ in the results. This can take an optional
|
107
124
|
# block to pass to each argument in the results.
|
108
125
|
#
|
data/lib/mongoid/document.rb
CHANGED
@@ -9,12 +9,13 @@ module Mongoid #:nodoc:
|
|
9
9
|
|
10
10
|
cattr_accessor :_collection, :collection_name, :embedded, :primary_key
|
11
11
|
|
12
|
-
self.
|
12
|
+
self.embedded = false
|
13
|
+
self.collection_name = self.name.collectionize
|
13
14
|
|
14
15
|
attr_accessor :association_name, :_parent
|
15
16
|
attr_reader :attributes, :new_record
|
16
17
|
|
17
|
-
delegate :collection, :embedded
|
18
|
+
delegate :collection, :embedded, :primary_key, :to => "self.class"
|
18
19
|
end
|
19
20
|
end
|
20
21
|
|
@@ -25,28 +26,18 @@ module Mongoid #:nodoc:
|
|
25
26
|
#
|
26
27
|
# Returns: <tt>Mongo::Collection</tt>
|
27
28
|
def collection
|
28
|
-
raise Errors::InvalidCollection.new(self) if embedded
|
29
|
+
raise Errors::InvalidCollection.new(self) if embedded
|
29
30
|
self._collection ||= Mongoid.database.collection(self.collection_name)
|
30
31
|
add_indexes; self._collection
|
31
32
|
end
|
32
33
|
|
33
|
-
# return true if the +Document+ is embedded in another +Documnet+. The
|
34
|
-
# embedded flag is set by declaring a belongs_to association.
|
35
|
-
#
|
36
|
-
# Example:
|
37
|
-
#
|
38
|
-
# <tt>Address.embedded?</tt>
|
39
|
-
def embedded?
|
40
|
-
self.embedded == true
|
41
|
-
end
|
42
|
-
|
43
34
|
# Returns a human readable version of the class.
|
44
35
|
#
|
45
36
|
# Example:
|
46
37
|
#
|
47
38
|
# <tt>MixedDrink.human_name # returns "Mixed Drink"</tt>
|
48
39
|
def human_name
|
49
|
-
name.
|
40
|
+
name.labelize
|
50
41
|
end
|
51
42
|
|
52
43
|
# Instantiate a new object, only when loaded from the database or when
|
@@ -228,6 +219,17 @@ module Mongoid #:nodoc:
|
|
228
219
|
[ self ]
|
229
220
|
end
|
230
221
|
|
222
|
+
# Return this document as a JSON string. Nothing special is required here
|
223
|
+
# since Mongoid bubbles up all the child associations to the parent
|
224
|
+
# attribute +Hash+ using observers throughout the +Document+ lifecycle.
|
225
|
+
#
|
226
|
+
# Example:
|
227
|
+
#
|
228
|
+
# <tt>person.to_json</tt>
|
229
|
+
def to_json
|
230
|
+
@attributes.to_json
|
231
|
+
end
|
232
|
+
|
231
233
|
# Returns the id of the Document, used in Rails compatibility.
|
232
234
|
def to_param
|
233
235
|
id
|
@@ -257,8 +259,7 @@ module Mongoid #:nodoc:
|
|
257
259
|
protected
|
258
260
|
def generate_key
|
259
261
|
if primary_key
|
260
|
-
|
261
|
-
@attributes[:_id] = values.join(" ").parameterize.to_s
|
262
|
+
@attributes[:_id] = extract_keys.join(" ").identify
|
262
263
|
else
|
263
264
|
@attributes[:_id] = Mongo::ObjectID.new.to_s unless id
|
264
265
|
end
|
@@ -268,6 +269,10 @@ module Mongoid #:nodoc:
|
|
268
269
|
@attributes[:_type] ||= self.class.name
|
269
270
|
end
|
270
271
|
|
272
|
+
def extract_keys
|
273
|
+
primary_key.collect { |key| @attributes[key] }.reject { |val| val.nil? }
|
274
|
+
end
|
275
|
+
|
271
276
|
end
|
272
277
|
|
273
278
|
end
|
@@ -17,6 +17,18 @@ module Mongoid #:nodoc:
|
|
17
17
|
"descending" => "ascending"
|
18
18
|
}
|
19
19
|
|
20
|
+
def collectionize
|
21
|
+
tableize.gsub("/", "_")
|
22
|
+
end
|
23
|
+
|
24
|
+
def identify
|
25
|
+
gsub(" ", "_").gsub(/\W/, "").dasherize.downcase
|
26
|
+
end
|
27
|
+
|
28
|
+
def labelize
|
29
|
+
underscore.humanize
|
30
|
+
end
|
31
|
+
|
20
32
|
def invert
|
21
33
|
REVERSALS[self]
|
22
34
|
end
|
@@ -0,0 +1,94 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Mongoid #:nodoc:
|
3
|
+
module NamedScopes
|
4
|
+
# Return the scopes or default to an empty +Hash+.
|
5
|
+
def scopes
|
6
|
+
read_inheritable_attribute(:scopes) || write_inheritable_attribute(:scopes, {})
|
7
|
+
end
|
8
|
+
|
9
|
+
# Creates a named_scope for the +Document+, similar to ActiveRecord's
|
10
|
+
# named_scopes. +NamedScopes+ are proxied +Criteria+ objects that can be
|
11
|
+
# chained.
|
12
|
+
#
|
13
|
+
# Example:
|
14
|
+
#
|
15
|
+
# class Person
|
16
|
+
# include Mongoid::Document
|
17
|
+
# field :active, :type => Boolean
|
18
|
+
# field :count, :type => Integer
|
19
|
+
#
|
20
|
+
# named_scope :active, :where => { :active => true }
|
21
|
+
# named_scope :count_gt_one, :where => { :count.gt => 1 }
|
22
|
+
# named_scope :at_least_count, lambda { |count| { :where => { :count.gt => count } } }
|
23
|
+
# end
|
24
|
+
def named_scope(name, options = {}, &block)
|
25
|
+
name = name.to_sym
|
26
|
+
scopes[name] = lambda do |parent_scope, *args|
|
27
|
+
CriteriaProxy.new(parent_scope, Hash === options ? options : options.call(*args), &block)
|
28
|
+
end
|
29
|
+
(class << self; self; end).class_eval <<-EOT
|
30
|
+
def #{name}(*args)
|
31
|
+
scopes[:#{name}].call(self, *args)
|
32
|
+
end
|
33
|
+
EOT
|
34
|
+
end
|
35
|
+
|
36
|
+
class CriteriaProxy #:nodoc
|
37
|
+
attr_accessor :conditions, :klass, :parent_scope
|
38
|
+
|
39
|
+
delegate :scopes, :to => :parent_scope
|
40
|
+
|
41
|
+
# Instantiate the new +CriteriaProxy+. If the conditions contains an
|
42
|
+
# extension, the proxy will extend from that module. If a block is given
|
43
|
+
# it will be extended as well.
|
44
|
+
#
|
45
|
+
# Example:
|
46
|
+
#
|
47
|
+
# <tt>CriteriaProxy.new(parent, :where => { :active => true })</tt>
|
48
|
+
def initialize(parent_scope, conditions, &block)
|
49
|
+
conditions ||= {}
|
50
|
+
[ conditions.delete(:extend) ].flatten.each do |extension|
|
51
|
+
extend extension
|
52
|
+
end if conditions.include?(:extend)
|
53
|
+
extend Module.new(&block) if block_given?
|
54
|
+
self.klass = parent_scope unless CriteriaProxy === parent_scope
|
55
|
+
self.parent_scope, self.conditions = parent_scope, conditions
|
56
|
+
end
|
57
|
+
|
58
|
+
# First check if the proxy has the scope defined, otherwise look to the
|
59
|
+
# parent scope.
|
60
|
+
def respond_to?(method, include_private = false)
|
61
|
+
super || if klass
|
62
|
+
proxy_found.respond_to?(method, include_private)
|
63
|
+
else
|
64
|
+
parent_scope.respond_to?(method, include_private)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
protected
|
69
|
+
|
70
|
+
def proxy_found
|
71
|
+
@found || load_found
|
72
|
+
end
|
73
|
+
|
74
|
+
private
|
75
|
+
|
76
|
+
def method_missing(method, *args, &block)
|
77
|
+
if scopes.include?(method)
|
78
|
+
scopes[method].call(self, *args)
|
79
|
+
elsif klass
|
80
|
+
proxy_found.send(method, *args, &block)
|
81
|
+
else
|
82
|
+
parent_scope.criteria(conditions)
|
83
|
+
parent_scope.send(method, *args, &block)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def load_found
|
88
|
+
@found = Criteria.new(klass)
|
89
|
+
@found.criteria(conditions); @found
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
data/mongoid.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{mongoid}
|
8
|
-
s.version = "1.0.
|
8
|
+
s.version = "1.0.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"]
|
12
|
-
s.date = %q{2010-01-
|
12
|
+
s.date = %q{2010-01-10}
|
13
13
|
s.email = %q{durran@gmail.com}
|
14
14
|
s.extra_rdoc_files = [
|
15
15
|
"README.rdoc"
|
@@ -73,6 +73,7 @@ Gem::Specification.new do |s|
|
|
73
73
|
"lib/mongoid/finders.rb",
|
74
74
|
"lib/mongoid/indexes.rb",
|
75
75
|
"lib/mongoid/memoization.rb",
|
76
|
+
"lib/mongoid/named_scopes.rb",
|
76
77
|
"lib/mongoid/timestamps.rb",
|
77
78
|
"lib/mongoid/versioning.rb",
|
78
79
|
"mongoid.gemspec",
|
@@ -83,6 +84,7 @@ Gem::Specification.new do |s|
|
|
83
84
|
"spec/integration/mongoid/document_spec.rb",
|
84
85
|
"spec/integration/mongoid/finders_spec.rb",
|
85
86
|
"spec/integration/mongoid/inheritance_spec.rb",
|
87
|
+
"spec/integration/mongoid/named_scopes_spec.rb",
|
86
88
|
"spec/spec.opts",
|
87
89
|
"spec/spec_helper.rb",
|
88
90
|
"spec/unit/mongoid/associations/belongs_to_related_spec.rb",
|
@@ -130,6 +132,7 @@ Gem::Specification.new do |s|
|
|
130
132
|
"spec/unit/mongoid/finders_spec.rb",
|
131
133
|
"spec/unit/mongoid/indexes_spec.rb",
|
132
134
|
"spec/unit/mongoid/memoization_spec.rb",
|
135
|
+
"spec/unit/mongoid/named_scopes_spec.rb",
|
133
136
|
"spec/unit/mongoid/timestamps_spec.rb",
|
134
137
|
"spec/unit/mongoid/versioning_spec.rb",
|
135
138
|
"spec/unit/mongoid_spec.rb"
|
@@ -146,6 +149,7 @@ Gem::Specification.new do |s|
|
|
146
149
|
"spec/integration/mongoid/document_spec.rb",
|
147
150
|
"spec/integration/mongoid/finders_spec.rb",
|
148
151
|
"spec/integration/mongoid/inheritance_spec.rb",
|
152
|
+
"spec/integration/mongoid/named_scopes_spec.rb",
|
149
153
|
"spec/spec_helper.rb",
|
150
154
|
"spec/unit/mongoid/associations/belongs_to_related_spec.rb",
|
151
155
|
"spec/unit/mongoid/associations/belongs_to_spec.rb",
|
@@ -192,6 +196,7 @@ Gem::Specification.new do |s|
|
|
192
196
|
"spec/unit/mongoid/finders_spec.rb",
|
193
197
|
"spec/unit/mongoid/indexes_spec.rb",
|
194
198
|
"spec/unit/mongoid/memoization_spec.rb",
|
199
|
+
"spec/unit/mongoid/named_scopes_spec.rb",
|
195
200
|
"spec/unit/mongoid/timestamps_spec.rb",
|
196
201
|
"spec/unit/mongoid/versioning_spec.rb",
|
197
202
|
"spec/unit/mongoid_spec.rb"
|
@@ -204,14 +209,14 @@ Gem::Specification.new do |s|
|
|
204
209
|
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
205
210
|
s.add_runtime_dependency(%q<activesupport>, [">= 2.2.2"])
|
206
211
|
s.add_runtime_dependency(%q<mongo>, [">= 0.18.2"])
|
207
|
-
s.add_runtime_dependency(%q<durran-validatable>, [">= 2.0.
|
212
|
+
s.add_runtime_dependency(%q<durran-validatable>, [">= 2.0.1"])
|
208
213
|
s.add_runtime_dependency(%q<leshill-will_paginate>, [">= 2.3.11"])
|
209
214
|
s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
|
210
215
|
s.add_development_dependency(%q<mocha>, [">= 0.9.8"])
|
211
216
|
else
|
212
217
|
s.add_dependency(%q<activesupport>, [">= 2.2.2"])
|
213
218
|
s.add_dependency(%q<mongo>, [">= 0.18.2"])
|
214
|
-
s.add_dependency(%q<durran-validatable>, [">= 2.0.
|
219
|
+
s.add_dependency(%q<durran-validatable>, [">= 2.0.1"])
|
215
220
|
s.add_dependency(%q<leshill-will_paginate>, [">= 2.3.11"])
|
216
221
|
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
217
222
|
s.add_dependency(%q<mocha>, [">= 0.9.8"])
|
@@ -219,7 +224,7 @@ Gem::Specification.new do |s|
|
|
219
224
|
else
|
220
225
|
s.add_dependency(%q<activesupport>, [">= 2.2.2"])
|
221
226
|
s.add_dependency(%q<mongo>, [">= 0.18.2"])
|
222
|
-
s.add_dependency(%q<durran-validatable>, [">= 2.0.
|
227
|
+
s.add_dependency(%q<durran-validatable>, [">= 2.0.1"])
|
223
228
|
s.add_dependency(%q<leshill-will_paginate>, [">= 2.3.11"])
|
224
229
|
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
225
230
|
s.add_dependency(%q<mocha>, [">= 0.9.8"])
|
@@ -457,6 +457,34 @@ describe Mongoid::Document do
|
|
457
457
|
|
458
458
|
end
|
459
459
|
|
460
|
+
describe "#to_json" do
|
461
|
+
|
462
|
+
before do
|
463
|
+
@person = Person.new(:title => "Sir", :age => 30)
|
464
|
+
@address = Address.new(:street => "Nan Jing Dong Lu")
|
465
|
+
@person.addresses << @address
|
466
|
+
end
|
467
|
+
|
468
|
+
context "on a new document" do
|
469
|
+
|
470
|
+
it "returns the json string" do
|
471
|
+
@person.to_json.should == @person.attributes.to_json
|
472
|
+
end
|
473
|
+
|
474
|
+
end
|
475
|
+
|
476
|
+
context "on a persisted document" do
|
477
|
+
|
478
|
+
it "returns the json string" do
|
479
|
+
@person.save
|
480
|
+
from_db = Person.find(@person.id)
|
481
|
+
from_db.to_json.should == from_db.attributes.to_json
|
482
|
+
end
|
483
|
+
|
484
|
+
end
|
485
|
+
|
486
|
+
end
|
487
|
+
|
460
488
|
context "typecasting" do
|
461
489
|
|
462
490
|
before do
|
@@ -11,7 +11,7 @@ describe Mongoid::Document do
|
|
11
11
|
|
12
12
|
it "saves in the same collection as the root" do
|
13
13
|
collection = Mongoid.database.collection("canvases")
|
14
|
-
attributes = collection.find({ :name => "Test"}, {}).
|
14
|
+
attributes = collection.find({ :name => "Test"}, {}).next_document
|
15
15
|
attributes["version"].should == 3
|
16
16
|
attributes["name"].should == "Test"
|
17
17
|
attributes["_type"].should == "Browser"
|
@@ -29,7 +29,7 @@ describe Mongoid::Document do
|
|
29
29
|
|
30
30
|
it "saves in the same collection as the root" do
|
31
31
|
collection = Mongoid.database.collection("canvases")
|
32
|
-
attributes = collection.find({ :name => "Testy"}, {}).
|
32
|
+
attributes = collection.find({ :name => "Testy"}, {}).next_document
|
33
33
|
attributes["version"].should == 2
|
34
34
|
attributes["name"].should == "Testy"
|
35
35
|
attributes["_type"].should == "Firefox"
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Mongoid::NamedScopes do
|
4
|
+
|
5
|
+
describe ".named_scope" do
|
6
|
+
|
7
|
+
class Person
|
8
|
+
named_scope :doctors, {:where => {:title => 'Dr.'}}
|
9
|
+
end
|
10
|
+
|
11
|
+
before do
|
12
|
+
@document = Person.create(:title => "Dr.")
|
13
|
+
end
|
14
|
+
|
15
|
+
after do
|
16
|
+
Person.delete_all
|
17
|
+
end
|
18
|
+
|
19
|
+
it "returns the document" do
|
20
|
+
Person.doctors.first.should == @document
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
@@ -215,6 +215,7 @@ describe Mongoid::Commands do
|
|
215
215
|
end
|
216
216
|
|
217
217
|
it "runs the validation callbacks" do
|
218
|
+
@comment.expects(:run_callbacks).with(:validate)
|
218
219
|
@comment.expects(:run_callbacks).with(:before_validation)
|
219
220
|
@comment.expects(:run_callbacks).with(:after_validation)
|
220
221
|
@comment.valid?
|
@@ -1129,4 +1129,17 @@ describe Mongoid::Criteria do
|
|
1129
1129
|
|
1130
1130
|
end
|
1131
1131
|
|
1132
|
+
context "#criteria" do
|
1133
|
+
|
1134
|
+
it ":where => {:title => 'Test'} returns a criteria with the correct selector" do
|
1135
|
+
@result = @criteria.criteria(:where => { :title => 'Test' })
|
1136
|
+
@result.selector[:title].should == 'Test'
|
1137
|
+
end
|
1138
|
+
|
1139
|
+
it ":where => {:title => 'Test'}, :skip => 10 returns a criteria with the correct selector and options" do
|
1140
|
+
@result = @criteria.criteria(:where => { :title => 'Test' }, :skip => 10)
|
1141
|
+
@result.selector[:title].should == 'Test'
|
1142
|
+
@result.options.should == { :skip => 10 }
|
1143
|
+
end
|
1144
|
+
end
|
1132
1145
|
end
|
@@ -164,12 +164,12 @@ describe Mongoid::Document do
|
|
164
164
|
|
165
165
|
end
|
166
166
|
|
167
|
-
describe ".embedded
|
167
|
+
describe ".embedded" do
|
168
168
|
|
169
169
|
context "when the document is embedded" do
|
170
170
|
|
171
171
|
it "returns true" do
|
172
|
-
Address.embedded
|
172
|
+
Address.embedded.should be_true
|
173
173
|
end
|
174
174
|
|
175
175
|
end
|
@@ -177,7 +177,7 @@ describe Mongoid::Document do
|
|
177
177
|
context "when the document is not embedded" do
|
178
178
|
|
179
179
|
it "returns false" do
|
180
|
-
Person.embedded
|
180
|
+
Person.embedded.should be_false
|
181
181
|
end
|
182
182
|
|
183
183
|
end
|
@@ -190,7 +190,7 @@ describe Mongoid::Document do
|
|
190
190
|
|
191
191
|
it "returns true" do
|
192
192
|
address = Address.new
|
193
|
-
address.embedded
|
193
|
+
address.embedded.should be_true
|
194
194
|
end
|
195
195
|
|
196
196
|
end
|
@@ -199,7 +199,7 @@ describe Mongoid::Document do
|
|
199
199
|
|
200
200
|
it "returns false" do
|
201
201
|
person = Person.new
|
202
|
-
person.embedded
|
202
|
+
person.embedded.should be_false
|
203
203
|
end
|
204
204
|
|
205
205
|
end
|
@@ -208,7 +208,7 @@ describe Mongoid::Document do
|
|
208
208
|
|
209
209
|
it "returns true" do
|
210
210
|
circle = Circle.new
|
211
|
-
circle.should
|
211
|
+
circle.embedded.should be_true
|
212
212
|
end
|
213
213
|
|
214
214
|
end
|
@@ -2,6 +2,42 @@ require "spec_helper"
|
|
2
2
|
|
3
3
|
describe Mongoid::Extensions::String::Inflections do
|
4
4
|
|
5
|
+
describe "#collectionize" do
|
6
|
+
|
7
|
+
context "when class is namepaced" do
|
8
|
+
|
9
|
+
it "returns an underscored tableized name" do
|
10
|
+
Medical::Patient.name.collectionize.should == "medical_patients"
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
|
15
|
+
context "when class is not namespaced" do
|
16
|
+
|
17
|
+
it "returns an underscored tableized name" do
|
18
|
+
MixedDrink.name.collectionize.should == "mixed_drinks"
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
describe "#identify" do
|
26
|
+
|
27
|
+
it "converts the string to all lowercase and dashed" do
|
28
|
+
"A Midnight Summer Night's Dream".identify.should == "a-midnight-summer-nights-dream"
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
describe "#labelize" do
|
34
|
+
|
35
|
+
it "returns the underscored name humanized" do
|
36
|
+
MixedDrink.name.labelize.should == "Mixed drink"
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
5
41
|
describe "#singular?" do
|
6
42
|
|
7
43
|
context "when singular" do
|
@@ -0,0 +1,86 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Mongoid::NamedScopes do
|
4
|
+
|
5
|
+
context "AR2 scopes" do
|
6
|
+
module Extension
|
7
|
+
def extension_module_method
|
8
|
+
"extension module method"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class NamedScopeTest
|
13
|
+
include Mongoid::Document
|
14
|
+
|
15
|
+
field :active, :type => Boolean
|
16
|
+
field :count, :type => Integer
|
17
|
+
|
18
|
+
named_scope :active, :where => {:active => true} do
|
19
|
+
def extension_method
|
20
|
+
"extension method"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
named_scope :count_gt_one, :where => { :count.gt => 1 }, :extend => Extension
|
24
|
+
named_scope :at_least_count, lambda { |count| { :where => { :count.gt => count } } }
|
25
|
+
end
|
26
|
+
|
27
|
+
context ".named_scope" do
|
28
|
+
it "adds the named scope to the hash of scopes" do
|
29
|
+
NamedScopeTest.scopes.should have_key(:active)
|
30
|
+
end
|
31
|
+
|
32
|
+
it "creates a class method for the named scope" do
|
33
|
+
NamedScopeTest.should respond_to(:active)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
context "accessing a named scope" do
|
38
|
+
it "is a criteria proxy" do
|
39
|
+
Mongoid::NamedScopes::CriteriaProxy.should === NamedScopeTest.active
|
40
|
+
end
|
41
|
+
|
42
|
+
it "responds like a criteria" do
|
43
|
+
NamedScopeTest.active.should respond_to(:selector)
|
44
|
+
end
|
45
|
+
|
46
|
+
it "instantiates the criteria" do
|
47
|
+
criteria = Mongoid::Criteria.new(NamedScopeTest)
|
48
|
+
Mongoid::Criteria.expects(:new).returns(criteria)
|
49
|
+
NamedScopeTest.active.selector
|
50
|
+
end
|
51
|
+
|
52
|
+
it "has set the conditions on the criteria" do
|
53
|
+
NamedScopeTest.active.selector[:active].should be_true
|
54
|
+
end
|
55
|
+
|
56
|
+
it "sets the association extension by block" do
|
57
|
+
NamedScopeTest.active.extension_method.should == "extension method"
|
58
|
+
end
|
59
|
+
|
60
|
+
it "sets the association extension by :extend" do
|
61
|
+
NamedScopeTest.count_gt_one.extension_module_method.should == "extension module method"
|
62
|
+
end
|
63
|
+
|
64
|
+
context "when using a lambda" do
|
65
|
+
it "accepts parameters to the criteria" do
|
66
|
+
NamedScopeTest.at_least_count(3).selector[:count].should == {'$gt' => 3}
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
context "chained scopes" do
|
72
|
+
it "instantiates the criteria" do
|
73
|
+
criteria = Mongoid::Criteria.new(NamedScopeTest)
|
74
|
+
Mongoid::Criteria.expects(:new).returns(criteria)
|
75
|
+
NamedScopeTest.active.count_gt_one.selector
|
76
|
+
end
|
77
|
+
|
78
|
+
it "merges the criteria" do
|
79
|
+
selector = NamedScopeTest.active.count_gt_one.selector
|
80
|
+
selector[:count].should == {'$gt' => 1}
|
81
|
+
selector[:active].should be_true
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
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.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Durran Jordan
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-01-
|
12
|
+
date: 2010-01-10 00:00:00 -05:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -40,7 +40,7 @@ dependencies:
|
|
40
40
|
requirements:
|
41
41
|
- - ">="
|
42
42
|
- !ruby/object:Gem::Version
|
43
|
-
version: 2.0.
|
43
|
+
version: 2.0.1
|
44
44
|
version:
|
45
45
|
- !ruby/object:Gem::Dependency
|
46
46
|
name: leshill-will_paginate
|
@@ -139,6 +139,7 @@ files:
|
|
139
139
|
- lib/mongoid/finders.rb
|
140
140
|
- lib/mongoid/indexes.rb
|
141
141
|
- lib/mongoid/memoization.rb
|
142
|
+
- lib/mongoid/named_scopes.rb
|
142
143
|
- lib/mongoid/timestamps.rb
|
143
144
|
- lib/mongoid/versioning.rb
|
144
145
|
- mongoid.gemspec
|
@@ -149,6 +150,7 @@ files:
|
|
149
150
|
- spec/integration/mongoid/document_spec.rb
|
150
151
|
- spec/integration/mongoid/finders_spec.rb
|
151
152
|
- spec/integration/mongoid/inheritance_spec.rb
|
153
|
+
- spec/integration/mongoid/named_scopes_spec.rb
|
152
154
|
- spec/spec.opts
|
153
155
|
- spec/spec_helper.rb
|
154
156
|
- spec/unit/mongoid/associations/belongs_to_related_spec.rb
|
@@ -196,6 +198,7 @@ files:
|
|
196
198
|
- spec/unit/mongoid/finders_spec.rb
|
197
199
|
- spec/unit/mongoid/indexes_spec.rb
|
198
200
|
- spec/unit/mongoid/memoization_spec.rb
|
201
|
+
- spec/unit/mongoid/named_scopes_spec.rb
|
199
202
|
- spec/unit/mongoid/timestamps_spec.rb
|
200
203
|
- spec/unit/mongoid/versioning_spec.rb
|
201
204
|
- spec/unit/mongoid_spec.rb
|
@@ -234,6 +237,7 @@ test_files:
|
|
234
237
|
- spec/integration/mongoid/document_spec.rb
|
235
238
|
- spec/integration/mongoid/finders_spec.rb
|
236
239
|
- spec/integration/mongoid/inheritance_spec.rb
|
240
|
+
- spec/integration/mongoid/named_scopes_spec.rb
|
237
241
|
- spec/spec_helper.rb
|
238
242
|
- spec/unit/mongoid/associations/belongs_to_related_spec.rb
|
239
243
|
- spec/unit/mongoid/associations/belongs_to_spec.rb
|
@@ -280,6 +284,7 @@ test_files:
|
|
280
284
|
- spec/unit/mongoid/finders_spec.rb
|
281
285
|
- spec/unit/mongoid/indexes_spec.rb
|
282
286
|
- spec/unit/mongoid/memoization_spec.rb
|
287
|
+
- spec/unit/mongoid/named_scopes_spec.rb
|
283
288
|
- spec/unit/mongoid/timestamps_spec.rb
|
284
289
|
- spec/unit/mongoid/versioning_spec.rb
|
285
290
|
- spec/unit/mongoid_spec.rb
|