mongoid_embedded_helper 0.2.2 → 0.2.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.2.
|
1
|
+
0.2.3
|
@@ -1,7 +1,27 @@
|
|
1
1
|
require 'mongoid'
|
2
2
|
|
3
3
|
module Mongoid
|
4
|
-
module EmbeddedHelper
|
4
|
+
module EmbeddedHelper
|
5
|
+
def self.included(model)
|
6
|
+
model.class_eval do
|
7
|
+
|
8
|
+
alias_method :old_assimilate, :assimilate
|
9
|
+
def assimilate(parent, options)
|
10
|
+
old_assimilate parent, options
|
11
|
+
send(:after_assimilate) if respond_to?(:after_assimilate)
|
12
|
+
# run_callbacks(:after_assimilate)
|
13
|
+
end
|
14
|
+
|
15
|
+
alias_method :old_parentize, :parentize
|
16
|
+
def parentize(object, association_name)
|
17
|
+
old_parentize object, association_name
|
18
|
+
send(:after_parentize) if respond_to?(:after_parentize)
|
19
|
+
# run_callbacks(:after_parentize)
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
5
25
|
def in_collection stack = []
|
6
26
|
stack.extend(ArrayExt) if stack.empty?
|
7
27
|
if embedded?
|
@@ -66,7 +86,8 @@ module Mongoid
|
|
66
86
|
end
|
67
87
|
end
|
68
88
|
|
69
|
-
module Mongoid::Document
|
89
|
+
module Mongoid::Document
|
90
|
+
|
70
91
|
def present? key
|
71
92
|
respond_to? key
|
72
93
|
end
|
@@ -17,7 +17,19 @@ class Item
|
|
17
17
|
field :number, :type => Integer
|
18
18
|
field :name, :type => String
|
19
19
|
field :assoc, :type => Symbol
|
20
|
-
|
20
|
+
|
21
|
+
field :assimilated, :type => Boolean, :default => false
|
22
|
+
field :parentized, :type => Boolean, :default => false
|
23
|
+
|
24
|
+
embedded_in :list, :inverse_of => :items
|
25
|
+
|
26
|
+
def after_parentize
|
27
|
+
self.parentized = true
|
28
|
+
end
|
29
|
+
|
30
|
+
def after_assimilate
|
31
|
+
self.assimilated = true
|
32
|
+
end
|
21
33
|
end
|
22
34
|
|
23
35
|
describe 'Mongoid Embedded Helper' do
|
@@ -38,12 +50,24 @@ describe 'Mongoid Embedded Helper' do
|
|
38
50
|
end
|
39
51
|
|
40
52
|
describe '#in_collection' do
|
41
|
-
it "should handle query from
|
53
|
+
it "should handle query from leaf node without List (non-embedded root node) includes Embedded::Helper" do
|
42
54
|
result = @list.items[0].in_collection.where(:pos => 2).to_a.first
|
43
55
|
result.pos.should == 2
|
44
56
|
result.number.should == 2
|
45
57
|
result.name.should == 'B'
|
46
58
|
end
|
47
59
|
end
|
60
|
+
|
61
|
+
describe 'new callbacks' do
|
62
|
+
context 'item with callbacks added to parent' do
|
63
|
+
it "should call :after_parentize callback" do
|
64
|
+
@list.items[0].parentized.should be_true
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should NOT call :after_assimilate callback" do
|
68
|
+
@list.items[0].assimilated.should be_false
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
48
72
|
end
|
49
73
|
|