delete_softly 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 [name of plugin creator]
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Manifest ADDED
@@ -0,0 +1,12 @@
1
+ MIT-LICENSE
2
+ README
3
+ Rakefile
4
+ init.rb
5
+ install.rb
6
+ lib/class_methods.rb
7
+ lib/delete_softly.rb
8
+ lib/instance_methods.rb
9
+ test/delete_softly_test.rb
10
+ test/test_helper.rb
11
+ uninstall.rb
12
+ Manifest
data/README ADDED
@@ -0,0 +1,32 @@
1
+ DeleteSoftly
2
+ ============
3
+
4
+ Add soft delete functionality to ActiveRecord models
5
+
6
+ Example
7
+ =======
8
+ class Post
9
+ delete_softly
10
+ end
11
+
12
+ class Comment
13
+ delete_softly false
14
+ end
15
+
16
+ Now the following stuff works:
17
+ p1 = Post.create
18
+ p2 = Post.create
19
+ Post.count #=> 2
20
+ p2.destroy
21
+ Post.count #=> 1
22
+ Post.at(1.year.ago).count #=> 0
23
+
24
+ c1 = Comment.create
25
+ c2 = Comment.create
26
+ Comment.count #=> 2
27
+ c1.destroy
28
+ Comment.count #=> 2 (Since we added false)
29
+ Comment.active.count #=> 1
30
+
31
+
32
+ Copyright (c) 2010 [Benjamin ter Kuile], released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,35 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+ require 'echoe'
5
+
6
+ desc 'Default: run unit tests.'
7
+ task :default => :test
8
+
9
+ desc 'Test the delete_softly plugin.'
10
+ Rake::TestTask.new(:test) do |t|
11
+ t.libs << 'lib'
12
+ t.libs << 'test'
13
+ t.pattern = 'test/**/*_test.rb'
14
+ t.verbose = true
15
+ end
16
+
17
+ desc 'Echoe'
18
+ Echoe.new('delete_softly', '0.0.1') do |p|
19
+ p.description = "Add soft delete functionality to your ActiveRecord models"
20
+ p.url = "http://github.com/bterkuile/delete_softly"
21
+ p.author = "Benjamin ter Kuile"
22
+ p.email = "bterkuile@gmail.com"
23
+ p.ignore_pattern = ["tmp/*", "script/*"]
24
+ p.runtime_dependencies = ["meta_where"]
25
+ p.development_dependencies = []
26
+ end
27
+
28
+ desc 'Generate documentation for the delete_softly plugin.'
29
+ Rake::RDocTask.new(:rdoc) do |rdoc|
30
+ rdoc.rdoc_dir = 'rdoc'
31
+ rdoc.title = 'DeleteSoftly'
32
+ rdoc.options << '--line-numbers' << '--inline-source'
33
+ rdoc.rdoc_files.include('README')
34
+ rdoc.rdoc_files.include('lib/**/*.rb')
35
+ end
@@ -0,0 +1,34 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{delete_softly}
5
+ s.version = "0.0.1"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Benjamin ter Kuile"]
9
+ s.date = %q{2010-10-31}
10
+ s.description = %q{Add soft delete functionality to your ActiveRecord models}
11
+ s.email = %q{bterkuile@gmail.com}
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", "delete_softly.gemspec"]
14
+ s.homepage = %q{http://github.com/bterkuile/delete_softly}
15
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Delete_softly", "--main", "README"]
16
+ s.require_paths = ["lib"]
17
+ s.rubyforge_project = %q{delete_softly}
18
+ s.rubygems_version = %q{1.3.7}
19
+ s.summary = %q{Add soft delete functionality to your ActiveRecord models}
20
+ s.test_files = ["test/delete_softly_test.rb", "test/test_helper.rb"]
21
+
22
+ if s.respond_to? :specification_version then
23
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
24
+ s.specification_version = 3
25
+
26
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
27
+ s.add_runtime_dependency(%q<meta_where>, [">= 0"])
28
+ else
29
+ s.add_dependency(%q<meta_where>, [">= 0"])
30
+ end
31
+ else
32
+ s.add_dependency(%q<meta_where>, [">= 0"])
33
+ end
34
+ end
data/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ # Include hook code here
2
+ require 'delete_softly'
data/install.rb ADDED
@@ -0,0 +1 @@
1
+ # Install hook code here
@@ -0,0 +1,17 @@
1
+ module DeleteSoftly
2
+ module ClassMethods
3
+ def at_date(date = Time.now.utc)
4
+ with_deleted.where(({:deleted_at.gt => date} | {:deleted_at => nil}) & {:created_at.lt => date})
5
+ end
6
+ alias_method :at, :at_date
7
+ alias_method :at_time, :at_date
8
+
9
+ def active
10
+ where(:deleted_at => nil)
11
+ end
12
+
13
+ def with_deleted
14
+ unscoped
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,38 @@
1
+ # DeleteSoftly
2
+ require 'class_methods'
3
+ require 'instance_methods'
4
+ require 'meta_where'
5
+ module DeleteSoftly
6
+ module ARExtender
7
+
8
+ # Always have Model.active available. It is a good practice to use it
9
+ # when you want the active records. Even use it when no logic is in
10
+ # place yet.
11
+ def active
12
+ scoped
13
+ end
14
+
15
+ # Make the model delete softly. A deleted_at:datetime column is required
16
+ # for this to work
17
+ def delete_softly(options = {:default => :active})
18
+ # Make destroy! the old destroy
19
+ alias_method :destroy!, :destroy
20
+
21
+ include DeleteSoftly::InstanceMethods
22
+ extend DeleteSoftly::ClassMethods
23
+ # Support single argument
24
+ # delete_softly :active
25
+ # delete_softly :default => :with_deleted
26
+ options = {:default => options } unless options.is_a?(Hash)
27
+ if options[:default]
28
+ if options[:default].is_a?(Symbol) && respond_to?(options[:default])
29
+ default_scope send(options[:default])
30
+ else
31
+ default_scope active
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+
38
+ ActiveRecord::Base.send(:extend, DeleteSoftly::ARExtender)
@@ -0,0 +1,16 @@
1
+ module DeleteSoftly
2
+ module InstanceMethods
3
+ def destroy
4
+ if persisted?
5
+ with_transaction_returning_status do
6
+ _run_destroy_callbacks do
7
+ update_attributes(:deleted_at => Time.now.utc)
8
+ end
9
+ end
10
+ end
11
+
12
+ @destroyed = true
13
+ freeze
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,8 @@
1
+ require 'test_helper'
2
+
3
+ class DeleteSoftlyTest < ActiveSupport::TestCase
4
+ # Replace this with your real tests.
5
+ test "the truth" do
6
+ assert true
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'active_support'
data/uninstall.rb ADDED
@@ -0,0 +1 @@
1
+ # Uninstall hook code here
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: delete_softly
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Benjamin ter Kuile
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-10-31 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: meta_where
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ description: Add soft delete functionality to your ActiveRecord models
36
+ email: bterkuile@gmail.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - README
43
+ - lib/class_methods.rb
44
+ - lib/delete_softly.rb
45
+ - lib/instance_methods.rb
46
+ files:
47
+ - MIT-LICENSE
48
+ - README
49
+ - Rakefile
50
+ - init.rb
51
+ - install.rb
52
+ - lib/class_methods.rb
53
+ - lib/delete_softly.rb
54
+ - lib/instance_methods.rb
55
+ - test/delete_softly_test.rb
56
+ - test/test_helper.rb
57
+ - uninstall.rb
58
+ - Manifest
59
+ - delete_softly.gemspec
60
+ has_rdoc: true
61
+ homepage: http://github.com/bterkuile/delete_softly
62
+ licenses: []
63
+
64
+ post_install_message:
65
+ rdoc_options:
66
+ - --line-numbers
67
+ - --inline-source
68
+ - --title
69
+ - Delete_softly
70
+ - --main
71
+ - README
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ hash: 3
80
+ segments:
81
+ - 0
82
+ version: "0"
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ hash: 11
89
+ segments:
90
+ - 1
91
+ - 2
92
+ version: "1.2"
93
+ requirements: []
94
+
95
+ rubyforge_project: delete_softly
96
+ rubygems_version: 1.3.7
97
+ signing_key:
98
+ specification_version: 3
99
+ summary: Add soft delete functionality to your ActiveRecord models
100
+ test_files:
101
+ - test/delete_softly_test.rb
102
+ - test/test_helper.rb