rails-trash 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/trash.rb +66 -0
  2. metadata +64 -0
data/lib/trash.rb ADDED
@@ -0,0 +1,66 @@
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
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails-trash
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 1.0.0
6
+ platform: ruby
7
+ authors:
8
+ - Francesc Esplugas
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-06-05 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rails
17
+ prerelease: false
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 3.0.0
24
+ type: :runtime
25
+ version_requirements: *id001
26
+ description:
27
+ email: contact@francescesplugas.com
28
+ executables: []
29
+
30
+ extensions: []
31
+
32
+ extra_rdoc_files: []
33
+
34
+ files:
35
+ - lib/trash.rb
36
+ homepage:
37
+ licenses: []
38
+
39
+ post_install_message:
40
+ rdoc_options: []
41
+
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ requirements: []
57
+
58
+ rubyforge_project:
59
+ rubygems_version: 1.8.5
60
+ signing_key:
61
+ specification_version: 3
62
+ summary: Trash
63
+ test_files: []
64
+