Skip to content

Commit 5f58166

Browse files
author
Nigel Rausch
committed
Working on the archive option of docs
1 parent f8964f1 commit 5f58166

File tree

4 files changed

+35
-14
lines changed

4 files changed

+35
-14
lines changed

app/models/doc.rb

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,24 @@ class Doc < ActiveRecord::Base
1818
# * nil if none found
1919
#
2020
def self.retrieve id, options={}
21-
validate_options(options, [:rev])
22-
case id
23-
when Symbol
24-
if id == :all
25-
docs = self.find(id)
21+
with_scope(:find=>{:conditions=>{:archived_at=>nil}}) do
22+
validate_options(options, [:rev])
23+
case id
24+
when Symbol
25+
if id == :all
26+
docs = self.find(id)
27+
else
28+
docs = [self.find(id)]
29+
end
2630
else
27-
docs = [self.find(id)]
31+
docs = self.find_all_by_id(id)
2832
end
29-
else
30-
docs = self.find_all_by_id(id)
31-
end
32-
unless docs.empty?
33-
res = (id.is_a?(Array) or id == :all) ? docs.map {|doc| doc.retrieve} : docs.first.retrieve(options[:rev]||0)
34-
# a = docs.map {|doc| doc.retrieve} #: docs.first.retrieve(options[:rev]||0)
33+
unless docs.empty?
34+
res = (id.is_a?(Array) or id == :all) ? docs.map {|doc| doc.retrieve} : docs.first.retrieve(options[:rev]||0)
35+
# a = docs.map {|doc| doc.retrieve} #: docs.first.retrieve(options[:rev]||0)
36+
end
37+
return res
3538
end
36-
return res
3739
end
3840

3941
# Stores or updates a document into the database
@@ -130,6 +132,7 @@ def self.remove id, options=nil
130132

131133
def remove # :nodoc:
132134
stores.update_all({:rev=>rev}, {:rev=>0})
135+
update_attribute(:archived_at, Time.now)
133136
end
134137

135138
def retrieve(revision=0) # :nodoc:
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class AddArchivedAtToDoc < ActiveRecord::Migration
2+
def self.up
3+
add_column :docs, :archived_at, :datetime
4+
end
5+
6+
def self.down
7+
remove_column :docs, :archived_at
8+
end
9+
end

db/schema.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@
99
#
1010
# It's strongly recommended to check this file into your version control system.
1111

12-
ActiveRecord::Schema.define(:version => 20081115093653) do
12+
ActiveRecord::Schema.define(:version => 20081122110526) do
1313

1414
create_table "docs", :force => true do |t|
1515
t.integer "rev"
1616
t.datetime "created_at"
1717
t.datetime "updated_at"
18+
t.datetime "archived_at"
1819
end
1920

2021
create_table "fields", :force => true do |t|

test/unit/doc_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ def test_retrieve_when_id_does_not_exist
5252
assert_nil Doc.retrieve([id,id+1])
5353
end
5454

55+
def test_retrieve_when_id_is_archived
56+
id = Doc.last.id
57+
Doc.remove id
58+
59+
assert_nil Doc.retrieve( id)
60+
end
61+
5562

5663
# Update
5764
def test_update
@@ -236,6 +243,7 @@ def test_remove
236243
assert_equal [doc.id], ret
237244
doc.stores.each {|item| assert item.rev != 0}
238245
assert_equal store_1.rev, Store.find(store_1.id).rev
246+
assert(!doc.reload.archived_at.nil?)
239247
end
240248
end
241249

0 commit comments

Comments
 (0)