Skip to content
Newer
Older
100644 126 lines (93 sloc) 4.91 KB
1ff227e @ajh Tweak company name.
ajh authored May 15, 2008
1 Copyright (C) 2008 Substantial and Andy Hartford <hartforda @ gmail.com>
4a63856 @ajh Add GPL license and copyright notice.
ajh authored May 15, 2008
2
2171bff @ajh Improve rdoc support.
ajh authored May 19, 2008
3 == acts_as_soft_deletable
cfb5d35 add README
(no author) authored Mar 4, 2008
4
5 This plugin provides the ability to soft delete ActiveRecord models. When
6 models are destroyed, they will be archived into a special deleted table.
7 They can later be restored easily.
8
2171bff @ajh Improve rdoc support.
ajh authored May 19, 2008
9 class Artist < ActiveRecord::Base
10 acts_as_soft_deletable # This will wrap the destroy method to provide soft delete
11 # support and create a new ActiveRecord class called Artist::Deleted
12 end
cfb5d35 add README
(no author) authored Mar 4, 2008
13
2171bff @ajh Improve rdoc support.
ajh authored May 19, 2008
14 model = Artist.find(34)
15 model.destroy # removes row from artists table, and adds a row to
16 # deleted_artists table
cfb5d35 add README
(no author) authored Mar 4, 2008
17
88141ca @ajh Add finders on live class which will also return deleted records.
ajh authored May 29, 2008
18
19 # The deleted record can be retrieved several ways
20 deleted = Artist::Deleted.find(34) # using the deleted class
21 deleted = Artist.find_with_deleted(34) # using the live class
22 deleted = Artist.find_with_deleted_by_id(34) # dynamic finds work too
23
2171bff @ajh Improve rdoc support.
ajh authored May 19, 2008
24 deleted.undestroy! # adds the row back to the artists table, and removes
25 # if from the deleted_artists table
cfb5d35 add README
(no author) authored Mar 4, 2008
26
2171bff @ajh Improve rdoc support.
ajh authored May 19, 2008
27 restored = Artist.find(34) # The artist is restored with all the same
28 # information. The updated_at column will be
29 # Time.now if it exists.
cfb5d35 add README
(no author) authored Mar 4, 2008
30
2171bff @ajh Improve rdoc support.
ajh authored May 19, 2008
31 == Compare to acts_as_paranoid
7e5bf93 add migration tests. Add update_column methods for deleted table in m…
(no author) authored Mar 13, 2008
32
f521949 @ajh Add a warning when columns are deleted with update_columns.
ajh authored May 20, 2008
33 Acts_as_paranoid takes the approach of using a deleted_at flag in the models
34 table. If the deleted_at column has a value, the row is considered 'deleted'.
35 The problem with this approach is that all finds on the model have to exclude
36 'deleted' rows. This turns out be be a challenge. Acts_as_paranoid patches the
37 ActiveRecord internals to accomplish this, but it is fragile and could break
38 with future changes to ActiveRecord. Also, some of the more exotic finds
39 currently don't work (has_many :through with polymorphism as of March 2008) and
40 supporting them means running on an upgrade treadmill to keep up with the
41 evolution of ActiveRecord.
42
43 This plugin avoids these problems by allowing the row to be deleted and
44 archiving it into another table. The behavior of ActiveRecord::Base#Find
45 doesn't have to change which should mean that this plugin is more immune to
46 breaking due to ActiveRecord development. Queries on the live table will also
47 be faster in the case of lots of deleted rows, because they will be in a
48 separate table.
49
eb7a273 @ajh add paragraph about active_record association support.
ajh authored Oct 20, 2008
50 The biggest tradeoff is that deleted rows aren't visible through active record
51 associations because they're in a different table. The only solution I could
52 think of was to write proxies for the associations. But it would involve so
53 much code that I figured it wasn't worth it.
54
55 Another tradeoff is that the deleted table needs to be maintained along with the
f521949 @ajh Add a warning when columns are deleted with update_columns.
ajh authored May 20, 2008
56 live table. For example, if the artists table adds a column in a future
57 migration, then the deleted_artists table needs that column as well.
58
59 A migration helper is available (see below) that will help keep the deleted
60 table in sync. Also, a unit test helper is provided (again, see below) which
61 adds unit tests to the model ensuring soft delete is working. If this is used,
62 the test will fail if the deleted table gets out of sync.
7e5bf93 add migration tests. Add update_column methods for deleted table in m…
(no author) authored Mar 13, 2008
63
2171bff @ajh Improve rdoc support.
ajh authored May 19, 2008
64 == Setup
cfb5d35 add README
(no author) authored Mar 4, 2008
65
2171bff @ajh Improve rdoc support.
ajh authored May 19, 2008
66 === Model
a3a974e add test helper and use them internally. Update Readme again.
(no author) authored Mar 13, 2008
67
cfb5d35 add README
(no author) authored Mar 4, 2008
68 Any ActiveRecord class that wants the soft delete functionality should add
69 the following line to their class definition:
70
2171bff @ajh Improve rdoc support.
ajh authored May 19, 2008
71 class SomeModel < ActiveRecord::Base
72 acts_as_soft_deletable
73 ...
cfb5d35 add README
(no author) authored Mar 4, 2008
74
2171bff @ajh Improve rdoc support.
ajh authored May 19, 2008
75 === Migration
a3a974e add test helper and use them internally. Update Readme again.
(no author) authored Mar 13, 2008
76
cfb5d35 add README
(no author) authored Mar 4, 2008
77 and setup the deleted table with the following migration:
78
2171bff @ajh Improve rdoc support.
ajh authored May 19, 2008
79 class AddActsAsSoftDeletable < ActiveRecord::Migration
80 def self.up
81 SomeModel::Deleted.create_table
82 end
cfb5d35 add README
(no author) authored Mar 4, 2008
83
2171bff @ajh Improve rdoc support.
ajh authored May 19, 2008
84 def self.down
85 SomeModel::Deleted.drop_table
86 end
87 end
cfb5d35 add README
(no author) authored Mar 4, 2008
88
f521949 @ajh Add a warning when columns are deleted with update_columns.
ajh authored May 20, 2008
89 Any changes to the original table (such as adding a column) should be reflected
90 in the deleted table. Use the update_columns method:
a3a974e add test helper and use them internally. Update Readme again.
(no author) authored Mar 13, 2008
91
2171bff @ajh Improve rdoc support.
ajh authored May 19, 2008
92 class AddSkuColumn < ActiveRecord::Migration
93 def self.up
94 add_column 'items', 'sku', :string
95 Item::Deleted.update_columns # will add sku column
96 end
a3a974e add test helper and use them internally. Update Readme again.
(no author) authored Mar 13, 2008
97
2171bff @ajh Improve rdoc support.
ajh authored May 19, 2008
98 def self.down
99 remove_column 'items', 'sku'
100 Item::Deleted.update_columns # will remove sku column
101 end
102 end
a3a974e add test helper and use them internally. Update Readme again.
(no author) authored Mar 13, 2008
103
f521949 @ajh Add a warning when columns are deleted with update_columns.
ajh authored May 20, 2008
104 Note that update_columns will happily delete columns if asked. In this case a
105 warning will be issued when the migration is run alerting the developer to the
106 situation. This warning can be disabled, see:
107 ActiveRecord::Acts::SoftDeletable#remove_column_warning_enabled=
a05b4ec @ajh rewrite some of README
ajh authored May 15, 2008
108
2171bff @ajh Improve rdoc support.
ajh authored May 19, 2008
109 === Unit tests
a3a974e add test helper and use them internally. Update Readme again.
(no author) authored Mar 13, 2008
110
a05b4ec @ajh rewrite some of README
ajh authored May 15, 2008
111 A model's soft delete capabilities can be easily unit tested by using this provided assert:
a3a974e add test helper and use them internally. Update Readme again.
(no author) authored Mar 13, 2008
112
2171bff @ajh Improve rdoc support.
ajh authored May 19, 2008
113 def test_soft_delete_works
114 # will run the model through a destroy and undestroy while making sure all values were saved
115 assert_model_soft_deletes( items(:radar_detector) )
116 end
a3a974e add test helper and use them internally. Update Readme again.
(no author) authored Mar 13, 2008
117
a05b4ec @ajh rewrite some of README
ajh authored May 15, 2008
118 This was developed with Test::Unit in mind. Not sure how well it works with rspec.
119
2171bff @ajh Improve rdoc support.
ajh authored May 19, 2008
120 === Thanks
a05b4ec @ajh rewrite some of README
ajh authored May 15, 2008
121
2171bff @ajh Improve rdoc support.
ajh authored May 19, 2008
122 * Substantial, my employer for letting me release this
123 * acts_as_paranoid and technoweenie, for a plugin I've got good years of use out of
124 * acts_as_versioned, who's approach influenced this plugin
125 * Danimal, for the feedback on rubyonrails-talk
Something went wrong with that request. Please try again.