never_wastes 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +22 -0
- data/README.md +11 -2
- data/lib/never_wastes.rb +11 -2
- data/lib/never_wastes/version.rb +1 -1
- metadata +3 -2
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2012 Yasuko Ohba
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
CHANGED
@@ -18,7 +18,16 @@ First, add deleted column in your models.
|
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
21
|
-
Currently
|
21
|
+
Currently the boolean "deleted" column is required.
|
22
|
+
|
23
|
+
If you need a timestamp, you can also add deleted_at column.
|
24
|
+
|
25
|
+
class AddDeletedToYourModels < ActiveRecord::Migration
|
26
|
+
def change
|
27
|
+
add_column :your_models, :deleted, :boolean, :null => false, :default => false
|
28
|
+
add_column :your_models, :deleted_at, :datetime
|
29
|
+
end
|
30
|
+
end
|
22
31
|
|
23
32
|
Next step is to specify never_wastes in your model which needs soft delete.
|
24
33
|
|
@@ -30,7 +39,7 @@ Then you can use destroy for soft delete.
|
|
30
39
|
|
31
40
|
model.destroy
|
32
41
|
|
33
|
-
If you want
|
42
|
+
If you want hard delete, use destroy!.
|
34
43
|
|
35
44
|
model.destroy!
|
36
45
|
|
data/lib/never_wastes.rb
CHANGED
@@ -22,14 +22,23 @@ module NeverWastes
|
|
22
22
|
module SoftDestroy
|
23
23
|
def self.included(base)
|
24
24
|
base.class_eval do
|
25
|
+
@never_wastes_boolean_column_name = :deleted
|
26
|
+
@never_wastes_datetime_column_name = :deleted_at
|
27
|
+
def self.soft_destroy_stamps
|
28
|
+
stamps = {@never_wastes_boolean_column_name => true}
|
29
|
+
stamps[@never_wastes_datetime_column_name] = Time.now if column_names.include?(@never_wastes_datetime_column_name.to_s)
|
30
|
+
stamps
|
31
|
+
end
|
32
|
+
|
25
33
|
alias_method :destroy!, :destroy
|
26
34
|
|
27
35
|
def destroy_softly
|
28
36
|
@destroying_softly = true
|
29
37
|
ret = with_transaction_returning_status do
|
30
38
|
run_callbacks :destroy do
|
31
|
-
self.class.
|
32
|
-
self.
|
39
|
+
stamps = self.class.soft_destroy_stamps
|
40
|
+
self.class.where(self.class.primary_key.to_sym => id).update_all(stamps)
|
41
|
+
stamps.each {|key, value| send("#{key}=", value)}
|
33
42
|
@destroyed = true
|
34
43
|
freeze
|
35
44
|
end
|
data/lib/never_wastes/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: never_wastes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-03-
|
12
|
+
date: 2012-03-08 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: It changes ActiveRecord::Base#destroy to support soft delete. Kind of
|
15
15
|
simple acts_as_paranoid.
|
@@ -21,6 +21,7 @@ extra_rdoc_files: []
|
|
21
21
|
files:
|
22
22
|
- .gitignore
|
23
23
|
- Gemfile
|
24
|
+
- LICENSE
|
24
25
|
- README.md
|
25
26
|
- Rakefile
|
26
27
|
- lib/never_wastes.rb
|