mongoid_alize 0.2.0 → 0.3.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.
Files changed (32) hide show
  1. data/config/locales/en.yml +1 -0
  2. data/lib/mongoid/alize/callback.rb +48 -13
  3. data/lib/mongoid/alize/callbacks/from/many.rb +5 -5
  4. data/lib/mongoid/alize/callbacks/from/one.rb +14 -31
  5. data/lib/mongoid/alize/errors/invalid_configuration.rb +16 -0
  6. data/lib/mongoid/alize/from_callback.rb +4 -2
  7. data/lib/mongoid/alize/instance_helpers.rb +25 -0
  8. data/lib/mongoid/alize/macros.rb +65 -42
  9. data/lib/mongoid/alize/to_callback.rb +104 -9
  10. data/lib/mongoid_alize.rb +3 -7
  11. data/spec/app/models/head.rb +17 -1
  12. data/spec/app/models/mock_object.rb +6 -0
  13. data/spec/app/models/person.rb +16 -0
  14. data/spec/helpers/macros_helper.rb +11 -7
  15. data/spec/mongoid/alize/callback_spec.rb +62 -6
  16. data/spec/mongoid/alize/callbacks/from/one_spec.rb +41 -43
  17. data/spec/mongoid/alize/callbacks/to/many_from_many_spec.rb +23 -17
  18. data/spec/mongoid/alize/callbacks/to/many_from_one_spec.rb +158 -60
  19. data/spec/mongoid/alize/callbacks/to/one_from_many_spec.rb +110 -42
  20. data/spec/mongoid/alize/callbacks/to/one_from_one_spec.rb +167 -47
  21. data/spec/mongoid/alize/instance_helpers_spec.rb +36 -0
  22. data/spec/mongoid/alize/macros_spec.rb +90 -17
  23. data/spec/mongoid/alize/to_callback_spec.rb +87 -18
  24. data/spec/mongoid_alize_spec.rb +245 -26
  25. data/spec/spec_helper.rb +3 -1
  26. metadata +6 -8
  27. data/lib/mongoid/alize/callbacks/to/many_from_many.rb +0 -16
  28. data/lib/mongoid/alize/callbacks/to/many_from_one.rb +0 -15
  29. data/lib/mongoid/alize/callbacks/to/one_from_many.rb +0 -15
  30. data/lib/mongoid/alize/callbacks/to/one_from_one.rb +0 -15
  31. data/lib/mongoid/alize/to_many_callback.rb +0 -50
  32. data/lib/mongoid/alize/to_one_callback.rb +0 -43
@@ -1,99 +1,197 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Mongoid::Alize::Callbacks::To::ManyFromOne do
3
+ describe Mongoid::Alize::ToCallback do
4
4
  def klass
5
- Mongoid::Alize::Callbacks::To::ManyFromOne
6
- end
7
-
8
- def args
9
- [Person, :seen_by, [:name, :location, :created_at]]
5
+ Mongoid::Alize::ToCallback
10
6
  end
11
7
 
12
8
  def new_callback
13
9
  klass.new(*args)
14
10
  end
15
11
 
16
- def sees_fields
17
- { "_id" => @person.id,
18
- "name"=> "Bob",
19
- "location" => "Paris",
20
- "created_at"=> @now.to_s(:utc) }
12
+ def define_and_create(callback_name=:define_callback)
13
+ @callback = new_callback
14
+ @callback.send(:define_fields)
15
+ create_models
16
+ @callback.send(callback_name)
21
17
  end
22
18
 
23
- def other_see
24
- { "_id" => "SomeObjectId" }
25
- end
19
+ describe "with metadata in advance" do
20
+ def create_models
21
+ @head = Head.create(
22
+ :sees => [@person = Person.create(:name => "Bob",
23
+ :created_at => @now = Time.now)])
24
+ @person.seen_by = @head
25
+ end
26
26
 
27
- before do
28
- Head.class_eval do
29
- field :sees_fields, :type => Array
27
+ def args
28
+ [Person, :seen_by, [:name, :location, :created_at]]
30
29
  end
