mongoid_alize 0.1.0

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.
@@ -0,0 +1,21 @@
1
+ class Person
2
+ include Mongoid::Document
3
+ include Mongoid::Alize
4
+
5
+ field :name, type: String
6
+ field :created_at, type: Time
7
+
8
+ # the attached head
9
+ has_one :head
10
+
11
+ # the heads taken from others
12
+ has_many :heads, :class_name => "Head", :inverse_of => :captor
13
+
14
+ # the heads wanted from others
15
+ has_and_belongs_to_many :wants, :class_name => "Head", :inverse_of => :wanted_by
16
+
17
+ # the only head that is watching
18
+ belongs_to :seen_by, :class_name => "Head", :inverse_of => :sees
19
+
20
+ end
21
+
@@ -0,0 +1,45 @@
1
+ module MacrosHelper
2
+ def self.included(base)
3
+ base.extend ClassMethods
4
+ end
5
+
6
+ module ClassMethods
7
+ def fns
8
+ Mongoid::Alize::Callbacks::From
9
+ end
10
+
11
+ def tns
12
+ Mongoid::Alize::Callbacks::To
13
+ end
14
+
15
+ def it_should_set_callbacks(klass, inverse_klass, relation, inverse_relation, fns, tns)
16
+ fields = [:fake]
17
+
18
+ it "should use #{fns} to pull" do
19
+ obj_mock = Object.new
20
+ obj_stub = Object.new
21
+
22
+ stub(tns).new { obj_stub }
23
+ stub(obj_stub).attach
24
+ stub(inverse_klass)
25
+
26
+ mock(fns).new(klass, relation, fields) { obj_mock }
27
+ mock(obj_mock).attach
28
+ klass.send(:alize, relation, *fields)
29
+ end
30
+
31
+ it "should use #{tns} to push" do
32
+ obj_stub = Object.new
33
+ obj_mock = Object.new
34
+
35
+ stub(fns).new { obj_stub }
36
+ stub(obj_stub).attach
37
+ stub(klass).set_callback
38
+
39
+ mock(tns).new(inverse_klass, inverse_relation, fields) { obj_mock }
40
+ mock(obj_mock).attach
41
+ klass.send(:alize, relation, *fields)
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+
3
+ describe Mongoid::Alize::Callback do
4
+ def klass
5
+ Mongoid::Alize::Callback
6
+ end
7
+
8
+ def args
9
+ [Head, :person, [:name, :created_at]]
10
+ end
11
+
12
+ def new_unit
13
+ klass.new(*args)
14
+ end
15
+
16
+ describe "initialize" do
17
+ it "should assign class attributes" do
18
+ unit = new_unit
19
+ unit.klass.should == Head
20
+ unit.relation.should == :person
21
+ unit.inverse_klass = Person
22
+ unit.inverse_relation = :head
23
+ unit.fields.should == [:name, :created_at]
24
+ end
25
+ end
26
+ end
27
+
@@ -0,0 +1,53 @@
1
+ require 'spec_helper'
2
+
3
+ describe Mongoid::Alize::Callbacks::From::Many do
4
+ def klass
5
+ Mongoid::Alize::Callbacks::From::Many
6
+ end
7
+
8
+ def args
9
+ [Head, :wanted_by, [:name, :created_at]]
10
+ end
11
+
12
+ def new_unit
13
+ klass.new(*args)
14
+ end
15
+
16
+ describe "#define_fields" do
17
+ it "should define an Array called {relation}_fields" do
18
+ unit = new_unit
19
+ unit.send(:define_fields)
20
+ Head.fields["wanted_by_fields"].type.should == Array
21
+ end
22
+ end
23
+
24
+ describe "the defined callback" do
25
+ def run_callback
26
+ @head.send(callback_name)
27
+ end
28
+
29
+ def callback_name
30
+ "denormalize_from_wanted_by"
31
+ end
32
+
33
+ before do
34
+ @head = Head.create(
35
+ :wanted_by => [@person = Person.create(:name => "Bob",
36
+ :created_at => @now = Time.now)])
37
+
38
+ @unit = new_unit
39
+ @unit.send(:define_fields)
40
+ @unit.send(:define_callback)
41
+ end
42
+
43
+ it "should pull the fields from the relation" do
44
+ @head.wanted_by_fields.should be_nil
45
+ run_callback
46
+ @head.wanted_by_fields.should == [{
47
+ "_id" => @person.id,
48
+ "name"=> "Bob",
49
+ "created_at"=> @now.to_s(:utc)
50
+ }]
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,60 @@
1
+ require 'spec_helper'
2
+
3
+ describe Mongoid::Alize::Callbacks::From::One do
4
+ def klass
5
+ Mongoid::Alize::Callbacks::From::One
6
+ end
7
+
8
+ def args
9
+ [Head, :person, [:name, :created_at]]
10
+ end
11
+
12
+ def new_unit
13
+ klass.new(*args)
14
+ end
15
+
16
+ describe "#define_fields" do
17
+ it "should add properly typed, prefixed fields from the relation" do
18
+ unit = new_unit
19
+ unit.send(:define_fields)
20
+ Head.fields["person_name"].type.should == String
21
+ Head.fields["person_created_at"].type.should == Time
22
+ end
23
+ end
24
+
25
+ describe "the defined callback" do
26
+ def run_callback
27
+ @head.send(callback_name)
28
+ end
29
+
30
+ def callback_name
31
+ "denormalize_from_person"
32
+ end
33
+
34
+ before do
35
+ @head = Head.create(
36
+ :person => @person = Person.create(:name => @name = "Bob",
37
+ :created_at => @now = Time.now))
38
+ @unit = new_unit
39
+ @unit.send(:define_fields)
40
+ @unit.send(:define_callback)
41
+ end
42
+
43
+ it "should set pull fields from the relation" do
44
+ @head.person_name.should be_nil
45
+ @head.person_created_at.should be_nil
46
+ run_callback
47
+ @head.person_name.should == @name
48
+ @head.person_created_at.to_i.should == @now.to_i
49
+ end
50
+
51
+ it "should assign nil values if the relation is nil" do
52
+ @head.person_name = "not nil"
53
+ @head.person_created_at = Time.now
54
+ @head.person = nil
55
+ run_callback
56
+ @head.person_name.should be_nil
57
+ @head.person_created_at.should be_nil
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,97 @@
1
+ require 'spec_helper'
2
+
3
+ describe Mongoid::Alize::Callbacks::To::ManyFromMany do
4
+ def klass
5
+ Mongoid::Alize::Callbacks::To::ManyFromMany
6
+ end
7
+
8
+ def args
9
+ [Person, :wants, [:name, :created_at]]
10
+ end
11
+
12
+ def new_unit
13
+ klass.new(*args)
14
+ end
15
+
16
+ def wanted_by_fields
17
+ { "_id" => @person.id,
18
+ "name"=> "Bob",
19
+ "created_at"=> @now.to_s(:utc) }
20
+ end
21
+
22
+ def other_wanted_by
23
+ { "_id" => "SomeObjectId" }
24
+ end
25
+
26
+ before do
27
+ Head.class_eval do
28
+ field :wanted_by_fields, type: Array
29
+ end
30
+
31
+ @head = Head.create(
32
+ :wanted_by => [@person = Person.create(:name => "Bob",
33
+ :created_at => @now = Time.now)])
34
+ @person.wants = [@head]
35
+
36
+ @unit = new_unit
37
+ end
38
+
39
+ describe "#define_callback" do
40
+ before do
41
+ @unit.send(:define_callback)
42
+ end
43
+
44
+ def run_callback
45
+ @person.send(callback_name)
46
+ end
47
+
48
+ def callback_name
49
+ "denormalize_to_wants"
50
+ end
51
+
52
+ it "should push the fields to the relation" do
53
+ @head.wanted_by_fields.should be_nil
54
+ run_callback
55
+ @head.wanted_by_fields.should == [wanted_by_fields]
56
+ end
57
+
58
+ it "should pull first any existing array entries matching the _id" do
59
+ @head.wanted_by_fields = [other_wanted_by]
60
+ @head.save!
61
+
62
+ run_callback
63
+ run_callback
64
+
65
+ # to make sure persisted in both DB and updated in memory
66
+ @head.wanted_by_fields.should == [other_wanted_by, wanted_by_fields]
67
+ @head.reload
68
+ @head.wanted_by_fields.should == [other_wanted_by, wanted_by_fields]
69
+ end
70
+ end
71
+
72
+ describe "#define_destroy_callback" do
73
+ before do
74
+ @unit.send(:define_destroy_callback)
75
+ end
76
+
77
+ def run_destroy_callback
78
+ @person.send(destroy_callback_name)
79
+ end
80
+
81
+ def destroy_callback_name
82
+ "denormalize_destroy_to_wants"
83
+ end
84
+
85
+ it "should pull first any existing array entries matching the _id" do
86
+ @head.wanted_by_fields = [wanted_by_fields, other_wanted_by]
87
+ @head.save!
88
+
89
+ run_destroy_callback
90
+
91
+ # to make sure persisted in both DB and updated in memory
92
+ @head.wanted_by_fields.should == [other_wanted_by]
93
+ @head.reload
94
+ @head.wanted_by_fields.should == [other_wanted_by]
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,106 @@
1
+ require 'spec_helper'
2
+
3
+ describe Mongoid::Alize::Callbacks::To::ManyFromOne do
4
+ def klass
5
+ Mongoid::Alize::Callbacks::To::ManyFromOne
6
+ end
7
+
8
+ def args
9
+ [Person, :seen_by, [:name, :created_at]]
10
+ end
11
+
12
+ def new_unit
13
+ klass.new(*args)
14
+ end
15
+
16
+ def sees_fields
17
+ { "_id" => @person.id,
18
+ "name"=> "Bob",
19
+ "created_at"=> @now.to_s(:utc) }
20
+ end
21
+
22
+ def other_see
23
+ { "_id" => "SomeObjectId" }
24
+ end
25
+
26
+ before do
27
+ Head.class_eval do
28
+ field :sees_fields, :type => Array
29
+ end
30
+
31
+ @head = Head.create(
32
+ :sees => [@person = Person.create(:name => "Bob",
33
+ :created_at => @now = Time.now)])
34
+ @person.seen_by = @head
35
+ @unit = new_unit
36
+ end
37
+
38
+ describe "#define_callback" do
39
+ before do
40
+ @unit.send(:define_callback)
41
+ end
42
+
43
+ def run_callback
44
+ @person.send(callback_name)
45
+ end
46
+
47
+ def callback_name
48
+ "denormalize_to_seen_by"
49
+ end
50
+
51
+ it "should push the fields to the relation" do
52
+ @head.sees_fields.should be_nil
53
+ run_callback
54
+ @head.sees_fields.should == [sees_fields]
55
+ end
56
+
57
+ it "should pull first any existing array entries matching the _id" do
58
+ @head.sees_fields = [other_see]
59
+ @head.save!
60
+
61
+ run_callback
62
+ run_callback
63
+
64
+ # to make sure persisted in both DB and updated in memory
65
+ @head.sees_fields.should == [other_see, sees_fields]
66
+ @head.reload
67
+ @head.sees_fields.should == [other_see, sees_fields]
68
+ end
69
+
70
+ it "should do nothing if the inverse is nil" do
71
+ @person.seen_by = nil
72
+ run_callback
73
+ end
74
+ end
75
+
76
+ describe "#define_destroy_callback" do
77
+ before do
78
+ @unit.send(:define_destroy_callback)
79
+ end
80
+
81
+ def run_destroy_callback
82
+ @person.send(destroy_callback_name)
83
+ end
84
+
85
+ def destroy_callback_name
86
+ "denormalize_destroy_to_seen_by"
87
+ end
88
+
89
+ it "should pull first any existing array entries matching the _id" do
90
+ @head.sees_fields = [sees_fields, other_see]
91
+ @head.save!
92
+
93
+ run_destroy_callback
94
+
95
+ # to make sure persisted in both DB and updated in memory
96
+ @head.sees_fields.should == [other_see]
97
+ @head.reload
98
+ @head.sees_fields.should == [other_see]
99
+ end
100
+
101
+ it "should do nothing if the inverse is nil" do
102
+ @person.seen_by = nil
103
+ run_destroy_callback
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,73 @@
1
+ require 'spec_helper'
2
+
3
+ describe Mongoid::Alize::Callbacks::To::OneFromMany do
4
+ def klass
5
+ Mongoid::Alize::Callbacks::To::OneFromMany
6
+ end
7
+
8
+ def args
9
+ [Person, :heads, [:name, :created_at]]
10
+ end
11
+
12
+ def new_unit
13
+ klass.new(*args)
14
+ end
15
+
16
+ before do
17
+ Head.class_eval do
18
+ field :captor_name, :type => String
19
+ field :captor_created_at, :type => Time
20
+ end
21
+
22
+ @head = Head.create(
23
+ :captor => @person = Person.create(:name => "Bob",
24
+ :created_at => @now = Time.now))
25
+ @person.heads = [@head]
26
+
27
+ @unit = new_unit
28
+ end
29
+
30
+ describe "#define_callback" do
31
+ before do
32
+ @unit.send(:define_callback)
33
+ end
34
+
35
+ def run_callback
36
+ @person.send(callback_name)
37
+ end
38
+
39
+ def callback_name
40
+ "denormalize_to_heads"
41
+ end
42
+
43
+ it "should push the fields to the relation" do
44
+ @head.captor_name.should be_nil
45
+ @head.captor_created_at.should be_nil
46
+ run_callback
47
+ @head.captor_name.should == "Bob"
48
+ @head.captor_created_at.to_i.should == @now.to_i
49
+ end
50
+ end
51
+
52
+ describe "#define_destroy_callback" do
53
+ def run_destroy_callback
54
+ @person.send(destroy_callback_name)
55
+ end
56
+
57
+ def destroy_callback_name
58
+ "denormalize_destroy_to_heads"
59
+ end
60
+
61
+ before do
62
+ @unit.send(:define_destroy_callback)
63
+ end
64
+
65
+ it "should remove the fields from the relation" do
66
+ @head.captor_name.should be_nil
67
+ @head.captor_created_at.should be_nil
68
+ run_destroy_callback
69
+ @head.captor_name.should be_nil
70
+ @head.captor_created_at.should be_nil
71
+ end
72
+ end
73
+ end