winton-acts_as_archive 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +63 -13
- data/acts_as_archive.gemspec +5 -5
- data/lib/acts_as_archive/base/destroy.rb +14 -15
- data/lib/acts_as_archive/base/restore.rb +1 -5
- data/lib/acts_as_archive/base/table.rb +13 -3
- data/lib/acts_as_archive/migration.rb +4 -0
- metadata +5 -5
data/README.markdown
CHANGED
@@ -1,23 +1,73 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
ActsAsArchive
|
2
|
+
=============
|
3
3
|
|
4
|
-
|
4
|
+
Don't delete your records, move them to a different table.
|
5
|
+
Like <code>acts\_as\_paranoid</code>, but doesn't change all your SQL queries.
|
5
6
|
|
6
|
-
|
7
|
-
|
7
|
+
Install
|
8
|
+
-------
|
9
|
+
|
10
|
+
script/plugin:
|
11
|
+
|
12
|
+
<pre>
|
13
|
+
script/plugin install git://github.com/winton/acts_as_archive.git
|
14
|
+
</pre>
|
15
|
+
|
16
|
+
rubygems:
|
8
17
|
|
9
18
|
<pre>
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
19
|
+
# terminal
|
20
|
+
sudo gem install winton-acts_as_archive
|
21
|
+
|
22
|
+
# environment.rb
|
23
|
+
config.gem "winton-acts_as_archive", :lib => "acts_as_archive", :source => "http://gems.github.com"
|
15
24
|
</pre>
|
16
25
|
|
17
|
-
|
26
|
+
Models
|
27
|
+
------
|
28
|
+
|
29
|
+
Add <code>acts\_as\_archive</code> to your models:
|
18
30
|
|
19
31
|
<pre>
|
20
|
-
|
32
|
+
class Article < ActiveRecord::Base
|
33
|
+
acts_as_archive
|
34
|
+
end
|
21
35
|
</pre>
|
22
36
|
|
23
|
-
|
37
|
+
That's it!
|
38
|
+
----------
|
39
|
+
|
40
|
+
Next time your Rails instance boots up, the plugin replicates your table's structure into
|
41
|
+
<code>archived\_articles</code> (as per the example), with an additional <code>deleted\_at</code> column.
|
42
|
+
|
43
|
+
Use <code>destroy</code>, <code>delete</code>, and <code>delete_all</code> like you normally would.
|
44
|
+
Records are copied into the archive table before being destroyed.
|
45
|
+
|
46
|
+
What if my schema changes?
|
47
|
+
--------------------------
|
48
|
+
|
49
|
+
Any new migrations on your <code>acts\_as\_archive</code> table are automatically applied to the archive table.
|
50
|
+
|
51
|
+
Query the archive
|
52
|
+
-----------------
|
53
|
+
|
54
|
+
Add <code>::Archive</code> to your ActiveRecord class:
|
55
|
+
|
56
|
+
<pre>
|
57
|
+
Article::Archive.find(:first)
|
58
|
+
</pre>
|
59
|
+
|
60
|
+
Restore from the archive
|
61
|
+
------------------------
|
62
|
+
|
63
|
+
Use <code>restore\_all</code> to copy archived records back to your table:
|
64
|
+
|
65
|
+
<pre>
|
66
|
+
Article.restore_all([ 'id = ?', 1 ])
|
67
|
+
</pre>
|
68
|
+
|
69
|
+
Auto-migrate from acts\_as\_paranoid
|
70
|
+
------------------------------------
|
71
|
+
|
72
|
+
If a <code>deleted\_at</code> column is present in your table, the plugin will attempt to move deleted
|
73
|
+
records to the archive table, preserving the <code>deleted\_at</code> value.
|
data/acts_as_archive.gemspec
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = 'acts_as_archive'
|
3
|
-
s.version = '0.1.
|
4
|
-
s.date = '2009-04-
|
3
|
+
s.version = '0.1.1'
|
4
|
+
s.date = '2009-04-23'
|
5
5
|
|
6
|
-
s.summary = "
|
7
|
-
s.description = "
|
6
|
+
s.summary = "Don't delete your records, move them to a different table"
|
7
|
+
s.description = "Don't delete your records, move them to a different table"
|
8
8
|
|
9
9
|
s.author = 'Winton Welsh'
|
10
10
|
s.email = 'mail@wintoni.us'
|
11
|
-
s.homepage = 'http://github.com/winton/'
|
11
|
+
s.homepage = 'http://github.com/winton/acts_as_archive'
|
12
12
|
|
13
13
|
# = MANIFEST =
|
14
14
|
s.files = %w[
|
@@ -16,31 +16,30 @@ module ActsAsArchive
|
|
16
16
|
end
|
17
17
|
|
18
18
|
module ClassMethods
|
19
|
-
def copy_to_archive(conditions)
|
19
|
+
def copy_to_archive(conditions, import=false)
|
20
20
|
add_conditions!(where = '', conditions)
|
21
|
-
|
21
|
+
insert_cols = column_names.clone
|
22
|
+
select_cols = column_names.clone
|
23
|
+
if insert_cols.include?('deleted_at')
|
24
|
+
unless import
|
25
|
+
select_cols[select_cols.index('deleted_at')] = "'#{Time.now.to_s(:db)}'"
|
26
|
+
end
|
27
|
+
else
|
28
|
+
insert_cols << 'deleted_at'
|
29
|
+
select_cols << "'#{Time.now.to_s(:db)}'"
|
30
|
+
end
|
22
31
|
connection.execute(%{
|
23
|
-
INSERT INTO archived_#{table_name} (#{
|
24
|
-
SELECT #{
|
32
|
+
INSERT INTO archived_#{table_name} (#{insert_cols.join(', ')})
|
33
|
+
SELECT #{select_cols.join(', ')}
|
25
34
|
FROM #{table_name}
|
26
35
|
#{where}
|
27
36
|
})
|
28
|
-
|
29
|
-
connection.execute("TRUNCATE TABLE #{table_name}")
|
30
|
-
else
|
31
|
-
connection.execute("DELETE FROM #{table_name} #{where}")
|
32
|
-
end
|
37
|
+
connection.execute("DELETE FROM #{table_name} #{where}")
|
33
38
|
end
|
34
39
|
|
35
40
|
def delete_all(conditions=nil)
|
36
41
|
copy_to_archive(conditions)
|
37
42
|
end
|
38
|
-
|
39
|
-
def migrate_from_acts_as_paranoid
|
40
|
-
if column_names.include?('deleted_at')
|
41
|
-
copy_to_archive('deleted_at IS NOT NULL')
|
42
|
-
end
|
43
|
-
end
|
44
43
|
end
|
45
44
|
|
46
45
|
module InstanceMethods
|
@@ -19,11 +19,7 @@ module ActsAsArchive
|
|
19
19
|
FROM archived_#{table_name}
|
20
20
|
#{where}
|
21
21
|
})
|
22
|
-
|
23
|
-
connection.execute("TRUNCATE TABLE archived_#{table_name}")
|
24
|
-
else
|
25
|
-
connection.execute("DELETE FROM archived_#{table_name} #{where}")
|
26
|
-
end
|
22
|
+
connection.execute("DELETE FROM archived_#{table_name} #{where}")
|
27
23
|
end
|
28
24
|
|
29
25
|
def restore_all(conditions=nil)
|
@@ -21,10 +21,20 @@ module ActsAsArchive
|
|
21
21
|
AS SELECT * from #{table_name}
|
22
22
|
WHERE false;
|
23
23
|
})
|
24
|
+
columns = connection.columns("archived_#{table_name}").collect(&:name)
|
25
|
+
unless columns.include?('deleted_at')
|
26
|
+
connection.add_column("archived_#{table_name}", :deleted_at, :datetime)
|
27
|
+
end
|
28
|
+
connection.add_index("archived_#{table_name}", :id)
|
29
|
+
connection.add_index("archived_#{table_name}", :deleted_at)
|
30
|
+
migrate_from_acts_as_paranoid
|
24
31
|
end
|
25
|
-
|
26
|
-
|
27
|
-
|
32
|
+
end
|
33
|
+
|
34
|
+
def migrate_from_acts_as_paranoid
|
35
|
+
if column_names.include?('deleted_at')
|
36
|
+
# Base::Destroy.copy_to_archive
|
37
|
+
copy_to_archive('deleted_at IS NOT NULL', true)
|
28
38
|
end
|
29
39
|
end
|
30
40
|
end
|
@@ -24,6 +24,10 @@ module ActsAsArchive
|
|
24
24
|
:drop_table, :remove_column, :remove_columns,
|
25
25
|
:remove_timestamps, :rename_column, :rename_table
|
26
26
|
]
|
27
|
+
if args.include?(:deleted_at) || args.include?('deleted_at')
|
28
|
+
# Don't change the archive's deleted_at column
|
29
|
+
return
|
30
|
+
end
|
27
31
|
if !args.empty? && supported.include?(method)
|
28
32
|
connection = ActiveRecord::Base.connection
|
29
33
|
args[0] = "archived_" + ActiveRecord::Migrator.proper_table_name(args[0])
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: winton-acts_as_archive
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Winton Welsh
|
@@ -9,11 +9,11 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-04-
|
12
|
+
date: 2009-04-23 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
16
|
-
description:
|
16
|
+
description: Don't delete your records, move them to a different table
|
17
17
|
email: mail@wintoni.us
|
18
18
|
executables: []
|
19
19
|
|
@@ -46,7 +46,7 @@ files:
|
|
46
46
|
- spec/spec.opts
|
47
47
|
- spec/spec_helper.rb
|
48
48
|
has_rdoc: false
|
49
|
-
homepage: http://github.com/winton/
|
49
|
+
homepage: http://github.com/winton/acts_as_archive
|
50
50
|
post_install_message:
|
51
51
|
rdoc_options: []
|
52
52
|
|
@@ -70,6 +70,6 @@ rubyforge_project:
|
|
70
70
|
rubygems_version: 1.2.0
|
71
71
|
signing_key:
|
72
72
|
specification_version: 2
|
73
|
-
summary:
|
73
|
+
summary: Don't delete your records, move them to a different table
|
74
74
|
test_files: []
|
75
75
|
|