31
30
 
32
- @head = Head.create(
33
- :sees => [@person = Person.create(:name => "Bob",
34
- :created_at => @now = Time.now)])
35
- @person.seen_by = @head
36
- @callback = new_callback
37
- end
31
+ def sees_fields
32
+ { "_id" => @person.id,
33
+ "name"=> "Bob",
34
+ "location" => "Paris",
35
+ "created_at"=> @now.to_s(:utc) }
36
+ end
37
+
38
+ def other_see
39
+ { "_id" => "SomeObjectId" }
40
+ end
38
41
 
39
- describe "#define_callback" do
40
42
  before do
41
- @callback.send(:define_callback)
43
+ Head.class_eval do
44
+ field :sees_fields, :type => Array, :default => []
45
+ end
42
46
  end
43
47
 
44
- def run_callback
45
- @person.send(:_denormalize_to_seen_by)
48
+ describe "#define_callback" do
49
+ def run_callback
50
+ @person.send(:_denormalize_to_seen_by)
51
+ end
52
+
53
+ before do
54
+ define_and_create
55
+ end
56
+
57
+ it "should push the fields to the relation" do
58
+ @head.sees_fields.should == []
59
+ run_callback
60
+ @head.sees_fields.should == [sees_fields]
61
+ end
62
+
63
+ it "should pull first any existing array entries matching the _id" do
64
+ @head.sees_fields = [other_see]
65
+ @head.save!
66
+
67
+ run_callback
68
+ run_callback
69
+
70
+ # to make sure persisted in both DB and updated in memory
71
+ @head.sees_fields.should == [other_see, sees_fields]
72
+ @head.reload
73
+ @head.sees_fields.should == [other_see, sees_fields]
74
+ end
75
+
76
+ it "should do nothing if the inverse is nil" do
77
+ @person.seen_by = nil
78
+ run_callback
79
+ end
46
80
  end
47
81
 
48
- it "should push the fields to the relation" do
49
- @head.sees_fields.should be_nil
50
- run_callback
51
- @head.sees_fields.should == [sees_fields]
82
+ describe "#define_destroy_callback" do
83
+ def run_destroy_callback
84
+ @person.send(:_denormalize_destroy_to_seen_by)
85
+ end
86
+
87
+ before do
88
+ define_and_create(:define_destroy_callback)
89
+ end
90
+
91
+ it "should pull first any existing array entries matching the _id" do
92
+ @head.sees_fields = [sees_fields, other_see]
93
+ @head.save!
94
+
95
+ run_destroy_callback
96
+
97
+ # to make sure persisted in both DB and updated in memory
98
+ @head.sees_fields.should == [other_see]
99
+ @head.reload
100
+ @head.sees_fields.should == [other_see]
101
+ end
102
+
103
+ it "should do nothing if the inverse is nil" do
104
+ @person.seen_by = nil
105
+ run_destroy_callback
106
+ end
52
107
  end
108
+ end
53
109
 
54
- it "should pull first any existing array entries matching the _id" do
55
- @head.sees_fields = [other_see]
56
- @head.save!
110
+ describe "with a polymorphic model" do
111
+ def create_models
112
+ @person = Person.create(:name => @name = "Bob", :created_at => @now = Time.now)
113
+ @head = Head.create(:size => @size = 10)
114
+ @person.above = @head
115
+ end
57
116
 
58
- run_callback
59
- run_callback
117
+ def args
118
+ [Person, :above, [:name]]
119
+ end
60
120
 
61
- # to make sure persisted in both DB and updated in memory
62
- @head.sees_fields.should == [other_see, sees_fields]
63
- @head.reload
64
- @head.sees_fields.should == [other_see, sees_fields]
121
+ def below_people_fields
122
+ { "_id" => @person.id,
123
+ "name"=> @name }
65
124
  end
66
125
 
67
- it "should do nothing if the inverse is nil" do
68
- @person.seen_by = nil
69
- run_callback
126
+ def other_below_people
127
+ { "_id" => "SomeObjectId" }
70
128
  end
71
- end
72
129
 
