delete_softly 0.0.1 → 0.0.2
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/Manifest +1 -0
- data/README +3 -2
- data/Rakefile +1 -1
- data/delete_softly.gemspec +3 -3
- data/lib/class_methods.rb +63 -4
- data/lib/delete_softly.rb +13 -1
- data/lib/instance_methods.rb +13 -0
- metadata +5 -5
data/Manifest
CHANGED
data/README
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
DeleteSoftly
|
2
2
|
============
|
3
3
|
|
4
|
-
Add soft delete functionality to ActiveRecord models
|
4
|
+
Add soft delete functionality to ActiveRecord models. Important information:
|
5
|
+
This is Rails3 only, no backwards compatibility.
|
5
6
|
|
6
7
|
Example
|
7
8
|
=======
|
@@ -19,7 +20,7 @@ Now the following stuff works:
|
|
19
20
|
Post.count #=> 2
|
20
21
|
p2.destroy
|
21
22
|
Post.count #=> 1
|
22
|
-
Post.
|
23
|
+
Post.at_time(1.year.ago).count #=> 0
|
23
24
|
|
24
25
|
c1 = Comment.create
|
25
26
|
c2 = Comment.create
|
data/Rakefile
CHANGED
@@ -15,7 +15,7 @@ Rake::TestTask.new(:test) do |t|
|
|
15
15
|
end
|
16
16
|
|
17
17
|
desc 'Echoe'
|
18
|
-
Echoe.new('delete_softly', '0.0.
|
18
|
+
Echoe.new('delete_softly', '0.0.2') do |p|
|
19
19
|
p.description = "Add soft delete functionality to your ActiveRecord models"
|
20
20
|
p.url = "http://github.com/bterkuile/delete_softly"
|
21
21
|
p.author = "Benjamin ter Kuile"
|
data/delete_softly.gemspec
CHANGED
@@ -2,15 +2,15 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{delete_softly}
|
5
|
-
s.version = "0.0.
|
5
|
+
s.version = "0.0.2"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["Benjamin ter Kuile"]
|
9
|
-
s.date = %q{2010-
|
9
|
+
s.date = %q{2010-11-05}
|
10
10
|
s.description = %q{Add soft delete functionality to your ActiveRecord models}
|
11
11
|
s.email = %q{bterkuile@gmail.com}
|
12
12
|
s.extra_rdoc_files = ["README", "lib/class_methods.rb", "lib/delete_softly.rb", "lib/instance_methods.rb"]
|
13
|
-
s.files = ["MIT-LICENSE", "README", "Rakefile", "init.rb", "install.rb", "lib/class_methods.rb", "lib/delete_softly.rb", "lib/instance_methods.rb", "test/delete_softly_test.rb", "test/test_helper.rb", "uninstall.rb", "Manifest"
|
13
|
+
s.files = ["MIT-LICENSE", "README", "Rakefile", "delete_softly.gemspec", "init.rb", "install.rb", "lib/class_methods.rb", "lib/delete_softly.rb", "lib/instance_methods.rb", "test/delete_softly_test.rb", "test/test_helper.rb", "uninstall.rb", "Manifest"]
|
14
14
|
s.homepage = %q{http://github.com/bterkuile/delete_softly}
|
15
15
|
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Delete_softly", "--main", "README"]
|
16
16
|
s.require_paths = ["lib"]
|
data/lib/class_methods.rb
CHANGED
@@ -1,17 +1,76 @@
|
|
1
1
|
module DeleteSoftly
|
2
2
|
module ClassMethods
|
3
|
+
|
4
|
+
# Give the representation of items at a certain date/time.
|
5
|
+
# class Item
|
6
|
+
# delete_softly
|
7
|
+
# end
|
8
|
+
# Will result in:
|
9
|
+
# Item.at_time(DateTime.parse('2010-01-01')) #=> (SELECT "items".* FROM "items" WHERE (((("items"."deleted_at" > '2010-01-01 00:00:00') OR ("items"."deleted_at" IS NULL)) AND ("items"."created_at" < '2010-01-01 00:00:00')))
|
3
10
|
def at_date(date = Time.now.utc)
|
4
|
-
with_deleted
|
11
|
+
with_deleted do
|
12
|
+
where(({:deleted_at.gt => date} | {:deleted_at => nil}) & {:created_at.lt => date})
|
13
|
+
end
|
5
14
|
end
|
6
|
-
alias_method :at, :at_date
|
7
15
|
alias_method :at_time, :at_date
|
8
16
|
|
17
|
+
# Give the currently active items. When delete_soflty is added this is invoked by default
|
18
|
+
# But when false is added, items are shown by default
|
19
|
+
# class Item
|
20
|
+
# delete_softly false
|
21
|
+
# end
|
22
|
+
# Or similar:
|
23
|
+
# class Item
|
24
|
+
# delete_softly :default => false
|
25
|
+
# end
|
26
|
+
# You need to call active on the model where you want to hide deleted items
|
27
|
+
# Item.all #=> SELECT "items".* FROM "items"
|
28
|
+
# Item.active #=> SELECT "items".* FROM "items" WHERE ("items"."deleted_at" IS NULL)
|
9
29
|
def active
|
10
30
|
where(:deleted_at => nil)
|
11
31
|
end
|
12
32
|
|
13
|
-
|
14
|
-
|
33
|
+
# Include deleted items when performing queries
|
34
|
+
# class Item
|
35
|
+
# default_scope order(:content)
|
36
|
+
# delete_softly
|
37
|
+
# end
|
38
|
+
# Will result in:
|
39
|
+
# Item.first #=> SELECT "items".* FROM "items" WHERE ("items"."deleted_at" IS NULL) ORDER BY "items"."content" LIMIT 1
|
40
|
+
# Item.with_deleted.first #=> SELECT "items".* FROM "items" ORDER BY "items"."content" LIMIT 1
|
41
|
+
# Item.where(:content.matches => 'a%') #=> SELECT "items".* FROM "items" WHERE ("items"."deleted_at" IS NULL) AND ("items"."content" ILIKE 'a%') ORDER BY "items"."content"
|
42
|
+
# Item.with_deleted do
|
43
|
+
# Item.where(:content.matches => 'a%') #=> SELECT "items".* FROM "items" WHERE ("items"."content" ILIKE 'a%') ORDER BY "items"."content"
|
44
|
+
# end
|
45
|
+
# IHaveManyItems.items #=> SELECT "items".* FROM "items" WHERE ("items"."deleted_at" IS NULL) AND ("items".i_have_many_items_id = 1) ORDER BY "items"."content"
|
46
|
+
def with_deleted(&block)
|
47
|
+
if scoped_methods.any? # There are scoped methods in place
|
48
|
+
|
49
|
+
# remove deleted at condition if present
|
50
|
+
del = scoped_methods.last.where_values.delete(:deleted_at => nil)
|
51
|
+
|
52
|
+
# Execute block with deleted or just run scoped
|
53
|
+
r = block_given? ? yield : scoped
|
54
|
+
|
55
|
+
# Add deleted condition if it was present
|
56
|
+
scoped_methods.last.where_values << del if del
|
57
|
+
|
58
|
+
# Return de relation generated without deleted_at => nil
|
59
|
+
r
|
60
|
+
else
|
61
|
+
# Do not do anything special when there are no scoped_methods
|
62
|
+
r = block_given? ? yield : scoped
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
# Support for paper_trail if it is installed as well
|
67
|
+
def version_at(time)
|
68
|
+
if respond_to?(:at_time)
|
69
|
+
at_time(time).map{|i| i.respond_to?(:version_at) ? i.version_at(time) : i}.compact
|
70
|
+
else
|
71
|
+
scoped.map{|i| i.respond_to?(:version_at) ? i.version_at(time) : i}.compact
|
72
|
+
end
|
15
73
|
end
|
16
74
|
end
|
75
|
+
|
17
76
|
end
|
data/lib/delete_softly.rb
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
# DeleteSoftly
|
2
|
+
require 'active_record'
|
3
|
+
require 'meta_where'
|
2
4
|
require 'class_methods'
|
3
5
|
require 'instance_methods'
|
4
|
-
require 'meta_where'
|
5
6
|
module DeleteSoftly
|
6
7
|
module ARExtender
|
7
8
|
|
@@ -36,3 +37,14 @@ module DeleteSoftly
|
|
36
37
|
end
|
37
38
|
|
38
39
|
ActiveRecord::Base.send(:extend, DeleteSoftly::ARExtender)
|
40
|
+
|
41
|
+
# Overwrite ActiveRecord::Base#default_scope
|
42
|
+
class ActiveRecord::Base
|
43
|
+
# default_scope fix discussed in ticket:
|
44
|
+
# https://rails.lighthouseapp.com/projects/8994/tickets/4583-merge-default-scopes-by-default#ticket-4583-11
|
45
|
+
def self.default_scope(options = {})
|
46
|
+
key = :"#{self}_scoped_methods"
|
47
|
+
Thread.current[key] = nil
|
48
|
+
self.default_scoping << construct_finder_arel(options, default_scoping.pop)
|
49
|
+
end
|
50
|
+
end
|
data/lib/instance_methods.rb
CHANGED
@@ -12,5 +12,18 @@ module DeleteSoftly
|
|
12
12
|
@destroyed = true
|
13
13
|
freeze
|
14
14
|
end
|
15
|
+
|
16
|
+
def revive
|
17
|
+
if self.class.respond_to?(:paper_trail_active) && self.class.paper_trail_active
|
18
|
+
self.class.paper_trail_off
|
19
|
+
update_attribute :deleted_at, nil
|
20
|
+
self.class.paper_trail_on
|
21
|
+
else
|
22
|
+
update_attribute :deleted_at, nil
|
23
|
+
end
|
24
|
+
end
|
25
|
+
alias_method :undelete, :revive
|
26
|
+
alias_method :undestroy, :revive
|
27
|
+
|
15
28
|
end
|
16
29
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: delete_softly
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Benjamin ter Kuile
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-
|
18
|
+
date: 2010-11-05 00:00:00 +01:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -47,6 +47,7 @@ files:
|
|
47
47
|
- MIT-LICENSE
|
48
48
|
- README
|
49
49
|
- Rakefile
|
50
|
+
- delete_softly.gemspec
|
50
51
|
- init.rb
|
51
52
|
- install.rb
|
52
53
|
- lib/class_methods.rb
|
@@ -56,7 +57,6 @@ files:
|
|
56
57
|
- test/test_helper.rb
|
57
58
|
- uninstall.rb
|
58
59
|
- Manifest
|
59
|
-
- delete_softly.gemspec
|
60
60
|
has_rdoc: true
|
61
61
|
homepage: http://github.com/bterkuile/delete_softly
|
62
62
|
licenses: []
|