delete_paranoid 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+
6
+ #test resources
7
+ test/*.log
8
+ test/*.sqlite3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in delete_paranoid.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Ryan Sonnek
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/README.rdoc ADDED
@@ -0,0 +1,29 @@
1
+ = delete_paranoid
2
+
3
+ Soft Delete ActiveRecord instances.
4
+
5
+ == Usage
6
+ class Blog < ActiveRecord::Base
7
+ acts_as_paranoid
8
+ end
9
+
10
+ blog = Blog.create :name => 'foo'
11
+ blog.destroy #soft delete the record
12
+
13
+ == Features
14
+
15
+ == Contributing
16
+
17
+ * Fork the project
18
+ * Fix the issue
19
+ * Submit a pull request on github
20
+
21
+ == Patch acceptance criteria
22
+
23
+ * tests must be included
24
+
25
+ == Copyright
26
+
27
+ Copyright (c) 2011 Ryan Sonnek. See LICENSE.txt for
28
+ further details.
29
+
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rake'
5
+
6
+ require 'rake/testtask'
7
+ Rake::TestTask.new(:test) do |test|
8
+ test.libs << 'lib' << 'test'
9
+ test.pattern = 'test/**/test_*.rb'
10
+ test.verbose = true
11
+ end
12
+ task :default => :test
13
+
@@ -0,0 +1,28 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "delete_paranoid/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "delete_paranoid"
7
+ s.version = DeleteParanoid::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Ryan Sonnek"]
10
+ s.email = ["ryan@codecrate.com"]
11
+ s.homepage = "http://github.com/wireframe/delete_paranoid"
12
+ s.summary = %q{soft delete Rails ActiveRecord objects}
13
+ s.description = %q{flag database records as deleted and hide them from subsequent queries}
14
+
15
+ s.rubyforge_project = "delete_paranoid"
16
+
17
+ s.add_runtime_dependency(%q<activerecord>, ["~> 3.0.0"])
18
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
19
+ s.add_development_dependency(%q<mocha>, [">= 0"])
20
+ s.add_development_dependency(%q<bundler>, [">= 0"])
21
+ s.add_development_dependency(%q<sqlite3-ruby>, ["~> 1.3.2"])
22
+ s.add_development_dependency(%q<ruby-debug>, [">= 0"])
23
+
24
+ s.files = `git ls-files`.split("\n")
25
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
26
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
27
+ s.require_paths = ["lib"]
28
+ end
@@ -0,0 +1,3 @@
1
+ module DeleteParanoid
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,37 @@
1
+ require 'active_record'
2
+
3
+ module DeleteParanoid
4
+ module ActiveRecordExtensions
5
+ def acts_as_paranoid
6
+ class << self
7
+ alias_method :destroy!, :destroy
8
+ end
9
+ default_scope where(:deleted_at => nil)
10
+ include DeleteParanoid::InstanceMethods
11
+ extend DeleteParanoid::ClassMethods
12
+ end
13
+ end
14
+ module ClassMethods
15
+ def with_deleted(&block)
16
+ self.unscoped do
17
+ yield
18
+ end
19
+ end
20
+ end
21
+ module InstanceMethods
22
+ def destroy
23
+ if persisted?
24
+ with_transaction_returning_status do
25
+ _run_destroy_callbacks do
26
+ update_attributes(:deleted_at => Time.now.utc)
27
+ end
28
+ end
29
+ end
30
+
31
+ @destroyed = true
32
+ freeze
33
+ end
34
+ end
35
+ end
36
+
37
+ ActiveRecord::Base.send(:extend, DeleteParanoid::ActiveRecordExtensions)
data/test/database.yml ADDED
@@ -0,0 +1,4 @@
1
+ sqlite:
2
+ adapter: sqlite3
3
+ database: test/delete_paranoid.sqlite3
4
+
@@ -0,0 +1,13 @@
1
+ require 'logger'
2
+ config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
3
+ ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
4
+ ActiveRecord::Base.establish_connection(config[ENV['DB'] || 'sqlite'])
5
+
6
+ ActiveRecord::Schema.define(:version => 1) do
7
+ create_table :blogs, :force => true do |t|
8
+ t.column :title, :string
9
+ t.column :body, :string
10
+ t.column :deleted_at, :timestamp
11
+ end
12
+ end
13
+
data/test/helper.rb ADDED
@@ -0,0 +1,22 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+ require 'mocha'
13
+ require "ruby-debug"
14
+
15
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
16
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
17
+ require 'delete_paranoid'
18
+ require 'database_setup'
19
+
20
+ class Test::Unit::TestCase
21
+ end
22
+
@@ -0,0 +1,29 @@
1
+ require File.join(File.dirname(__FILE__), 'helper')
2
+
3
+ class TestDeleteParanoid < Test::Unit::TestCase
4
+ class Blog < ActiveRecord::Base
5
+ acts_as_paranoid
6
+ end
7
+ context 'with paranoid class' do
8
+ should 'have destroy! method' do
9
+ assert Blog.respond_to? :destroy!
10
+ end
11
+ context 'when on instance destroyed softly' do
12
+ setup do
13
+ @blog = Blog.create! :title => 'foo'
14
+ @blog.destroy
15
+ end
16
+ should 'not be included in all results' do
17
+ assert !Blog.all.include?(@blog)
18
+ end
19
+ should 'be included when wrapped in with_deleted block' do
20
+ Blog.with_deleted do
21
+ assert Blog.all.include?(@blog)
22
+ end
23
+ end
24
+ end
25
+
26
+ should 'fire destroy callbacks'
27
+ should 'hard delete with destroy!'
28
+ end
29
+ end
metadata ADDED
@@ -0,0 +1,169 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: delete_paranoid
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Ryan Sonnek
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-03-30 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: activerecord
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 7
30
+ segments:
31
+ - 3
32
+ - 0
33
+ - 0
34
+ version: 3.0.0
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: shoulda
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 3
46
+ segments:
47
+ - 0
48
+ version: "0"
49
+ type: :development
50
+ version_requirements: *id002
51
+ - !ruby/object:Gem::Dependency
52
+ name: mocha
53
+ prerelease: false
54
+ requirement: &id003 !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ hash: 3
60
+ segments:
61
+ - 0
62
+ version: "0"
63
+ type: :development
64
+ version_requirements: *id003
65
+ - !ruby/object:Gem::Dependency
66
+ name: bundler
67
+ prerelease: false
68
+ requirement: &id004 !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ hash: 3
74
+ segments:
75
+ - 0
76
+ version: "0"
77
+ type: :development
78
+ version_requirements: *id004
79
+ - !ruby/object:Gem::Dependency
80
+ name: sqlite3-ruby
81
+ prerelease: false
82
+ requirement: &id005 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ~>
86
+ - !ruby/object:Gem::Version
87
+ hash: 31
88
+ segments:
89
+ - 1
90
+ - 3
91
+ - 2
92
+ version: 1.3.2
93
+ type: :development
94
+ version_requirements: *id005
95
+ - !ruby/object:Gem::Dependency
96
+ name: ruby-debug
97
+ prerelease: false
98
+ requirement: &id006 !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ hash: 3
104
+ segments:
105
+ - 0
106
+ version: "0"
107
+ type: :development
108
+ version_requirements: *id006
109
+ description: flag database records as deleted and hide them from subsequent queries
110
+ email:
111
+ - ryan@codecrate.com
112
+ executables: []
113
+
114
+ extensions: []
115
+
116
+ extra_rdoc_files: []
117
+
118
+ files:
119
+ - .gitignore
120
+ - Gemfile
121
+ - LICENSE.txt
122
+ - README.rdoc
123
+ - Rakefile
124
+ - delete_paranoid.gemspec
125
+ - lib/delete_paranoid.rb
126
+ - lib/delete_paranoid/version.rb
127
+ - test/database.yml
128
+ - test/database_setup.rb
129
+ - test/helper.rb
130
+ - test/test_delete_paranoid.rb
131
+ has_rdoc: true
132
+ homepage: http://github.com/wireframe/delete_paranoid
133
+ licenses: []
134
+
135
+ post_install_message:
136
+ rdoc_options: []
137
+
138
+ require_paths:
139
+ - lib
140
+ required_ruby_version: !ruby/object:Gem::Requirement
141
+ none: false
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ hash: 3
146
+ segments:
147
+ - 0
148
+ version: "0"
149
+ required_rubygems_version: !ruby/object:Gem::Requirement
150
+ none: false
151
+ requirements:
152
+ - - ">="
153
+ - !ruby/object:Gem::Version
154
+ hash: 3
155
+ segments:
156
+ - 0
157
+ version: "0"
158
+ requirements: []
159
+
160
+ rubyforge_project: delete_paranoid
161
+ rubygems_version: 1.6.2
162
+ signing_key:
163
+ specification_version: 3
164
+ summary: soft delete Rails ActiveRecord objects
165
+ test_files:
166
+ - test/database.yml
167
+ - test/database_setup.rb
168
+ - test/helper.rb
169
+ - test/test_delete_paranoid.rb