73
- describe "#define_destroy_callback" do
74
130
  before do
75
- @callback.send(:define_destroy_callback)
131
+ Head.class_eval do
132
+ field :below_people_fields, :type => Array, :default => []
133
+ end
76
134
  end
77
135
 
78
- def run_destroy_callback
79
- @person.send(:_denormalize_destroy_to_seen_by)
136
+ describe "#define_callback" do
137
+ def run_callback
138
+ @person.send(:_denormalize_to_above)
139
+ end
140
+
141
+ before do
142
+ define_and_create
143
+ end
144
+
145
+ it "should push the fields to the relation" do
146
+ @head.below_people_fields.should == []
147
+ run_callback
148
+ @head.below_people_fields.should == [below_people_fields]
149
+ end
150
+
151
+ it "should pull first any existing array entries matching the _id" do
152
+ @head.below_people_fields = [other_below_people]
153
+ @head.save!
154
+
155
+ run_callback
156
+ run_callback
157
+
158
+ # to make sure persisted in both DB and updated in memory
159
+ @head.below_people_fields.should == [other_below_people, below_people_fields]
160
+ @head.reload
161
+ @head.below_people_fields.should == [other_below_people, below_people_fields]
162
+ end
163
+
164
+ it "should do nothing if the inverse is nil" do
165
+ @person.above = nil
166
+ run_callback
167
+ end
80
168
  end
81
169
 
82
- it "should pull first any existing array entries matching the _id" do
83
- @head.sees_fields = [sees_fields, other_see]
84
- @head.save!
170
+ describe "#define_destroy_callback" do
171
+ def run_destroy_callback
172
+ @person.send(:_denormalize_destroy_to_above)
173
+ end
85
174
 
86
- run_destroy_callback
175
+ before do
176
+ define_and_create(:define_destroy_callback)
177
+ end
87
178
 
88
- # to make sure persisted in both DB and updated in memory
89
- @head.sees_fields.should == [other_see]
90
- @head.reload
91
- @head.sees_fields.should == [other_see]
92
- end
179
+ it "should pull first any existing array entries matching the _id" do
180
+ @head.below_people_fields = [below_people_fields, other_below_people]
181
+ @head.save!
182
+
183
+ run_destroy_callback
184
+
185
+ # to make sure persisted in both DB and updated in memory
186
+ @head.below_people_fields.should == [other_below_people]
187
+ @head.reload
188
+ @head.below_people_fields.should == [other_below_people]
189
+ end
93
190
 
94
- it "should do nothing if the inverse is nil" do
95
- @person.seen_by = nil
96
- run_destroy_callback
191
+ it "should do nothing if the inverse is nil" do
192
+ @person.above = nil
193
+ run_destroy_callback
194
+ end
97
195
  end
98
196
  end
99
197
  end
@@ -1,70 +1,138 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Mongoid::Alize::Callbacks::To::OneFromMany do
3
+ describe Mongoid::Alize::ToCallback do
4
4
  def klass
5
- Mongoid::Alize::Callbacks::To::OneFromMany
6
- end
7
-
8
- def args
9
- [Person, :heads, [:name, :location, :created_at]]
5
+ Mongoid::Alize::ToCallback
10
6
  end
11
7
 
12
8
  def new_callback
13
9
  klass.new(*args)
14
10
  end
15
11
 
16
- before do
17
- Head.class_eval do
18
- field :captor_name, :type => String
19
- field :captor_location, :type => String
20
- field :captor_created_at, :type => Time
21
- end
22
-
23
- @head = Head.create(
24
- :captor => @person = Person.create(:name => "Bob",
25
- :created_at => @now = Time.now))
26
- @person.heads = [@head]
27
-
12
+ def define_and_create(callback_name=:define_callback)
28
13
  @callback = new_callback
14
+ @callback.send(:define_fields)
15
+ create_models
16
+ @callback.send(callback_name)
29
17
  end
30
18
 
