mongoid_embedded_helper 0.2.2 → 0.2.3

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/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.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
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mongoid_embedded_helper}
8
- s.version = "0.2.2"
8
+ s.version = "0.2.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Kristian Mandrup"]
@@ -65,7 +65,7 @@ describe 'Mongoid Embedded Helper' do
65
65
  end
66
66
  end
67
67
 
68
- describe '#top level query' do
68
+ describe '#in_collection' do
69
69
 
70
70
  it "should handle query from top level node" do
71
71
  result = @person.in_collection.where(:name => 'Kristian').to_a.first
@@ -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
- embedded_in :list, :inverse_of => :items
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 mid level node without List including Embedded::Helper" do
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
 
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 2
9
- version: 0.2.2
8
+ - 3
9
+ version: 0.2.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - Kristian Mandrup