rails-trash 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/rails-trash.rb +68 -0
  2. metadata +3 -3
  3. data/lib/trash.rb +0 -66
@@ -0,0 +1,68 @@
1
+ module Rails
2
+ module Trash
3
+
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ end
7
+
8
+ module ClassMethods
9
+
10
+ ##
11
+ # class Entry < ActiveRecord::Base
12
+ # has_trash
13
+ # default_scope where(:deleted_at => nil)
14
+ # end
15
+ #
16
+ def has_trash
17
+ extend ClassMethodsMixin
18
+ include InstanceMethods
19
+ alias_method_chain :destroy, :trash
20
+ end
21
+
22
+ module ClassMethodsMixin
23
+
24
+ def deleted
25
+ deleted_at = Arel::Table.new(self.table_name)[:deleted_at]
26
+ unscoped.where(deleted_at.not_eq(nil))
27
+ end
28
+
29
+ end
30
+
31
+ module InstanceMethods
32
+
33
+ def destroy_with_trash
34
+ return destroy_without_trash if @trash_is_disabled
35
+ deleted_at = Time.now.utc
36
+ self.update_attribute(:deleted_at, deleted_at)
37
+ self.class.reflect_on_all_associations(:has_many).each do |reflection|
38
+ if reflection.options[:dependent].eql?(:destroy)
39
+ self.send(reflection.name).update_all(:deleted_at => deleted_at)
40
+ end
41
+ end
42
+ end
43
+
44
+ def restore
45
+ self.update_attribute(:deleted_at, nil)
46
+ end
47
+
48
+ def disable_trash
49
+ save_val = @trash_is_disabled
50
+ begin
51
+ @trash_is_disabled = true
52
+ yield if block_given?
53
+ ensure
54
+ @trash_is_disabled = save_val
55
+ end
56
+ end
57
+
58
+ def trashed?
59
+ deleted_at.present?
60
+ end
61
+
62
+ end
63
+
64
+ end
65
+ end
66
+ end
67
+
68
+ ActiveRecord::Base.send :include, Rails::Trash
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: rails-trash
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 1.0.0
5
+ version: 1.0.1
6
6
  platform: ruby
7
7
  authors:
8
8
  - Francesc Esplugas
@@ -32,8 +32,8 @@ extensions: []
32
32
  extra_rdoc_files: []
33
33
 
34
34
  files:
35
- - lib/trash.rb
36
- homepage:
35
+ - lib/rails-trash.rb
36
+ homepage: https://github.com/fesplugas/rails-trash
37
37
  licenses: []
38
38
 
39
39
  post_install_message:
@@ -1,66 +0,0 @@
1
- module Trash
2
-
3
- def self.included(base)
4
- base.extend ClassMethods
5
- end
6
-
7
- module ClassMethods
8
-
9
- ##
10
- # class Entry < ActiveRecord::Base
11
- # has_trash
12
- # default_scope where(:deleted_at => nil)
13
- # end
14
- #
15
- def has_trash
16
- extend ClassMethodsMixin
17
- include InstanceMethods
18
- alias_method_chain :destroy, :trash
19
- end
20
-
21
- module ClassMethodsMixin
22
-
23
- def deleted
24
- deleted_at = Arel::Table.new(self.table_name)[:deleted_at]
25
- unscoped.where(deleted_at.not_eq(nil))
26
- end
27
-
28
- end
29
-
30
- module InstanceMethods
31
-
32
- def destroy_with_trash
33
- return destroy_without_trash if @trash_is_disabled
34
- deleted_at = Time.now.utc
35
- self.update_attribute(:deleted_at, deleted_at)
36
- self.class.reflect_on_all_associations(:has_many).each do |reflection|
37
- if reflection.options[:dependent].eql?(:destroy)
38
- self.send(reflection.name).update_all(:deleted_at => deleted_at)
39
- end
40
- end
41
- end
42
-
43
- def restore
44
- self.update_attribute(:deleted_at, nil)
45
- end
46
-
47
- def disable_trash
48
- save_val = @trash_is_disabled
49
- begin
50
- @trash_is_disabled = true
51
- yield if block_given?
52
- ensure
53
- @trash_is_disabled = save_val
54
- end
55
- end
56
-
57
- def trashed?
58
- deleted_at.present?
59
- end
60
-
61
- end
62
-
63
- end
64
- end
65
-
66
- ActiveRecord::Base.send :include, Trash