diff --git a/Rakefile b/Rakefile index 14cfe0b5..f73c7138 100644 --- a/Rakefile +++ b/Rakefile @@ -1,2 +1,8 @@ require 'bundler' Bundler::GemHelper.install_tasks + +task :test do + Dir['test/*_test.rb'].each do |testfile| + load testfile + end +end \ No newline at end of file diff --git a/lib/paranoia.rb b/lib/paranoia.rb index a573a2d2..7b155324 100644 --- a/lib/paranoia.rb +++ b/lib/paranoia.rb @@ -1,4 +1,12 @@ module Paranoia + def self.included(klazz) + klazz.extend Query + end + + module Query + def paranoid? ; true ; end + end + def destroy _run_destroy_callbacks self[:deleted_at] ||= Time.now @@ -17,4 +25,7 @@ def self.acts_as_paranoid self.send(:include, Paranoia) default_scope :conditions => { :deleted_at => nil } end + + def self.paranoid? ; false ; end + def paranoid? ; self.class.paranoid? ; end end diff --git a/test/paranoia_test.rb b/test/paranoia_test.rb new file mode 100644 index 00000000..eb1513b5 --- /dev/null +++ b/test/paranoia_test.rb @@ -0,0 +1,40 @@ +require 'test/unit' +require 'active_record' +require 'lib/paranoia' + +DB_FILE = 'tmp/test_db' + +FileUtils.mkdir_p File.dirname(DB_FILE) +FileUtils.rm_f DB_FILE + +ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => DB_FILE +ActiveRecord::Base.connection.execute 'CREATE TABLE paranoid_models (id INTEGER NOT NULL PRIMARY KEY)' +ActiveRecord::Base.connection.execute 'CREATE TABLE plain_models (id INTEGER NOT NULL PRIMARY KEY)' + +class ParanoiaTest < Test::Unit::TestCase + def test_plain_model_class_is_not_paranoid + assert_equal false, PlainModel.paranoid? + end + + def test_paranoid_model_class_is_paranoid + assert_equal true, ParanoidModel.paranoid? + end + + def test_plain_models_are_not_paranoid + assert_equal false, PlainModel.new.paranoid? + end + + def test_paranoid_models_are_paranoid + assert_equal true, ParanoidModel.new.paranoid? + end + +end + +# Helper classes + +class ParanoidModel < ActiveRecord::Base + acts_as_paranoid +end + +class PlainModel < ActiveRecord::Base +end