Skip to content

Commit

Permalink
* Added query to ask a class or instance if it is paranoid
Browse files Browse the repository at this point in the history
* Added test setup
  • Loading branch information
donv committed May 12, 2011
1 parent 9879f78 commit d6163b5
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
6 changes: 6 additions & 0 deletions 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
11 changes: 11 additions & 0 deletions 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
Expand All @@ -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
40 changes: 40 additions & 0 deletions 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

0 comments on commit d6163b5

Please sign in to comment.