mongo_mapper-rails3 0.7.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +10 -0
- data/Gemfile +15 -0
- data/LICENSE +20 -0
- data/README.rdoc +60 -0
- data/Rakefile +58 -0
- data/VERSION +1 -0
- data/bin/mmconsole +60 -0
- data/lib/mongo_mapper.rb +131 -0
- data/lib/mongo_mapper/document.rb +439 -0
- data/lib/mongo_mapper/embedded_document.rb +68 -0
- data/lib/mongo_mapper/plugins.rb +30 -0
- data/lib/mongo_mapper/plugins/associations.rb +106 -0
- data/lib/mongo_mapper/plugins/associations/base.rb +123 -0
- data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +30 -0
- data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +25 -0
- data/lib/mongo_mapper/plugins/associations/collection.rb +21 -0
- data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +50 -0
- data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +141 -0
- data/lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb +28 -0
- data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +120 -0
- data/lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +31 -0
- data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +23 -0
- data/lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb +13 -0
- data/lib/mongo_mapper/plugins/associations/one_proxy.rb +68 -0
- data/lib/mongo_mapper/plugins/associations/proxy.rb +119 -0
- data/lib/mongo_mapper/plugins/callbacks.rb +87 -0
- data/lib/mongo_mapper/plugins/clone.rb +14 -0
- data/lib/mongo_mapper/plugins/descendants.rb +17 -0
- data/lib/mongo_mapper/plugins/dirty.rb +120 -0
- data/lib/mongo_mapper/plugins/equality.rb +24 -0
- data/lib/mongo_mapper/plugins/identity_map.rb +124 -0
- data/lib/mongo_mapper/plugins/inspect.rb +15 -0
- data/lib/mongo_mapper/plugins/keys.rb +310 -0
- data/lib/mongo_mapper/plugins/logger.rb +19 -0
- data/lib/mongo_mapper/plugins/pagination.rb +26 -0
- data/lib/mongo_mapper/plugins/pagination/proxy.rb +72 -0
- data/lib/mongo_mapper/plugins/protected.rb +46 -0
- data/lib/mongo_mapper/plugins/rails.rb +46 -0
- data/lib/mongo_mapper/plugins/serialization.rb +50 -0
- data/lib/mongo_mapper/plugins/validations.rb +88 -0
- data/lib/mongo_mapper/query.rb +130 -0
- data/lib/mongo_mapper/support.rb +217 -0
- data/lib/mongo_mapper/support/descendant_appends.rb +46 -0
- data/lib/mongo_mapper/support/find.rb +77 -0
- data/mongo_mapper-rails3.gemspec +208 -0
- data/performance/read_write.rb +52 -0
- data/specs.watchr +51 -0
- data/test/NOTE_ON_TESTING +1 -0
- data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +63 -0
- data/test/functional/associations/test_belongs_to_proxy.rb +101 -0
- data/test/functional/associations/test_in_array_proxy.rb +321 -0
- data/test/functional/associations/test_many_documents_as_proxy.rb +229 -0
- data/test/functional/associations/test_many_documents_proxy.rb +453 -0
- data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +176 -0
- data/test/functional/associations/test_many_embedded_proxy.rb +256 -0
- data/test/functional/associations/test_many_polymorphic_proxy.rb +302 -0
- data/test/functional/associations/test_one_proxy.rb +161 -0
- data/test/functional/test_associations.rb +44 -0
- data/test/functional/test_binary.rb +27 -0
- data/test/functional/test_callbacks.rb +81 -0
- data/test/functional/test_dirty.rb +163 -0
- data/test/functional/test_document.rb +1244 -0
- data/test/functional/test_embedded_document.rb +125 -0
- data/test/functional/test_identity_map.rb +508 -0
- data/test/functional/test_logger.rb +20 -0
- data/test/functional/test_modifiers.rb +252 -0
- data/test/functional/test_pagination.rb +93 -0
- data/test/functional/test_protected.rb +161 -0
- data/test/functional/test_string_id_compatibility.rb +67 -0
- data/test/functional/test_validations.rb +329 -0
- data/test/models.rb +232 -0
- data/test/support/custom_matchers.rb +55 -0
- data/test/support/timing.rb +16 -0
- data/test/test_helper.rb +59 -0
- data/test/unit/associations/test_base.rb +207 -0
- data/test/unit/associations/test_proxy.rb +105 -0
- data/test/unit/serializers/test_json_serializer.rb +189 -0
- data/test/unit/test_descendant_appends.rb +71 -0
- data/test/unit/test_document.rb +231 -0
- data/test/unit/test_dynamic_finder.rb +123 -0
- data/test/unit/test_embedded_document.rb +663 -0
- data/test/unit/test_keys.rb +169 -0
- data/test/unit/test_lint.rb +8 -0
- data/test/unit/test_mongo_mapper.rb +125 -0
- data/test/unit/test_pagination.rb +160 -0
- data/test/unit/test_plugins.rb +51 -0
- data/test/unit/test_query.rb +334 -0
- data/test/unit/test_rails.rb +123 -0
- data/test/unit/test_rails_compatibility.rb +57 -0
- data/test/unit/test_serialization.rb +51 -0
- data/test/unit/test_support.rb +362 -0
- data/test/unit/test_time_zones.rb +39 -0
- data/test/unit/test_validations.rb +557 -0
- metadata +344 -0
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class LoggerTest < Test::Unit::TestCase
|
4
|
+
context "with connection that has logger" do
|
5
|
+
setup do
|
6
|
+
@output = StringIO.new
|
7
|
+
@logger = Logger.new(@output)
|
8
|
+
MongoMapper.connection = Mongo::Connection.new('127.0.0.1', 27017, :logger => @logger)
|
9
|
+
end
|
10
|
+
|
11
|
+
should "be able to get access to that logger" do
|
12
|
+
MongoMapper.logger.should == @logger
|
13
|
+
end
|
14
|
+
|
15
|
+
should "be able to log messages" do
|
16
|
+
MongoMapper.logger.debug 'testing'
|
17
|
+
@output.string.include?('testing').should be_true
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,252 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ModifierTest < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@page_class = Doc do
|
6
|
+
key :title, String
|
7
|
+
key :day_count, Integer, :default => 0
|
8
|
+
key :week_count, Integer, :default => 0
|
9
|
+
key :month_count, Integer, :default => 0
|
10
|
+
key :tags, Array
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def assert_page_counts(page, day_count, week_count, month_count)
|
15
|
+
page.reload
|
16
|
+
page.day_count.should == day_count
|
17
|
+
page.week_count.should == week_count
|
18
|
+
page.month_count.should == month_count
|
19
|
+
end
|
20
|
+
|
21
|
+
should "be able to increment with criteria and modifier hashes" do
|
22
|
+
page = @page_class.create(:title => 'Home')
|
23
|
+
page2 = @page_class.create(:title => 'Home')
|
24
|
+
|
25
|
+
@page_class.increment({:title => 'Home'}, {
|
26
|
+
:day_count => 1, :week_count => 2, :month_count => 3
|
27
|
+
})
|
28
|
+
|
29
|
+
assert_page_counts page, 1, 2, 3
|
30
|
+
assert_page_counts page2, 1, 2, 3
|
31
|
+
end
|
32
|
+
|
33
|
+
should "be able to increment with ids and modifier hash" do
|
34
|
+
page = @page_class.create(:title => 'Home')
|
35
|
+
page2 = @page_class.create(:title => 'Home')
|
36
|
+
|
37
|
+
@page_class.increment(page.id, page2.id, {
|
38
|
+
:day_count => 1, :week_count => 2, :month_count => 3
|
39
|
+
})
|
40
|
+
|
41
|
+
assert_page_counts page, 1, 2, 3
|
42
|
+
assert_page_counts page2, 1, 2, 3
|
43
|
+
end
|
44
|
+
|
45
|
+
should "be able to decrement with criteria and modifier hashes" do
|
46
|
+
page = @page_class.create(:title => 'Home', :day_count => 1, :week_count => 2, :month_count => 3)
|
47
|
+
page2 = @page_class.create(:title => 'Home', :day_count => 1, :week_count => 2, :month_count => 3)
|
48
|
+
|
49
|
+
@page_class.decrement({:title => 'Home'}, {
|
50
|
+
:day_count => 1, :week_count => 2, :month_count => 3
|
51
|
+
})
|
52
|
+
|
53
|
+
assert_page_counts page, 0, 0, 0
|
54
|
+
assert_page_counts page2, 0, 0, 0
|
55
|
+
end
|
56
|
+
|
57
|
+
should "be able to decrement with ids and modifier hash" do
|
58
|
+
page = @page_class.create(:title => 'Home', :day_count => 1, :week_count => 2, :month_count => 3)
|
59
|
+
page2 = @page_class.create(:title => 'Home', :day_count => 1, :week_count => 2, :month_count => 3)
|
60
|
+
|
61
|
+
@page_class.decrement(page.id, page2.id, {
|
62
|
+
:day_count => 1, :week_count => 2, :month_count => 3
|
63
|
+
})
|
64
|
+
|
65
|
+
assert_page_counts page, 0, 0, 0
|
66
|
+
assert_page_counts page2, 0, 0, 0
|
67
|
+
end
|
68
|
+
|
69
|
+
should "always decrement when decrement is called whether number is positive or negative" do
|
70
|
+
page = @page_class.create(:title => 'Home', :day_count => 1, :week_count => 2, :month_count => 3)
|
71
|
+
page2 = @page_class.create(:title => 'Home', :day_count => 1, :week_count => 2, :month_count => 3)
|
72
|
+
|
73
|
+
@page_class.decrement(page.id, page2.id, {
|
74
|
+
:day_count => -1, :week_count => 2, :month_count => -3
|
75
|
+
})
|
76
|
+
|
77
|
+
assert_page_counts page, 0, 0, 0
|
78
|
+
assert_page_counts page2, 0, 0, 0
|
79
|
+
end
|
80
|
+
|
81
|
+
should "be able to set with criteria and modifier hashes" do
|
82
|
+
page = @page_class.create(:title => 'Home')
|
83
|
+
page2 = @page_class.create(:title => 'Home')
|
84
|
+
|
85
|
+
@page_class.set({:title => 'Home'}, :title => 'Home Revised')
|
86
|
+
|
87
|
+
page.reload
|
88
|
+
page.title.should == 'Home Revised'
|
89
|
+
|
90
|
+
page2.reload
|
91
|
+
page2.title.should == 'Home Revised'
|
92
|
+
end
|
93
|
+
|
94
|
+
should "be able to set with ids and modifier hash" do
|
95
|
+
page = @page_class.create(:title => 'Home')
|
96
|
+
page2 = @page_class.create(:title => 'Home')
|
97
|
+
|
98
|
+
@page_class.set(page.id, page2.id, :title => 'Home Revised')
|
99
|
+
|
100
|
+
page.reload
|
101
|
+
page.title.should == 'Home Revised'
|
102
|
+
|
103
|
+
page2.reload
|
104
|
+
page2.title.should == 'Home Revised'
|
105
|
+
end
|
106
|
+
|
107
|
+
should "be able to push with criteria and modifier hashes" do
|
108
|
+
page = @page_class.create(:title => 'Home')
|
109
|
+
page2 = @page_class.create(:title => 'Home')
|
110
|
+
|
111
|
+
@page_class.push({:title => 'Home'}, :tags => 'foo')
|
112
|
+
|
113
|
+
page.reload
|
114
|
+
page.tags.should == %w(foo)
|
115
|
+
|
116
|
+
page2.reload
|
117
|
+
page.tags.should == %w(foo)
|
118
|
+
end
|
119
|
+
|
120
|
+
should "be able to push with ids and modifier hash" do
|
121
|
+
page = @page_class.create(:title => 'Home')
|
122
|
+
page2 = @page_class.create(:title => 'Home')
|
123
|
+
|
124
|
+
@page_class.push(page.id, page2.id, :tags => 'foo')
|
125
|
+
|
126
|
+
page.reload
|
127
|
+
page.tags.should == %w(foo)
|
128
|
+
|
129
|
+
page2.reload
|
130
|
+
page.tags.should == %w(foo)
|
131
|
+
end
|
132
|
+
|
133
|
+
should "be able to push all with criteria and modifier hashes" do
|
134
|
+
page = @page_class.create(:title => 'Home')
|
135
|
+
page2 = @page_class.create(:title => 'Home')
|
136
|
+
tags = %w(foo bar)
|
137
|
+
|
138
|
+
@page_class.push_all({:title => 'Home'}, :tags => tags)
|
139
|
+
|
140
|
+
page.reload
|
141
|
+
page.tags.should == tags
|
142
|
+
|
143
|
+
page2.reload
|
144
|
+
page.tags.should == tags
|
145
|
+
end
|
146
|
+
|
147
|
+
should "be able to push all with ids and modifier hash" do
|
148
|
+
page = @page_class.create(:title => 'Home')
|
149
|
+
page2 = @page_class.create(:title => 'Home')
|
150
|
+
tags = %w(foo bar)
|
151
|
+
|
152
|
+
@page_class.push_all(page.id, page2.id, :tags => tags)
|
153
|
+
|
154
|
+
page.reload
|
155
|
+
page.tags.should == tags
|
156
|
+
|
157
|
+
page2.reload
|
158
|
+
page.tags.should == tags
|
159
|
+
end
|
160
|
+
|
161
|
+
should "be able to pull with criteria and modifier hashes" do
|
162
|
+
page = @page_class.create(:title => 'Home', :tags => %w(foo bar))
|
163
|
+
page2 = @page_class.create(:title => 'Home', :tags => %w(foo bar))
|
164
|
+
|
165
|
+
@page_class.pull({:title => 'Home'}, :tags => 'foo')
|
166
|
+
|
167
|
+
page.reload
|
168
|
+
page.tags.should == %w(bar)
|
169
|
+
|
170
|
+
page2.reload
|
171
|
+
page.tags.should == %w(bar)
|
172
|
+
end
|
173
|
+
|
174
|
+
should "be able to pull with ids and modifier hash" do
|
175
|
+
page = @page_class.create(:title => 'Home', :tags => %w(foo bar))
|
176
|
+
page2 = @page_class.create(:title => 'Home', :tags => %w(foo bar))
|
177
|
+
|
178
|
+
@page_class.pull(page.id, page2.id, :tags => 'foo')
|
179
|
+
|
180
|
+
page.reload
|
181
|
+
page.tags.should == %w(bar)
|
182
|
+
|
183
|
+
page2.reload
|
184
|
+
page.tags.should == %w(bar)
|
185
|
+
end
|
186
|
+
|
187
|
+
should "be able to pull all with criteria and modifier hashes" do
|
188
|
+
page = @page_class.create(:title => 'Home', :tags => %w(foo bar baz))
|
189
|
+
page2 = @page_class.create(:title => 'Home', :tags => %w(foo bar baz))
|
190
|
+
|
191
|
+
@page_class.pull_all({:title => 'Home'}, :tags => %w(foo bar))
|
192
|
+
|
193
|
+
page.reload
|
194
|
+
page.tags.should == %w(baz)
|
195
|
+
|
196
|
+
page2.reload
|
197
|
+
page.tags.should == %w(baz)
|
198
|
+
end
|
199
|
+
|
200
|
+
should "be able to pull all with ids and modifier hash" do
|
201
|
+
page = @page_class.create(:title => 'Home', :tags => %w(foo bar baz))
|
202
|
+
page2 = @page_class.create(:title => 'Home', :tags => %w(foo bar baz))
|
203
|
+
|
204
|
+
@page_class.pull_all(page.id, page2.id, :tags => %w(foo bar))
|
205
|
+
|
206
|
+
page.reload
|
207
|
+
page.tags.should == %w(baz)
|
208
|
+
|
209
|
+
page2.reload
|
210
|
+
page.tags.should == %w(baz)
|
211
|
+
end
|
212
|
+
|
213
|
+
should "be able to push uniq with criteria and modifier hash" do
|
214
|
+
page = @page_class.create(:title => 'Home', :tags => 'foo')
|
215
|
+
page2 = @page_class.create(:title => 'Home')
|
216
|
+
|
217
|
+
@page_class.push_uniq({:title => 'Home'}, :tags => 'foo')
|
218
|
+
|
219
|
+
page.reload
|
220
|
+
page.tags.should == %w(foo)
|
221
|
+
|
222
|
+
page2.reload
|
223
|
+
page.tags.should == %w(foo)
|
224
|
+
end
|
225
|
+
|
226
|
+
should "be able to push uniq with ids and modifier hash" do
|
227
|
+
page = @page_class.create(:title => 'Home', :tags => 'foo')
|
228
|
+
page2 = @page_class.create(:title => 'Home')
|
229
|
+
|
230
|
+
@page_class.push_uniq(page.id, page2.id, :tags => 'foo')
|
231
|
+
|
232
|
+
page.reload
|
233
|
+
page.tags.should == %w(foo)
|
234
|
+
|
235
|
+
page2.reload
|
236
|
+
page.tags.should == %w(foo)
|
237
|
+
end
|
238
|
+
|
239
|
+
should "be able to remove the last element the array" do
|
240
|
+
page = @page_class.create(:title => 'Home', :tags => %w(foo bar))
|
241
|
+
@page_class.pop(page.id, :tags => 1)
|
242
|
+
page.reload
|
243
|
+
page.tags.should == %w(foo)
|
244
|
+
end
|
245
|
+
|
246
|
+
should "be able to remove the first element of the array" do
|
247
|
+
page = @page_class.create(:title => 'Home', :tags => %w(foo bar))
|
248
|
+
@page_class.pop(page.id, :tags => -1)
|
249
|
+
page.reload
|
250
|
+
page.tags.should == %w(bar)
|
251
|
+
end
|
252
|
+
end
|
@@ -0,0 +1,93 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class PaginationTest < Test::Unit::TestCase
|
4
|
+
context "Paginating" do
|
5
|
+
setup do
|
6
|
+
@document = Doc do
|
7
|
+
set_collection_name 'users'
|
8
|
+
|
9
|
+
key :first_name, String
|
10
|
+
key :last_name, String
|
11
|
+
key :age, Integer
|
12
|
+
|
13
|
+
def self.per_page; 1 end
|
14
|
+
end
|
15
|
+
|
16
|
+
@doc1 = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'})
|
17
|
+
@doc2 = @document.create({:first_name => 'Steve', :last_name => 'Smith', :age => '28'})
|
18
|
+
@doc3 = @document.create({:first_name => 'Steph', :last_name => 'Nunemaker', :age => '26'})
|
19
|
+
end
|
20
|
+
|
21
|
+
should "return the total pages" do
|
22
|
+
result = @document.paginate(:per_page => 2, :page => 1)
|
23
|
+
result.total_pages.should == 2
|
24
|
+
end
|
25
|
+
|
26
|
+
should "return the total pages when defaulting to the document class per_page" do
|
27
|
+
result = @document.paginate(:page => 1)
|
28
|
+
result.total_pages.should == 3
|
29
|
+
end
|
30
|
+
|
31
|
+
should "return the total of records" do
|
32
|
+
result = @document.paginate(:per_page => 2, :page => 1)
|
33
|
+
result.total_entries.should == 3
|
34
|
+
end
|
35
|
+
|
36
|
+
should "return the items" do
|
37
|
+
result = @document.paginate(:per_page => 2, :page => 1, :order => 'first_name')
|
38
|
+
result.size.should == 2
|
39
|
+
result.should == [@doc1, @doc3]
|
40
|
+
end
|
41
|
+
|
42
|
+
should "accept conditions" do
|
43
|
+
result = @document.paginate({
|
44
|
+
:last_name => 'Nunemaker',
|
45
|
+
:order => "age DESC",
|
46
|
+
:per_page => 2,
|
47
|
+
:page => 1,
|
48
|
+
})
|
49
|
+
result.should == [@doc1, @doc3]
|
50
|
+
result.first.age.should == 27
|
51
|
+
|
52
|
+
result = @document.paginate({
|
53
|
+
:conditions => {:last_name => 'Nunemaker'},
|
54
|
+
:order => "age DESC",
|
55
|
+
:per_page => 2,
|
56
|
+
:page => 1} )
|
57
|
+
result.should == [@doc1, @doc3]
|
58
|
+
result.first.age.should == 27
|
59
|
+
end
|
60
|
+
|
61
|
+
should "withstand rigor" do
|
62
|
+
result = @document.paginate({
|
63
|
+
:per_page => 1,
|
64
|
+
:page => 1,
|
65
|
+
:order => 'age desc',
|
66
|
+
:last_name => 'Nunemaker'
|
67
|
+
})
|
68
|
+
result.should == [@doc1]
|
69
|
+
result.total_entries.should == 2
|
70
|
+
result.total_pages.should == 2
|
71
|
+
|
72
|
+
result = @document.paginate({
|
73
|
+
:per_page => 1,
|
74
|
+
:page => 2,
|
75
|
+
:order => 'age desc',
|
76
|
+
:last_name => 'Nunemaker'
|
77
|
+
})
|
78
|
+
result.should == [@doc3]
|
79
|
+
result.total_entries.should == 2
|
80
|
+
result.total_pages.should == 2
|
81
|
+
|
82
|
+
result = @document.paginate({
|
83
|
+
:per_page => 2,
|
84
|
+
:page => 1,
|
85
|
+
:order => 'age desc',
|
86
|
+
:last_name => 'Nunemaker'
|
87
|
+
})
|
88
|
+
result.should == [@doc1, @doc3]
|
89
|
+
result.total_entries.should == 2
|
90
|
+
result.total_pages.should == 1
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
@@ -0,0 +1,161 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ProtectedTest < Test::Unit::TestCase
|
4
|
+
context 'A document with protected attributes' do
|
5
|
+
setup do
|
6
|
+
@doc_class = Doc do
|
7
|
+
key :name, String
|
8
|
+
key :admin, Boolean, :default => false
|
9
|
+
|
10
|
+
attr_protected :admin
|
11
|
+
end
|
12
|
+
|
13
|
+
@doc = @doc_class.create(:name => 'Steve Sloan')
|
14
|
+
end
|
15
|
+
|
16
|
+
should 'have protected attributes class method' do
|
17
|
+
@doc_class.protected_attributes.should == [:admin].to_set
|
18
|
+
end
|
19
|
+
|
20
|
+
should "default protected attributes to nil" do
|
21
|
+
Doc().protected_attributes.should be_nil
|
22
|
+
end
|
23
|
+
|
24
|
+
should "have protected attributes instance method" do
|
25
|
+
@doc.protected_attributes.should equal(@doc_class.protected_attributes)
|
26
|
+
end
|
27
|
+
|
28
|
+
should "work with :protected shortcut when defining key" do
|
29
|
+
Doc() do
|
30
|
+
key :user_id, ObjectId, :protected => true
|
31
|
+
end.protected_attributes.should == [:user_id].to_set
|
32
|
+
end
|
33
|
+
|
34
|
+
should 'assign protected attribute through accessor' do
|
35
|
+
@doc.admin = true
|
36
|
+
@doc.admin.should be_true
|
37
|
+
end
|
38
|
+
|
39
|
+
should "ignore protected attribute on #initialize" do
|
40
|
+
doc = @doc_class.new(:name => 'John', :admin => true)
|
41
|
+
doc.admin.should be_false
|
42
|
+
doc.name.should == 'John'
|
43
|
+
end
|
44
|
+
|
45
|
+
should "not ignore protected attributes on #initialize from the database" do
|
46
|
+
doc = @doc_class.new(:name => 'John')
|
47
|
+
doc.admin = true
|
48
|
+
doc.save!
|
49
|
+
|
50
|
+
doc = @doc_class.first(:name => 'John')
|
51
|
+
doc.admin.should be_true
|
52
|
+
doc.name.should == 'John'
|
53
|
+
end
|
54
|
+
|
55
|
+
should 'ignore protected attribute on #update_attributes' do
|
56
|
+
@doc.update_attributes(:name => 'Ren Hoek', :admin => true)
|
57
|
+
@doc.name.should == 'Ren Hoek'
|
58
|
+
@doc.admin.should be_false
|
59
|
+
end
|
60
|
+
|
61
|
+
should 'ignore protected attribute on #update_attributes!' do
|
62
|
+
@doc.update_attributes!(:name => 'Stimpson J. Cat', :admin => true)
|
63
|
+
@doc.name.should == 'Stimpson J. Cat'
|
64
|
+
@doc.admin.should be_false
|
65
|
+
end
|
66
|
+
|
67
|
+
should 'be indifferent to whether the protected keys are strings or symbols' do
|
68
|
+
@doc.update_attributes!("name" => 'Stimpson J. Cat', "admin" => true)
|
69
|
+
@doc.name.should == 'Stimpson J. Cat'
|
70
|
+
@doc.admin.should be_false
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
context "Single collection inherited protected attributes" do
|
75
|
+
setup do
|
76
|
+
class ::GrandParent
|
77
|
+
include MongoMapper::Document
|
78
|
+
|
79
|
+
key :_type, String
|
80
|
+
key :site_id, ObjectId
|
81
|
+
|
82
|
+
attr_protected :site_id
|
83
|
+
end
|
84
|
+
GrandParent.collection.remove
|
85
|
+
|
86
|
+
class ::Child < ::GrandParent
|
87
|
+
key :position, Integer
|
88
|
+
|
89
|
+
attr_protected :position
|
90
|
+
end
|
91
|
+
|
92
|
+
class ::GrandChild < ::Child; end
|
93
|
+
|
94
|
+
class ::OtherChild < ::GrandParent
|
95
|
+
key :blog_id, ObjectId
|
96
|
+
|
97
|
+
attr_protected :blog_id
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
teardown do
|
102
|
+
Object.send :remove_const, 'GrandParent' if defined?(::GrandParent)
|
103
|
+
Object.send :remove_const, 'Child' if defined?(::Child)
|
104
|
+
Object.send :remove_const, 'GrandChild' if defined?(::GrandChild)
|
105
|
+
Object.send :remove_const, 'OtherChild' if defined?(::OtherChild)
|
106
|
+
end
|
107
|
+
|
108
|
+
should "share keys down the inheritance trail" do
|
109
|
+
GrandParent.protected_attributes.should == [:site_id].to_set
|
110
|
+
Child.protected_attributes.should == [:site_id, :position].to_set
|
111
|
+
GrandChild.protected_attributes.should == [:site_id, :position].to_set
|
112
|
+
OtherChild.protected_attributes.should == [:site_id, :blog_id].to_set
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
context 'An embedded document with protected attributes' do
|
117
|
+
setup do
|
118
|
+
@doc_class = Doc('Project')
|
119
|
+
@edoc_class = EDoc('Person') do
|
120
|
+
key :name, String
|
121
|
+
key :admin, Boolean, :default => false
|
122
|
+
|
123
|
+
attr_protected :admin
|
124
|
+
end
|
125
|
+
@doc_class.many :people, :class => @edoc_class
|
126
|
+
|
127
|
+
@doc = @doc_class.create(:title => 'MongoMapper')
|
128
|
+
@edoc = @edoc_class.new(:name => 'Steve Sloan')
|
129
|
+
@doc.people << @edoc
|
130
|
+
end
|
131
|
+
|
132
|
+
should 'have protected attributes class method' do
|
133
|
+
@edoc_class.protected_attributes.should == [:admin].to_set
|
134
|
+
end
|
135
|
+
|
136
|
+
should "default protected attributes to nil" do
|
137
|
+
EDoc().protected_attributes.should be_nil
|
138
|
+
end
|
139
|
+
|
140
|
+
should "have protected attributes instance method" do
|
141
|
+
@edoc.protected_attributes.should equal(@edoc_class.protected_attributes)
|
142
|
+
end
|
143
|
+
|
144
|
+
should 'assign protected attribute through accessor' do
|
145
|
+
@edoc.admin = true
|
146
|
+
@edoc.admin.should be_true
|
147
|
+
end
|
148
|
+
|
149
|
+
should 'ignore protected attribute on #update_attributes' do
|
150
|
+
@edoc.update_attributes(:name => 'Ren Hoek', :admin => true)
|
151
|
+
@edoc.name.should == 'Ren Hoek'
|
152
|
+
@edoc.admin.should be_false
|
153
|
+
end
|
154
|
+
|
155
|
+
should 'ignore protected attribute on #update_attributes!' do
|
156
|
+
@edoc.update_attributes!(:name => 'Stimpson J. Cat', :admin => true)
|
157
|
+
@edoc.name.should == 'Stimpson J. Cat'
|
158
|
+
@edoc.admin.should be_false
|
159
|
+
end
|
160
|
+
end
|
161
|
+
end
|