mongoid 0.9.6 → 0.9.7
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/mongoid.rb +2 -2
- data/lib/mongoid/associations.rb +16 -9
- data/lib/mongoid/associations/belongs_to.rb +29 -5
- data/lib/mongoid/associations/has_many.rb +12 -0
- data/lib/mongoid/associations/has_one.rb +29 -9
- data/lib/mongoid/associations/options.rb +5 -0
- data/lib/mongoid/associations/relates_to_many.rb +10 -0
- data/lib/mongoid/associations/relates_to_one.rb +24 -5
- data/lib/mongoid/commands.rb +8 -0
- data/lib/mongoid/commands/quick_save.rb +19 -0
- data/lib/mongoid/commands/save.rb +1 -1
- data/lib/mongoid/criteria.rb +8 -18
- data/lib/mongoid/document.rb +27 -59
- data/mongoid.gemspec +8 -9
- data/spec/integration/mongoid/associations_spec.rb +41 -0
- data/spec/integration/mongoid/document_spec.rb +0 -13
- data/spec/spec_helper.rb +1 -8
- data/spec/unit/mongoid/associations/belongs_to_spec.rb +34 -4
- data/spec/unit/mongoid/associations/has_many_spec.rb +9 -0
- data/spec/unit/mongoid/associations/has_one_spec.rb +23 -2
- data/spec/unit/mongoid/associations/options_spec.rb +12 -1
- data/spec/unit/mongoid/associations/relates_to_many_spec.rb +22 -0
- data/spec/unit/mongoid/associations/relates_to_one_spec.rb +65 -1
- data/spec/unit/mongoid/associations_spec.rb +53 -1
- data/spec/unit/mongoid/commands/quick_save_spec.rb +24 -0
- data/spec/unit/mongoid/commands/save_spec.rb +2 -2
- data/spec/unit/mongoid/commands_spec.rb +107 -102
- data/spec/unit/mongoid/criteria_spec.rb +33 -1
- metadata +7 -8
- data/lib/mongoid/associations/accessor.rb +0 -30
- data/lib/mongoid/associations/decorator.rb +0 -27
- data/spec/unit/mongoid/associations/accessor_spec.rb +0 -123
- data/spec/unit/mongoid/associations/decorator_spec.rb +0 -36
@@ -1,30 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
module Mongoid #:nodoc:
|
3
|
-
module Associations #:nodoc:
|
4
|
-
class Accessor #:nodoc:
|
5
|
-
class << self
|
6
|
-
# Gets an association, based on the type provided and
|
7
|
-
# passes the name and document into the newly instantiated
|
8
|
-
# association.
|
9
|
-
def get(type, document, options)
|
10
|
-
document ? type.new(document, options) : nil
|
11
|
-
end
|
12
|
-
|
13
|
-
# Set an object association. This is used to set the parent reference
|
14
|
-
# in a +BelongsTo+, a child reference in a +HasOne+, or many child
|
15
|
-
# references in a +HasMany+.
|
16
|
-
#
|
17
|
-
# Options:
|
18
|
-
#
|
19
|
-
# type: The association type
|
20
|
-
# name: The name of the association
|
21
|
-
# document: The base document to handle the access for.
|
22
|
-
# object: The object that was passed in to the setter method.
|
23
|
-
# options: optional options.
|
24
|
-
def set(type, document, object, options)
|
25
|
-
type.update(object, document, options)
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
@@ -1,27 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
module Mongoid #:nodoc:
|
3
|
-
module Associations #:nodoc:
|
4
|
-
module Decorator #:nodoc:
|
5
|
-
def self.included(base)
|
6
|
-
base.class_eval do
|
7
|
-
attr_reader :document
|
8
|
-
|
9
|
-
# Grabs all the public methods on the document and adds them
|
10
|
-
# to the association class. This is preferred over method_missing
|
11
|
-
# since we can ask the class for its methods and get an
|
12
|
-
# accurate list.
|
13
|
-
def decorate!
|
14
|
-
meths = document.public_methods
|
15
|
-
meths.each do |method|
|
16
|
-
(class << self; self; end).class_eval do
|
17
|
-
define_method method do |*args|
|
18
|
-
document.send method, *args
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
@@ -1,123 +0,0 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
|
3
|
-
describe Mongoid::Associations::Accessor do
|
4
|
-
|
5
|
-
describe "#get" do
|
6
|
-
|
7
|
-
before do
|
8
|
-
@document = Person.new
|
9
|
-
@object = stub
|
10
|
-
end
|
11
|
-
|
12
|
-
context "when type is has_many" do
|
13
|
-
|
14
|
-
it "returns a HasMany" do
|
15
|
-
@options = Mongoid::Associations::Options.new(:name => :addresses)
|
16
|
-
association = Mongoid::Associations::Accessor.get(
|
17
|
-
Mongoid::Associations::HasMany,
|
18
|
-
@document,
|
19
|
-
@options
|
20
|
-
)
|
21
|
-
association.should be_a_kind_of(Mongoid::Associations::HasMany)
|
22
|
-
end
|
23
|
-
|
24
|
-
end
|
25
|
-
|
26
|
-
context "when type is has_one" do
|
27
|
-
|
28
|
-
context "when document is not nil" do
|
29
|
-
|
30
|
-
it "returns a HasOne" do
|
31
|
-
@options = Mongoid::Associations::Options.new(:name => :name)
|
32
|
-
association = Mongoid::Associations::Accessor.get(
|
33
|
-
Mongoid::Associations::HasOne,
|
34
|
-
@document,
|
35
|
-
@options
|
36
|
-
)
|
37
|
-
association.should be_a_kind_of(Name)
|
38
|
-
end
|
39
|
-
|
40
|
-
end
|
41
|
-
|
42
|
-
context "when document is nil" do
|
43
|
-
|
44
|
-
it "returns nil" do
|
45
|
-
@options = Mongoid::Associations::Options.new(:name => :name)
|
46
|
-
association = Mongoid::Associations::Accessor.get(
|
47
|
-
Mongoid::Associations::HasOne,
|
48
|
-
nil,
|
49
|
-
@options
|
50
|
-
)
|
51
|
-
association.should be_nil
|
52
|
-
end
|
53
|
-
|
54
|
-
end
|
55
|
-
|
56
|
-
end
|
57
|
-
|
58
|
-
context "when type is belongs_to" do
|
59
|
-
|
60
|
-
it "returns a BelongsTo" do
|
61
|
-
@options = Mongoid::Associations::Options.new(:name => :person)
|
62
|
-
association = Mongoid::Associations::Accessor.get(
|
63
|
-
Mongoid::Associations::BelongsTo,
|
64
|
-
stub(:parent => @document),
|
65
|
-
@options
|
66
|
-
)
|
67
|
-
association.should be_a_kind_of(Person)
|
68
|
-
end
|
69
|
-
|
70
|
-
end
|
71
|
-
|
72
|
-
end
|
73
|
-
|
74
|
-
describe "#set" do
|
75
|
-
|
76
|
-
context "when type is has_many" do
|
77
|
-
|
78
|
-
it "returns a HasMany" do
|
79
|
-
@options = Mongoid::Associations::Options.new(:name => :addresses)
|
80
|
-
Mongoid::Associations::HasMany.expects(:update).with(@document, @object, @options)
|
81
|
-
Mongoid::Associations::Accessor.set(
|
82
|
-
Mongoid::Associations::HasMany,
|
83
|
-
@document,
|
84
|
-
@object,
|
85
|
-
@options
|
86
|
-
)
|
87
|
-
end
|
88
|
-
|
89
|
-
end
|
90
|
-
|
91
|
-
context "when type is has_one" do
|
92
|
-
|
93
|
-
it "returns a HasOne" do
|
94
|
-
@options = Mongoid::Associations::Options.new(:name => :name)
|
95
|
-
Mongoid::Associations::HasOne.expects(:update).with(@document, @object, @options)
|
96
|
-
Mongoid::Associations::Accessor.set(
|
97
|
-
Mongoid::Associations::HasOne,
|
98
|
-
@document,
|
99
|
-
@object,
|
100
|
-
@options
|
101
|
-
)
|
102
|
-
end
|
103
|
-
|
104
|
-
end
|
105
|
-
|
106
|
-
context "when type is belongs_to" do
|
107
|
-
|
108
|
-
it "returns a BelongsTo" do
|
109
|
-
@options = Mongoid::Associations::Options.new(:name => :person)
|
110
|
-
Mongoid::Associations::BelongsTo.expects(:update).with(@object, @document, @options)
|
111
|
-
Mongoid::Associations::Accessor.set(
|
112
|
-
Mongoid::Associations::BelongsTo,
|
113
|
-
@document,
|
114
|
-
@object,
|
115
|
-
@options
|
116
|
-
)
|
117
|
-
end
|
118
|
-
|
119
|
-
end
|
120
|
-
|
121
|
-
end
|
122
|
-
|
123
|
-
end
|
@@ -1,36 +0,0 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
|
3
|
-
describe Mongoid::Associations::Decorator do
|
4
|
-
|
5
|
-
describe "#included" do
|
6
|
-
|
7
|
-
before do
|
8
|
-
@person = Person.new
|
9
|
-
@decorated = Decorated.new(@person)
|
10
|
-
end
|
11
|
-
|
12
|
-
it "adds a document reader" do
|
13
|
-
@decorated.should respond_to(:document)
|
14
|
-
end
|
15
|
-
|
16
|
-
it "adds a decorate! instance method" do
|
17
|
-
@decorated.should respond_to(:decorate!)
|
18
|
-
end
|
19
|
-
|
20
|
-
end
|
21
|
-
|
22
|
-
describe "#decorate!" do
|
23
|
-
|
24
|
-
before do
|
25
|
-
@person = Person.new
|
26
|
-
@decorated = Decorated.new(@person)
|
27
|
-
end
|
28
|
-
|
29
|
-
it "adds all the documents methods to the class" do
|
30
|
-
@decorated.decorate!
|
31
|
-
@decorated.should respond_to(:title, :terms, :age, :addresses, :name, :save)
|
32
|
-
end
|
33
|
-
|
34
|
-
end
|
35
|
-
|
36
|
-
end
|