Permalink
Browse files

add plugin to source control

git-svn-id: http://llama/svn/trunk/ruby/acts_as_soft_deletable@219 bb26965d-a405-0410-8ce8-d62df5cd24e9
  • Loading branch information...
(no author)
(no author) committed Mar 4, 2008
0 parents commit 42758de8ee600a42efe747be5f18a5d478ce077d
Showing with 796 additions and 0 deletions.
  1. +13 −0 Rakefile
  2. +80 −0 lib/acts_as_soft_deletable.rb
  3. +12 −0 test/base_test.rb
  4. +18 −0 test/config.yml
  5. +632 −0 test/debug.log
  6. +2 −0 test/fixtures/artists.yml
  7. +3 −0 test/fixtures/models.rb
  8. +9 −0 test/fixtures/schema.rb
  9. +27 −0 test/test_helper.rb
@@ -0,0 +1,13 @@
+require 'rubygems'
+require 'rake/testtask'
+
+desc 'Default: run unit tests.'
+task :default => :test
+
+desc 'Test the plugin.'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
+
@@ -0,0 +1,80 @@
+module ActiveRecord #:nodoc:
+ module Acts #:nodoc:
+ # Specify this act if you wish to save a copy of the row in a special deleted table so that it can be
+ # restored later.
+ module SoftDeletable
+ module ClassMethods
+ def acts_as_soft_deletable
+ # don't allow multiple calls
+ #return if self.included_modules.include?(ActiveRecord::Acts::Versioned::ActMethods)
+ include Model::InstanceMethods
+ extend Model::ClassMethods
+
+ model_class = self
+ @deleted_class = const_set("Deleted", Class.new(ActiveRecord::Base)).class_eval do
+ # class of undeleted model
+ cattr_accessor :model_class
+
+ self.model_class = model_class
+ self.set_table_name "deleted_#{model_class.table_name}"
+
+ extend Deleted::ClassMethods
+ end
+ end
+ end
+
+ module Deleted
+ module ClassMethods
+ def create_table(create_table_options = {})
+ connection.create_table(table_name, create_table_options) do |t|
+ model_class.columns.select{|c| !excluded_columns.include?(c.name)}.each do |col|
+ t.column col.name, col.type
+ #:limit => col.limit,
+ #:default => col.default,
+ #:scale => col.scale,
+ #:precision => col.precision
+ end
+ t.datetime :deleted_at
+ end
+ end
+
+ # returns a list of column names that will not be archived
+ def excluded_columns
+ [model_class.primary_key]
+ end
+ end
+ end
+
+ module Model
+
+ module ClassMethods
+ # returns instance of deleted class
+ def deleted_class
+ @deleted_class
+ end
+ end
+
+ module InstanceMethods
+ def self.included(base)
+ base.class_eval do
+ alias_method_chain :destroy, :soft_delete
+ end
+ end
+
+ def destroy_with_soft_delete
+ self.class.transaction do
+ deleted = self.class.deleted_class.new
+ self.attributes.keys.each do |key|
+ deleted.send("#{key}=", self.send(key))
+ end
+ deleted.save!
+ destroy_without_soft_delete
+ end
+ end
+ end
+ end
+ end
+ end
+end
+
+ActiveRecord::Base.send :extend, ActiveRecord::Acts::SoftDeletable::ClassMethods
@@ -0,0 +1,12 @@
+require File.join(File.dirname(__FILE__), 'test_helper')
+
+class BaseTest < Test::Unit::TestCase
+ def test_destroy_creates_a_deleted_model
+ artist = Artist.find_by_name('Chick Corea')
+ artist.destroy
+ assert_nil Artist.find_by_name('Chick Corea')
+
+ deleted = Artist::Deleted.find_by_name('Chick Corea')
+ assert_equal artist.attributes, deleted.attributes.reject{|k,v| k == 'deleted_at'}
+ end
+end
@@ -0,0 +1,18 @@
+sqlite:
+ :adapter: sqlite
+ :dbfile: acts_as_soft_deletable_plugin.sqlite.db
+sqlite3:
+ :adapter: sqlite3
+ :dbfile: acts_as_soft_deletable_plugin.sqlite3.db
+postgresql:
+ :adapter: postgresql
+ :username: postgres
+ :password: postgres
+ :database: acts_as_soft_deletable_plugin_test
+ :min_messages: ERROR
+mysql:
+ :adapter: mysql
+ :host: localhost
+ :username: rails
+ :password:
+ :database: acts_as_soft_deletable_plugin_test

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -0,0 +1,2 @@
+chick:
+ name: Chick Corea
@@ -0,0 +1,3 @@
+class Artist < ActiveRecord::Base
+ acts_as_soft_deletable
+end
@@ -0,0 +1,9 @@
+ActiveRecord::Schema.define(:version => 1) do
+ create_table :artists, :force => true do |t|
+ t.string :name
+ t.date :birthday
+ t.string :description
+ t.timestamps
+ end
+ Artist::Deleted.create_table(:force => true)
+end
@@ -0,0 +1,27 @@
+$:.unshift(File.dirname(__FILE__) + '/../lib')
+require 'test/unit'
+# pull in a rails projects environment.rb
+require '/home/ajh/devel/substantial/gorillakiller/config/environment.rb'
+require 'rubygems'
+require 'active_record/fixtures'
+require 'acts_as_soft_deletable'
+
+config = YAML::load(IO.read(File.dirname(__FILE__) + '/config.yml'))
+ActiveRecord::Base.logger = Logger.new File.dirname(__FILE__) + "/debug.log"
+ActiveRecord::Base.establish_connection config[ENV['DB'] || 'mysql']
+
+require File.join(File.dirname(__FILE__),"fixtures", "models.rb")
+load File.join(File.dirname(__FILE__),"fixtures", "schema.rb")
+
+# what does these do?
+#Test::Unit::TestCase.fixture_path = File.dirname(__FILE__) + "/fixtures/"
+#$LOAD_PATH.unshift(Test::Unit::TestCase.fixture_path)
+
+class Test::Unit::TestCase #:nodoc:
+ self.use_transactional_fixtures = true
+ self.use_instantiated_fixtures = false
+ Fixtures.create_fixtures( \
+ File.join(File.dirname(__FILE__), "fixtures"),
+ Dir.glob('test/fixtures/*.yml').collect{|y| File.basename(y).match(%r/(.*)\.yml/)[1]}
+ )
+end

0 comments on commit 42758de

Please sign in to comment.