active_metadata 0.0.2 → 0.1.9
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/app/controllers/active_metadata/attachments_controller.rb +47 -0
- data/app/controllers/active_metadata/histories_controller.rb +16 -0
- data/app/controllers/active_metadata/notes_controller.rb +61 -0
- data/app/controllers/active_metadata/watchers_controller.rb +34 -0
- data/app/helpers/application_helper.rb +2 -0
- data/app/views/active_metadata/attachments/index.html.erb +11 -0
- data/app/views/active_metadata/histories/index.html.erb +5 -0
- data/app/views/active_metadata/notes/_form.html.erb +23 -0
- data/app/views/active_metadata/notes/edit.html.erb +8 -0
- data/app/views/active_metadata/notes/index.html.erb +11 -0
- data/app/views/active_metadata/notes/index.js.erb +1 -0
- data/app/views/active_metadata/notes/show.html.erb +9 -0
- data/app/views/active_metadata/watchers/create.js.erb +1 -0
- data/app/views/active_metadata/watchers/destroy.js.erb +1 -0
- data/app/views/active_metadata/watchers/index.html.erb +5 -0
- data/config/active_metadata.yml +17 -0
- data/config/application.rb +0 -0
- data/config/database.yml +11 -0
- data/config/initializers/inflections.rb +7 -0
- data/config/mongoid.yml +28 -0
- data/config/routes.rb +30 -0
- data/db/development.sqlite3 +0 -0
- data/db/migrate/001_create_document.rb +12 -0
- data/db/migrate/002_add_surname_to_document.rb +9 -0
- data/db/migrate/003_create_section.rb +13 -0
- data/db/migrate/004_create_user.rb +14 -0
- data/db/migrate/005_create_inboxes.rb +19 -0
- data/db/migrate/006_create_alert_message.rb +37 -0
- data/db/migrate/007_create_notes.rb +22 -0
- data/db/migrate/008_create_history.rb +20 -0
- data/db/migrate/009_create_watcher.rb +21 -0
- data/db/migrate/010_create_attachment.rb +26 -0
- data/db/migrate/011_add_created_by_to_histories.rb +10 -0
- data/db/test.sqlite3 +0 -0
- data/features/step_definitions/add_a_user_to_a_watcher_steps.rb +29 -0
- data/features/step_definitions/trigger_alert_on_modify_steps.rb +86 -0
- data/features/supports/file.txt +1 -0
- data/features/supports/initializer.rb +1 -0
- data/features/supports/updated_file.txt +1 -0
- data/features/watchlist/add_a_user_to_a_watcher.feature +19 -0
- data/features/watchlist/trigger_alert_on_modify.feature +90 -0
- data/lib/active_metadata/base.rb +59 -0
- data/lib/active_metadata/persistence/active_record/attachment.rb +42 -0
- data/lib/active_metadata/persistence/active_record/history.rb +22 -0
- data/lib/active_metadata/persistence/active_record/note.rb +45 -0
- data/lib/active_metadata/persistence/active_record/watcher.rb +45 -0
- data/lib/active_metadata/persistence/active_record.rb +26 -0
- data/lib/active_metadata/persistence/mongoid/attachment.rb +45 -0
- data/lib/active_metadata/persistence/mongoid/history.rb +26 -0
- data/lib/active_metadata/persistence/mongoid/note.rb +48 -0
- data/lib/active_metadata/persistence/mongoid/watcher.rb +43 -0
- data/lib/active_metadata/persistence/mongoid.rb +30 -0
- data/lib/active_metadata/persistence/persistence.rb +13 -0
- data/lib/active_metadata/railtie.rb +6 -0
- data/lib/active_metadata/version.rb +1 -1
- data/lib/active_metadata.rb +6 -12
- data/lib/application_controller.rb +28 -0
- data/lib/application_helper.rb +7 -0
- data/lib/engine.rb +29 -0
- data/lib/model/active_record/attachment.rb +18 -0
- data/lib/model/active_record/history.rb +2 -0
- data/lib/model/active_record/note.rb +2 -0
- data/lib/model/active_record/watcher.rb +4 -0
- data/lib/model/mongoid/active_meta.rb +6 -0
- data/lib/model/mongoid/attachment.rb +25 -0
- data/lib/model/mongoid/history.rb +9 -0
- data/lib/model/mongoid/label.rb +14 -0
- data/lib/model/mongoid/note.rb +10 -0
- data/lib/model/mongoid/watcher.rb +24 -0
- data/lib/rails/railties/tasks.rake +8 -0
- data/lib/rake/active_record_tasks.rb +77 -0
- data/lib/rake/task.rb +18 -0
- data/lib/templates/active_metadata.yml +17 -0
- data/lib/templates/active_metadata_migrations +67 -0
- data/lib/templates/mongoid.yml +20 -0
- data/spec/active_metadata_spec.rb +596 -0
- data/spec/benchmark_spec.rb +35 -0
- data/spec/controllers/metadata_controller_spec.rb +14 -0
- data/spec/spec_helper.rb +87 -0
- data/spec/support/document.rb +8 -0
- data/spec/support/pdf_test.pdf +0 -0
- data/spec/support/pdf_test_2.pdf +0 -0
- data/spec/support/section.rb +8 -0
- data/spec/support/user.rb +13 -0
- data/spec/support/watcher_notifier.rb +21 -0
- metadata +201 -19
- data/.gitignore +0 -4
- data/Gemfile +0 -4
- data/Rakefile +0 -1
- data/active_metadata.gemspec +0 -24
@@ -0,0 +1,596 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "rack/test/uploaded_file"
|
3
|
+
require "time"
|
4
|
+
|
5
|
+
describe ActiveMetadata do
|
6
|
+
|
7
|
+
context "model methods" do
|
8
|
+
|
9
|
+
it "should exist a method acts_as_metadata in the model" do
|
10
|
+
Document.respond_to?(:acts_as_metadata).should be_true
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should find the metadata id if no metadata_id_from params has been provided" do
|
14
|
+
@document = Document.create! { |d| d.name = "John" }
|
15
|
+
@document.reload
|
16
|
+
@document.metadata_id.should eq @document.id
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should find the metadata id if a metadata_id_from params has been specified" do
|
20
|
+
@document = Document.create! { |d| d.name = "John" }
|
21
|
+
@document.reload
|
22
|
+
@section = @document.create_section :title => "new section"
|
23
|
+
@section.reload
|
24
|
+
@section.metadata_id.should eq @document.id
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
context "saving and quering notes" do
|
30
|
+
|
31
|
+
before(:each) do
|
32
|
+
@document = Document.create! { |d| d.name = "John" }
|
33
|
+
@document.reload
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should create a new note for a given field" do
|
37
|
+
@document.create_note_for(:name, "Very important note!")
|
38
|
+
@document.notes_for(:name).should have(1).record
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should verify the content of a note created" do
|
42
|
+
@document.create_note_for(:name, "Very important note!")
|
43
|
+
@document.notes_for(:name).last.note.should eq "Very important note!"
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should verify the created_by of a note created" do
|
47
|
+
@document.create_note_for(:name, "Very important note!")
|
48
|
+
@document.notes_for(:name).last.created_by.should eq User.current.id
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should verify that notes_for_name return only notes for the self Document" do
|
52
|
+
# fixtures
|
53
|
+
@another_doc = Document.create :name => "Andrea"
|
54
|
+
@another_doc.create_note_for(:name, "Very important note for doc2!")
|
55
|
+
@another_doc.reload
|
56
|
+
@document.create_note_for(:name, "Very important note!")
|
57
|
+
|
58
|
+
# expectations
|
59
|
+
@document.notes_for(:name).count.should eq(1)
|
60
|
+
@document.notes_for(:name).last.note.should eq "Very important note!"
|
61
|
+
@another_doc.notes_for(:name).last.note.should eq "Very important note for doc2!"
|
62
|
+
|
63
|
+
end
|
64
|
+
|
65
|
+
it "should update a note using update_note_for_name", :mongoid => true do
|
66
|
+
@document.create_note_for(:name, "Very important note!")
|
67
|
+
id = @document.notes_for(:name).last._id
|
68
|
+
@document.update_note(id, "New note value!")
|
69
|
+
@document.notes_for(:name).last.note.should eq "New note value!"
|
70
|
+
end
|
71
|
+
|
72
|
+
it "should update a note using update_note_for_name", :active_record => true do
|
73
|
+
@document.create_note_for(:name, "Very important note!")
|
74
|
+
id = @document.notes_for(:name).last.id
|
75
|
+
@document.update_note(id, "New note value!")
|
76
|
+
@document.notes_for(:name).last.note.should eq "New note value!"
|
77
|
+
end
|
78
|
+
|
79
|
+
it "should verify the content of a note created for a second attribute" do
|
80
|
+
@document.create_note_for(:name, "Very important note!")
|
81
|
+
@document.create_note_for(:surname, "Note on surname attribute!")
|
82
|
+
|
83
|
+
@document.notes_for(:name).last.note.should eq "Very important note!"
|
84
|
+
@document.notes_for(:surname).last.note.should eq "Note on surname attribute!"
|
85
|
+
end
|
86
|
+
|
87
|
+
it "should save multiple notes" do
|
88
|
+
notes = ["note number 1", "note number 2"]
|
89
|
+
@document.create_notes_for(:name, notes)
|
90
|
+
@document.notes_for(:name).should have(2).record
|
91
|
+
end
|
92
|
+
|
93
|
+
it "should save the updater id in metadata" do
|
94
|
+
@document.create_note_for(:name, "Very important note!")
|
95
|
+
id = @document.notes_for(:name).last.id
|
96
|
+
@document.update_note id, "new note value"
|
97
|
+
@document.notes_for(:name).last.updated_by.should eq User.current.id
|
98
|
+
end
|
99
|
+
|
100
|
+
it "should save the created_at datetime in metadata" do
|
101
|
+
@document.create_note_for(:name, "Very important note!")
|
102
|
+
@document.notes_for(:name).last.created_at.should be_a_kind_of Time
|
103
|
+
end
|
104
|
+
|
105
|
+
it "should save the updated_at datetime in metadata" do
|
106
|
+
@document.create_note_for(:name, "Very important note!")
|
107
|
+
@document.notes_for(:name).last.updated_at.should be_a_kind_of Time
|
108
|
+
end
|
109
|
+
|
110
|
+
it "should update the updated_at field when a note is updated", :mongoid => true do
|
111
|
+
@document.create_note_for(:name, "Very important note!")
|
112
|
+
id = @document.notes_for(:name).last._id
|
113
|
+
sleep 1.seconds
|
114
|
+
@document.update_note id, "new note value"
|
115
|
+
note = @document.notes_for(:name).last
|
116
|
+
note.updated_at.should > note.created_at
|
117
|
+
end
|
118
|
+
|
119
|
+
it "should update the updated_at field when a note is updated", :active_record => true do
|
120
|
+
@document.create_note_for(:name, "Very important note!")
|
121
|
+
id = @document.notes_for(:name).last.id
|
122
|
+
sleep 1.seconds
|
123
|
+
@document.update_note id, "new note value"
|
124
|
+
note = @document.notes_for(:name).last
|
125
|
+
note.updated_at.should > note.created_at
|
126
|
+
end
|
127
|
+
|
128
|
+
it "should verify that note are saved with the correct model id if metadata_id_from is defined" , :mongoid => true do
|
129
|
+
# fixtures
|
130
|
+
@document.create_note_for(:name, "Very important note!")
|
131
|
+
@section = @document.create_section :title => "new section"
|
132
|
+
@section.reload
|
133
|
+
@section.create_note_for(:title, "Very important note for section!")
|
134
|
+
|
135
|
+
# expectations
|
136
|
+
@document.notes_for(:name).last.label.active_meta.document_id.should eq @document.id
|
137
|
+
@section.notes_for(:title).last.label.active_meta.document_id.should eq @document.id
|
138
|
+
end
|
139
|
+
|
140
|
+
it "should verify that note are saved with the correct model id if metadata_id_from is defined" , :active_record => true do
|
141
|
+
# fixtures
|
142
|
+
@document.create_note_for(:name, "Very important note!")
|
143
|
+
@section = @document.create_section :title => "new section"
|
144
|
+
@section.reload
|
145
|
+
@section.create_note_for(:title, "Very important note for section!")
|
146
|
+
|
147
|
+
# expectations
|
148
|
+
@document.notes_for(:name).last.document_id.should eq @document.id
|
149
|
+
@section.notes_for(:title).last.document_id.should eq @document.id
|
150
|
+
end
|
151
|
+
|
152
|
+
it "should delete a note passing a bson object as id", :mongoid => true do
|
153
|
+
#fixtures
|
154
|
+
2.times do |i|
|
155
|
+
@document.create_note_for(:name, "Note number #{i}")
|
156
|
+
end
|
157
|
+
|
158
|
+
#expectations
|
159
|
+
notes = @document.notes_for(:name)
|
160
|
+
notes.count.should eq 2
|
161
|
+
note_to_be_deleted = notes[0].note
|
162
|
+
|
163
|
+
@document.delete_note_for(:name,notes[0]._id)
|
164
|
+
|
165
|
+
notes = @document.notes_for(:name)
|
166
|
+
notes.count.should eq 1
|
167
|
+
notes.first.note.should_not eq note_to_be_deleted
|
168
|
+
|
169
|
+
end
|
170
|
+
|
171
|
+
it "should delete a note passing a string as id", :mongoid => true do
|
172
|
+
#fixtures
|
173
|
+
2.times do |i|
|
174
|
+
@document.create_note_for(:name, "Note number #{i}")
|
175
|
+
end
|
176
|
+
|
177
|
+
#expectations
|
178
|
+
notes = @document.notes_for(:name)
|
179
|
+
notes.count.should eq 2
|
180
|
+
note_to_be_deleted = notes[0].note
|
181
|
+
|
182
|
+
@document.delete_note_for(:name,notes[0].id.to_s)
|
183
|
+
|
184
|
+
notes = @document.notes_for(:name)
|
185
|
+
notes.count.should eq 1
|
186
|
+
notes.first.note.should_not eq note_to_be_deleted
|
187
|
+
|
188
|
+
end
|
189
|
+
|
190
|
+
it "should delete a note id", :active_record => true do
|
191
|
+
#fixtures
|
192
|
+
2.times do |i|
|
193
|
+
@document.create_note_for(:name, "Note number #{i}")
|
194
|
+
end
|
195
|
+
|
196
|
+
#expectations
|
197
|
+
notes = @document.notes_for(:name)
|
198
|
+
notes.count.should eq 2
|
199
|
+
note_to_be_deleted = notes[0].note
|
200
|
+
|
201
|
+
@document.delete_note_for(:name,notes[0].id)
|
202
|
+
|
203
|
+
notes = @document.notes_for(:name)
|
204
|
+
notes.count.should eq 1
|
205
|
+
notes.first.note.should_not eq note_to_be_deleted
|
206
|
+
|
207
|
+
end
|
208
|
+
|
209
|
+
it "should verify that notes_for sort by updated_at descending" do
|
210
|
+
#fixtures
|
211
|
+
3.times do |i|
|
212
|
+
sleep 1.seconds
|
213
|
+
@document.create_note_for(:name, "Note number #{i}")
|
214
|
+
end
|
215
|
+
|
216
|
+
#expectations
|
217
|
+
@document.notes_for(:name).first.note.should eq "Note number 2"
|
218
|
+
@document.notes_for(:name).last.note.should eq "Note number 0"
|
219
|
+
end
|
220
|
+
|
221
|
+
it "should find a note by id", :mongoid => true, do
|
222
|
+
|
223
|
+
3.times do |i|
|
224
|
+
@document.create_note_for(:name, "Note number #{i}")
|
225
|
+
end
|
226
|
+
|
227
|
+
note = @document.notes_for(:name).last
|
228
|
+
id = note.id.to_s
|
229
|
+
|
230
|
+
match_note = @document.note_for :name,id
|
231
|
+
match_note.id.to_s.should eq id
|
232
|
+
|
233
|
+
end
|
234
|
+
|
235
|
+
it "should find a note by id", :active_record => true, do
|
236
|
+
|
237
|
+
3.times do |i|
|
238
|
+
@document.create_note_for(:name, "Note number #{i}")
|
239
|
+
end
|
240
|
+
|
241
|
+
note = @document.notes_for(:name).last
|
242
|
+
id = note.id
|
243
|
+
|
244
|
+
match_note = @document.note_for :name,id
|
245
|
+
match_note.id.should eq id
|
246
|
+
|
247
|
+
end
|
248
|
+
|
249
|
+
it "should has_notes_for verify if defined field has notes" do
|
250
|
+
@document.has_notes_for(:name).should be_false
|
251
|
+
@document.create_note_for(:name, "new note")
|
252
|
+
@document.has_notes_for(:name).should be_true
|
253
|
+
end
|
254
|
+
|
255
|
+
end
|
256
|
+
|
257
|
+
context "history" do
|
258
|
+
|
259
|
+
before(:each) do
|
260
|
+
@document = Document.create! { |d| d.name = "John" }
|
261
|
+
@document.reload
|
262
|
+
end
|
263
|
+
|
264
|
+
it "should create history when a document is created" do
|
265
|
+
@document.history_for(:name).should have(1).record
|
266
|
+
end
|
267
|
+
|
268
|
+
it "should create history for a defined field when a document is created" do
|
269
|
+
@document.history_for(:name)[0].value.should eq(@document.name)
|
270
|
+
end
|
271
|
+
|
272
|
+
it "should save the craeted_at datetime anytime an history entry is created" do
|
273
|
+
@document.history_for(:name)[0].created_at.should be_a_kind_of Time
|
274
|
+
end
|
275
|
+
|
276
|
+
it "should verify that history return records only for the self document" do
|
277
|
+
# fixtures
|
278
|
+
@another_doc = Document.create :name => "Andrea"
|
279
|
+
@another_doc.reload
|
280
|
+
|
281
|
+
# expectations
|
282
|
+
@document.history_for(:name).count.should eq(1)
|
283
|
+
@another_doc.history_for(:name).count.should eq(1)
|
284
|
+
|
285
|
+
@document.history_for(:name).last.value.should eq @document.name
|
286
|
+
@another_doc.history_for(:name).last.value.should eq @another_doc.name
|
287
|
+
end
|
288
|
+
|
289
|
+
it "should verify that history is saved with the correct model id if metadata_id_from is defined", :mongoid => true, do
|
290
|
+
# fixtures
|
291
|
+
@section = @document.create_section :title => "new section"
|
292
|
+
@section.reload
|
293
|
+
|
294
|
+
# expectations
|
295
|
+
@document.history_for(:name).count.should eq(1)
|
296
|
+
@document.history_for(:name).last.label.active_meta.document_id.should eq @document.id
|
297
|
+
|
298
|
+
@section.history_for(:title).count.should eq(1)
|
299
|
+
@section.history_for(:title).last.label.active_meta.document_id.should eq @document.id
|
300
|
+
end
|
301
|
+
|
302
|
+
it "should verify that history is saved with the correct model id if metadata_id_from is defined", :active_record => true, do
|
303
|
+
# fixtures
|
304
|
+
@section = @document.create_section :title => "new section"
|
305
|
+
@section.reload
|
306
|
+
|
307
|
+
# expectations
|
308
|
+
@document.history_for(:name).count.should eq(1)
|
309
|
+
@document.history_for(:name).last.document_id.should eq @document.id
|
310
|
+
|
311
|
+
@section.history_for(:title).count.should eq(1)
|
312
|
+
@section.history_for(:title).last.document_id.should eq @document.id
|
313
|
+
end
|
314
|
+
|
315
|
+
it "should verify that history_for sort by created_at descending" do
|
316
|
+
#fixtures
|
317
|
+
3.times do |i|
|
318
|
+
sleep 0.1.seconds
|
319
|
+
@document.name = "name #{i}"
|
320
|
+
@document.save
|
321
|
+
end
|
322
|
+
|
323
|
+
#expectations
|
324
|
+
@document.history_for(:name).first.value.should eq "name 2"
|
325
|
+
@document.history_for(:name).last.value.should eq "John"
|
326
|
+
end
|
327
|
+
|
328
|
+
it "should verify that no history is craeted for the skipped field defined in the config file" do
|
329
|
+
@document.history_for(:name).should have(1).record
|
330
|
+
@document.history_for(:id).should have(0).record
|
331
|
+
end
|
332
|
+
|
333
|
+
it "should save the correct creator when a history is created" do
|
334
|
+
current_user = User.current
|
335
|
+
history = @document.history_for(:name).first
|
336
|
+
history.created_by.should eq current_user.id
|
337
|
+
end
|
338
|
+
|
339
|
+
end
|
340
|
+
|
341
|
+
context "attachments" do
|
342
|
+
|
343
|
+
before(:each) do
|
344
|
+
@document = Document.create! { |d| d.name = "John" }
|
345
|
+
@document.reload
|
346
|
+
doc = File.expand_path('../support/pdf_test.pdf',__FILE__)
|
347
|
+
@attachment = Rack::Test::UploadedFile.new(doc, "application/pdf")
|
348
|
+
doc2 = File.expand_path('../support/pdf_test_2.pdf',__FILE__)
|
349
|
+
@attachment2 = Rack::Test::UploadedFile.new(doc2, "application/pdf")
|
350
|
+
end
|
351
|
+
|
352
|
+
it "should save attachment for a given attribute" do
|
353
|
+
@document.save_attachment_for(:name,@attachment)
|
354
|
+
@document.attachments_for(:name).should have(1).record
|
355
|
+
end
|
356
|
+
|
357
|
+
it "should verify that the attachment metadata id refers to the correct self id", :mongoid => true do
|
358
|
+
@document.save_attachment_for(:name,@attachment)
|
359
|
+
@document.attachments_for(:name).last.label.active_meta.document_id.should eq @document.id
|
360
|
+
end
|
361
|
+
|
362
|
+
it "should verify that the attachment metadata id refers to the correct self id", :active_record => true do
|
363
|
+
@document.save_attachment_for(:name,@attachment)
|
364
|
+
@document.attachments_for(:name).last.document_id.should eq @document.id
|
365
|
+
end
|
366
|
+
|
367
|
+
it "should verify that the attachment file name is correctly saved" do
|
368
|
+
@document.save_attachment_for(:name,@attachment)
|
369
|
+
@document.attachments_for(:name).last.attach.original_filename.should eq @attachment.original_filename
|
370
|
+
end
|
371
|
+
|
372
|
+
it "should verify that the attachment content type is correctly saved" do
|
373
|
+
@document.save_attachment_for(:name,@attachment)
|
374
|
+
@document.attachments_for(:name).last.attach.content_type.should eq @attachment.content_type
|
375
|
+
end
|
376
|
+
|
377
|
+
it "should verify that the attachment size is correctly saved" do
|
378
|
+
@document.save_attachment_for(:name,@attachment)
|
379
|
+
@document.attachments_for(:name).last.attach.size.should eq @attachment.size
|
380
|
+
end
|
381
|
+
|
382
|
+
it "should verify that the attachment updated_at is correctly saved", :mongoid => true do
|
383
|
+
@document.save_attachment_for(:name,@attachment)
|
384
|
+
@document.attachments_for(:name).last.attach.instance_read(:updated_at).should be_a_kind_of DateTime
|
385
|
+
end
|
386
|
+
|
387
|
+
it "should verify that the attachment updated_at is correctly saved", :active_record => true do
|
388
|
+
@document.save_attachment_for(:name,@attachment)
|
389
|
+
@document.attachments_for(:name).last.attach.instance_read(:updated_at).should be_a_kind_of Time
|
390
|
+
end
|
391
|
+
|
392
|
+
it "should verify that the document has been saved in the correct position on filesystem", :active_record => true do
|
393
|
+
@document.save_attachment_for(:name,@attachment)
|
394
|
+
att = @document.attachments_for(:name).first
|
395
|
+
expected_path = File.expand_path "#{ActiveMetadata::CONFIG['attachment_base_path']}/#{@document.id}/#{:name.to_s}/#{att.id}/#{@attachment.original_filename}"
|
396
|
+
File.exists?(expected_path).should be_true
|
397
|
+
end
|
398
|
+
|
399
|
+
it "should verify that the document has been saved in the correct position on filesystem", :mongoid => true do
|
400
|
+
@document.save_attachment_for(:name,@attachment)
|
401
|
+
att = @document.attachments_for(:name).first
|
402
|
+
expected_path = File.expand_path "#{ActiveMetadata::CONFIG['attachment_base_path']}/#{@document.id}/#{:name.to_s}/#{att.counter}/#{@attachment.original_filename}"
|
403
|
+
File.exists?(expected_path).should be_true
|
404
|
+
end
|
405
|
+
|
406
|
+
it "should delete an attachment passing a bson object as id", :mongoid => true do
|
407
|
+
|
408
|
+
#fixtures
|
409
|
+
2.times do |i|
|
410
|
+
@document.save_attachment_for(:name,@attachment)
|
411
|
+
end
|
412
|
+
|
413
|
+
#expectations
|
414
|
+
attachments = @document.attachments_for(:name)
|
415
|
+
attachments.count.should eq 2
|
416
|
+
attachment_path_to_be_deleted = attachments[0].attach.path
|
417
|
+
|
418
|
+
@document.delete_attachment_for(:name,attachments[0]._id)
|
419
|
+
|
420
|
+
attachments = @document.attachments_for(:name)
|
421
|
+
attachments.count.should eq 1
|
422
|
+
attachments.first.attach.path.should_not eq attachment_path_to_be_deleted
|
423
|
+
end
|
424
|
+
|
425
|
+
it "should delete an attachment passing a string as id", :mongoid => true do
|
426
|
+
#fixtures
|
427
|
+
2.times do |i|
|
428
|
+
@document.save_attachment_for(:name,@attachment)
|
429
|
+
end
|
430
|
+
|
431
|
+
#expectations
|
432
|
+
attachments = @document.attachments_for(:name)
|
433
|
+
attachments.count.should eq 2
|
434
|
+
attachment_path_to_be_deleted = attachments[0].attach.path
|
435
|
+
|
436
|
+
@document.delete_attachment_for(:name,attachments[0].id.to_s)
|
437
|
+
|
438
|
+
attachments = @document.attachments_for(:name)
|
439
|
+
attachments.count.should eq 1
|
440
|
+
attachments.first.attach.path.should_not eq attachment_path_to_be_deleted
|
441
|
+
end
|
442
|
+
|
443
|
+
it "should delete an attachment by id", :active_record => true do
|
444
|
+
#fixtures
|
445
|
+
2.times do |i|
|
446
|
+
@document.save_attachment_for(:name,@attachment)
|
447
|
+
end
|
448
|
+
|
449
|
+
#expectations
|
450
|
+
attachments = @document.attachments_for(:name)
|
451
|
+
attachments.count.should eq 2
|
452
|
+
attachment_path_to_be_deleted = attachments[0].attach.path
|
453
|
+
|
454
|
+
@document.delete_attachment_for(:name,attachments[0].id)
|
455
|
+
|
456
|
+
attachments = @document.attachments_for(:name)
|
457
|
+
attachments.count.should eq 1
|
458
|
+
attachments.first.attach.path.should_not eq attachment_path_to_be_deleted
|
459
|
+
end
|
460
|
+
|
461
|
+
it "should update an attachment", :mongoid => true do
|
462
|
+
@document.save_attachment_for(:name,@attachment)
|
463
|
+
att = @document.attachments_for(:name).last
|
464
|
+
|
465
|
+
@document.update_attachment_for :name,att._id,@attachment2
|
466
|
+
att2 = @document.attachments_for(:name).last
|
467
|
+
|
468
|
+
File.exists?(att.attach.path).should be_false
|
469
|
+
File.exists?(att2.attach.path).should be_true
|
470
|
+
end
|
471
|
+
|
472
|
+
it "should update an attachment", :active_record => true do
|
473
|
+
@document.save_attachment_for(:name,@attachment)
|
474
|
+
att = @document.attachments_for(:name).last
|
475
|
+
|
476
|
+
@document.update_attachment_for :name,att.id,@attachment2
|
477
|
+
att2 = @document.attachments_for(:name).last
|
478
|
+
|
479
|
+
File.exists?(att.attach.path).should be_false
|
480
|
+
File.exists?(att2.attach.path).should be_true
|
481
|
+
end
|
482
|
+
|
483
|
+
it "should verify that field attachment_updated_at is modified after an update", :mongoid => true do
|
484
|
+
@document.save_attachment_for(:name,@attachment)
|
485
|
+
att = @document.attachments_for(:name).last
|
486
|
+
|
487
|
+
sleep 1.seconds
|
488
|
+
|
489
|
+
@document.update_attachment_for :name,att._id,@attachment2
|
490
|
+
att2 = @document.attachments_for(:name).last
|
491
|
+
|
492
|
+
att2.attach.instance_read(:updated_at).should be > att.attach.instance_read(:updated_at)
|
493
|
+
end
|
494
|
+
|
495
|
+
it "should verify that field attachment_updated_at is modified after an update", :active_record => true do
|
496
|
+
@document.save_attachment_for(:name,@attachment)
|
497
|
+
att = @document.attachments_for(:name).last
|
498
|
+
|
499
|
+
sleep 1.seconds
|
500
|
+
|
501
|
+
@document.update_attachment_for :name,att.id,@attachment2
|
502
|
+
att2 = @document.attachments_for(:name).last
|
503
|
+
|
504
|
+
att2.attach.instance_read(:updated_at).should be > att.attach.instance_read(:updated_at)
|
505
|
+
end
|
506
|
+
|
507
|
+
it "should verify that is possible to upload 2 files with the same name for the same field" do
|
508
|
+
2.times do
|
509
|
+
@document.save_attachment_for(:name,@attachment)
|
510
|
+
end
|
511
|
+
|
512
|
+
#expectations
|
513
|
+
attachments = @document.attachments_for :name
|
514
|
+
attachments.count.should eq 2
|
515
|
+
File.exists?(attachments[0].attach.path).should be_true
|
516
|
+
attachments[0].attach.instance_read(:file_name).should eq "pdf_test.pdf"
|
517
|
+
File.exists?(attachments[1].attach.path).should be_true
|
518
|
+
attachments[1].attach.instance_read(:file_name).should eq "pdf_test.pdf"
|
519
|
+
end
|
520
|
+
|
521
|
+
it "should save the correct creator when an attachment is created" do
|
522
|
+
@document.save_attachment_for(:name,@attachment)
|
523
|
+
@document.attachments_for(:name).last.created_by.should eq User.current.id
|
524
|
+
end
|
525
|
+
|
526
|
+
it "should save the correct updater when anttachment is updated" do
|
527
|
+
@document.save_attachment_for(:name,@attachment)
|
528
|
+
att = @document.attachments_for(:name).last
|
529
|
+
|
530
|
+
@document.update_attachment_for :name,att.id,@attachment2
|
531
|
+
att2 = @document.attachments_for(:name).last
|
532
|
+
|
533
|
+
@document.attachments_for(:name).last.updated_by.should eq User.current.id
|
534
|
+
end
|
535
|
+
|
536
|
+
it "should has_notes_for verify if defined field has attachments" do
|
537
|
+
@document.has_attachments_for(:name).should be_false
|
538
|
+
@document.save_attachment_for(:name,@attachment)
|
539
|
+
@document.has_attachments_for(:name).should be_true
|
540
|
+
end
|
541
|
+
|
542
|
+
end
|
543
|
+
|
544
|
+
context "watchers" do
|
545
|
+
before(:each) do
|
546
|
+
@document = Document.create! { |d| d.name = "John" }
|
547
|
+
end
|
548
|
+
|
549
|
+
it "should create a watcher for a given field" do
|
550
|
+
user = User.create!(:email => "email@email.it", :firstname => 'John', :lastname => 'smith' )
|
551
|
+
@document.create_watcher_for(:name, user)
|
552
|
+
@document.watchers_for(:name).should have(1).record
|
553
|
+
end
|
554
|
+
|
555
|
+
it "should delete a watcher for a given field" do
|
556
|
+
user = User.create!(:email => "email@email.it", :firstname => 'John', :lastname => 'smith' )
|
557
|
+
@document.create_watcher_for(:name, user)
|
558
|
+
@document.watchers_for(:name).should have(1).record
|
559
|
+
|
560
|
+
@document.delete_watcher_for :name, user
|
561
|
+
@document.watchers_for(:name).should have(0).record
|
562
|
+
end
|
563
|
+
|
564
|
+
it "should return false if field is not watched by the passed user" do
|
565
|
+
user = User.create!(:email => "email@email.it", :firstname => 'John', :lastname => 'smith' )
|
566
|
+
another_user = User.create!(:email => "email2@email.it", :firstname => 'George', :lastname => 'Washington' )
|
567
|
+
|
568
|
+
@document.create_watcher_for(:name, user)
|
569
|
+
@document.is_watched_by(:name,user).should be_true
|
570
|
+
@document.is_watched_by(:name,another_user).should be_false
|
571
|
+
end
|
572
|
+
|
573
|
+
|
574
|
+
it "should create an unread message by default" do
|
575
|
+
pending "to be moved in virgilio project"
|
576
|
+
user = User.create!(:email => "email@email.it", :firstname => 'John', :lastname => 'smith' )
|
577
|
+
@document.create_watcher_for(:name, user)
|
578
|
+
@document.update_attribute(:name, 'new_value')
|
579
|
+
|
580
|
+
|
581
|
+
user.inbox.messages.should have(1).record
|
582
|
+
user.inbox.messages.first.read.should be_false
|
583
|
+
end
|
584
|
+
|
585
|
+
it "should read an unread message" do
|
586
|
+
pending "to be moved in virgilio project"
|
587
|
+
user = User.create!(:email => "email@email.it", :firstname => 'John', :lastname => 'smith' )
|
588
|
+
@document.create_watcher_for(:name, user)
|
589
|
+
@document.update_attribute(:name, 'new_value')
|
590
|
+
alert_message = user.inbox.messages.first
|
591
|
+
alert_message.mark_as_read
|
592
|
+
user.inbox.messages.first.read.should be_true
|
593
|
+
end
|
594
|
+
|
595
|
+
end
|
596
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "rack/test/uploaded_file"
|
3
|
+
require "benchmark"
|
4
|
+
|
5
|
+
describe "ActiveMetadata" do
|
6
|
+
|
7
|
+
before(:each) do
|
8
|
+
@document = Document.create! { |d| d.name = "John" }
|
9
|
+
@document.reload
|
10
|
+
end
|
11
|
+
|
12
|
+
context "running on mongo" do
|
13
|
+
|
14
|
+
# it "should insert xxx notes" do
|
15
|
+
# notes = 1000000
|
16
|
+
# result = []
|
17
|
+
# Benchmark.bm do |x|
|
18
|
+
# x.report do
|
19
|
+
# notes.times do |i|
|
20
|
+
# start = Time.now
|
21
|
+
# @document.create_note_for(:name, "Note number #{i}")
|
22
|
+
# result.push Time.now - start
|
23
|
+
# end
|
24
|
+
# end
|
25
|
+
# end
|
26
|
+
# puts "Total = #{result.sum}"
|
27
|
+
# puts "Average = #{result.sum / notes}"
|
28
|
+
# @document.notes_for(:name).count.should eq notes
|
29
|
+
# end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
|