31
- describe "#define_callback" do
19
+ describe "with metadata in advance" do
20
+ def create_models
21
+ @head = Head.create(
22
+ :captor => @person = Person.create(:name => "Bob", :created_at => @now = Time.now))
23
+ @person.heads = [@head]
24
+ end
25
+
26
+ def args
27
+ [Person, :heads, [:name, :location, :created_at]]
28
+ end
29
+
32
30
  before do
33
- @callback.send(:define_callback)
31
+ Head.class_eval do
32
+ field :captor_fields, :type => Hash, :default => nil
33
+ end
34
34
  end
35
35
 
36
- def run_callback
37
- @person.send(:_denormalize_to_heads)
36
+ describe "#define_callback" do
37
+ def captor_fields
38
+ { "name"=> "Bob",
39
+ "location" => "Paris",
40
+ "created_at"=> @now.to_s(:utc) }
41
+ end
42
+
43
+ def run_callback
44
+ @person.send(:_denormalize_to_heads)
45
+ end
46
+
47
+ before do
48
+ define_and_create
49
+ end
50
+
51
+ it "should push the fields to the relation" do
52
+ @head.captor_fields.should be_nil
53
+ run_callback
54
+ @head.captor_fields.should == captor_fields
55
+ end
38
56
  end
39
57
 
40
- it "should push the fields to the relation" do
41
- @head.captor_name.should be_nil
42
- @head.captor_location.should be_nil
43
- @head.captor_created_at.should be_nil
44
- run_callback
45
- @head.captor_name.should == "Bob"
46
- @head.captor_location.should == "Paris"
47
- @head.captor_created_at.to_i.should == @now.to_i
58
+ describe "#define_destroy_callback" do
59
+ def run_destroy_callback
60
+ @person.send(:_denormalize_destroy_to_heads)
61
+ end
62
+
63
+ before do
64
+ define_and_create(:define_destroy_callback)
65
+ end
66
+
67
+ it "should remove the fields from the relation" do
68
+ @head.captor_fields = { "hi" => "hello" }
69
+ run_destroy_callback
70
+ @head.captor_fields.should be_nil
71
+ end
72
+
73
+ it "should do nothing if the relation doesn't exist" do
74
+ @head.captor = nil
75
+ run_destroy_callback
76
+ end
48
77
  end
49
78
  end
50
79
 
51
- describe "#define_destroy_callback" do
52
- def run_destroy_callback
53
- @person.send(:_denormalize_destroy_to_heads)
80
+ describe "with a polymorphic relationship" do
81
+ def create_models
82
+ @person = Person.create(:name => "Bob", :created_at => @now = Time.now)
83
+ @head = Head.create(:size => @size = 10)
84
+ @person.above = @head
85
+ end
86
+
87
+ def args
88
+ [Head, :below_people, [:size]]
54
89
  end
55
90
 
56
91
  before do
57
- @callback.send(:define_destroy_callback)
92
+ Person.class_eval do
93
+ field :above_fields, :type => Hash, :default => nil
94
+ end
95
+ end
96
+
97
+ describe "#define_callback" do
98
+ def above_fields
99
+ { "size"=> @size }
100
+ end
101
+
102
+ def run_callback
103
+ @head.send(:_denormalize_to_below_people)
104
+ end
105
+
106
+ before do
107
+ define_and_create
108
+ end
109
+
110
+ it "should push the fields to the relation" do
111
+ @person.above_fields.should be_nil
112
+ run_callback
113
+ @person.above_fields.should == above_fields
114
+ end
58
115
  end
59
116
 
60
- it "should remove the fields from the relation" do
61
- @head.captor_name = "Chuck"
62
- @head.captor_location = "Paris"
63
- @head.captor_created_at = Time.now
64
- run_destroy_callback
65
- @head.captor_name.should be_nil
66
- @head.captor_location.should be_nil
67
- @head.captor_created_at.should be_nil
117
+ describe "#define_destroy_callback" do
118
+ before do
119
+ define_and_create(:define_destroy_callback)
120
+ end
121
+
122
+ def run_destroy_callback
123
+ @head.send(:_denormalize_destroy_to_below_people)
124
+ end
125
+
126
+ it "should remove the fields from the relation" do
127
+ @person.above_fields = { "hi" => "hello" }
128
+ run_destroy_callback
129
+ @person.above_fields.should be_nil
130
+ end
131
+
132
+ it "should do nothing if the relation doesn't exist" do
133
+ @person.above = nil
134
+ run_destroy_callback
135
+ end
68
136
  end
69
137
  end
70
138
  end
@@ -1,79 +1,199 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Mongoid::Alize::Callbacks::To::OneFromOne do
3
+ describe Mongoid::Alize::ToCallback do
4
4
  def klass
5
- Mongoid::Alize::Callbacks::To::OneFromOne
6
- end
7
-
8
- def args
9
- [Person, :head, [:name, :location, :created_at]]
5
+ Mongoid::Alize::ToCallback
10
6
  end
11
7
 
12
8
  def new_callback
13
9
  klass.new(*args)
14
10
  end
15
11
 
16
- before do
17
- Head.class_eval do
18
- field :person_name, :type => String
19
- field :person_location, :type => String
20
- field :person_created_at, :type => Time
12
+ def define_and_create(callback_name=:define_callback)
13
+ @callback = new_callback
14
+ @callback.send(:define_fields)
15
+ create_models
16
+ @callback.send(callback_name)
17
+ end
18
+
19
+ describe "with metadata in advance" do
20
+ def args
21
+ [Person, :head, [:name, :location, :created_at]]
21
22
  end
22
23
 
23
- @head = Head.create(
24
- :person => @person = Person.create(:name => "Bob",
25
- :created_at => @now = Time.now))
24
+ def create_models
25
+ @head = Head.create(
26
+ :person => @person = Person.create(:name => "Bob",
27
+ :created_at => @now = Time.now))
28
+ end
26
29
 
27
- @callback = new_callback
30
+ before do
31
+ Head.class_eval do
32
+ field :person_fields, :type => Hash, :default => nil
33
+ end
34
+ end
35
+
36
+ describe "define_callback" do
37
+ def person_fields
38
+ { "name"=> "Bob",
39
+ "location" => "Paris",
40
+ "created_at"=> @now.to_s(:utc) }
41
+ end
42
+
43
+ def run_callback
44
+ @person.send(:_denormalize_to_head)
45
+ end
46
+
47
+ before do
48
+ define_and_create
49
+ end
50
+
51
+ it "should push the fields to the relation" do
52
+ @head.person_fields.should == nil
53
+ run_callback
54
+ @head.person_fields.should == person_fields
55
+ end
56
+ end
57
+
58
+ describe "define_destroy_callback" do
59
+ def run_destroy_callback
60
+ @person.send(:_denormalize_destroy_to_head)
61
+ end
62
+
63
+ before do
64
+ define_and_create(:define_destroy_callback)
65
+ end
66
+
67
+ it "should nillify the fields in the relation" do
68
+ @head.person_fields = { "hi" => "hello" }
69
+ run_destroy_callback
70
+ @head.person_fields.should be_nil
71
+ end
72
+
73
+ it "should do nothing if the relation doesn't exist" do
74
+ @head.person = nil
75
+ run_destroy_callback
76
+ end
77
+ end
28
78
  end
29
79
 
30
- describe "define_callback" do
31
- before do
32
- @callback.send(:define_callback)
80
+ describe "to a polymorphic child" do
81
+ def args
82
+ [Person, :nearest_head, [:name, :location, :created_at]]
33
83
  end
34
84
 
35
- def run_callback
36
- @person.send(:_denormalize_to_head)
85
+ def create_models
86
+ @head = Head.create(
87
+ :nearest => @person = Person.create(:name => @name = "Bob",
88
+ :created_at => @now = Time.now))
37
89
  end
38
90
 
39
- it "should push the fields to the relation" do
40
- @head.person_name.should be_nil
41
- @head.person_location.should be_nil
42
- @head.person_created_at.should be_nil
43
- run_callback
44
- @head.person_name.should == "Bob"
45
- @head.person_location.should == "Paris"
46
- @head.person_created_at.to_i.should == @now.to_i
91
+ before do
92
+ Head.class_eval do
93
+ field :nearest_fields, :type => Hash, :default => nil
94
+ end
95
+ end
96
+
97
+ describe "define_callback" do
98
+ def nearest_fields
99
+ { "name"=> "Bob",
100
+ "location" => "Paris",
101
+ "created_at"=> @now.to_s(:utc) }
102
+ end
103
+
104
+ def run_callback
105
+ @person.send(:_denormalize_to_nearest_head)
106
+ end
107
+
108
+ before do
109
+ define_and_create
110
+ end
111
+
112
+ it "should push the fields to the relation" do
113
+ @head.nearest_fields.should be_nil
114
+ run_callback
115
+ @head.nearest_fields.should == nearest_fields
116
+ end
47
117
  end
48
118
 
49
- it "should do nothing if the relation doesn't exist" do
50
- @head.person = nil
51
- run_callback
119
+ describe "define_destroy_callback" do
120
+ def run_destroy_callback
121
+ @person.send(:_denormalize_destroy_to_nearest_head)
122
+ end
123
+
124
+ before do
125
+ define_and_create(:define_destroy_callback)
126
+ end
127
+
128
+ it "should nillify the fields in the relation" do
129
+ @head.nearest_fields = { "hi" => "hello" }
130
+ run_destroy_callback
131
+ @head.nearest_fields.should be_nil
132
+ end
133
+
134
+ it "should do nothing if the relation doesn't exist" do
135
+ @head.nearest = nil
136
+ run_destroy_callback
137
+ end
52
138
  end
53
139
  end
54
140
 
55
- describe "define_destroy_callback" do
56
- before do
57
- @callback.send(:define_destroy_callback)
141
+ describe "to a polymorphic parent" do
142
+ def args
143
+ [Head, :nearest, [:size]]
58
144
  end
59
145
 
60
- def run_destroy_callback
61
- @person.send(:_denormalize_destroy_to_head)
146
+ def create_models
147
+ @head = Head.create(
148
+ :nearest => @person = Person.create(:name => @name = "Bob",
149
+ :created_at => @now = Time.now))
62
150
  end
63
151
 
64
- it "should nillify the fields in the relation" do
65
- @head.person_name = "Chuck"
66
- @head.person_location = "Paris"
67
- @head.person_created_at = Time.now
68
- run_destroy_callback
69
- @head.person_name.should be_nil
70
- @head.person_location.should be_nil
71
- @head.person_created_at.should be_nil
152
+ before do
153
+ Person.class_eval do
154
+ field :nearest_head_fields, :type => Hash, :default => nil
155
+ end
72
156
  end
73
157
 
74
- it "should do nothing if the relation doesn't exist" do
75
- @head.person = nil
76
- run_destroy_callback
158
+ describe "define_callback" do
159
+ def nearest_head_fields
160
+ { "size"=> @size }
161
+ end
162
+
163
+ def run_callback
164
+ @head.send(:_denormalize_to_nearest)
165
+ end
166
+
167
+ before do
168
+ define_and_create
169
+ end
170
+
171
+ it "should push the fields to the relation" do
172
+ @person.nearest_head_fields.should be_nil
173
+ run_callback
174
+ @person.nearest_head_fields.should == nearest_head_fields
175
+ end
176
+ end
177
+
178
+ describe "define_destroy_callback" do
179
+ def run_destroy_callback
180
+ @head.send(:_denormalize_destroy_to_nearest)
181
+ end
182
+
183
+ before do
184
+ define_and_create(:define_destroy_callback)
185
+ end
186
+
187
+ it "should nillify the fields in the relation" do
188
+ @person.nearest_head_fields = { "hi" => "hello" }
189
+ run_destroy_callback
190
+ @person.nearest_head_fields.should be_nil
191
+ end
192
+
193
+ it "should do nothing if the relation doesn't exist" do
194
+ @person.nearest_head = nil
195
+ run_destroy_callback
196
+ end
77
197
  end
78
198
  end
79
199
  end