Commit 87fc3481 by Jake Quain Committed by Ryan Bigg

Add configuration option for column name

References: https://github.com/goncalossilva/acts_as_paranoid

Fixes #76
parent 0f2fde6f
...@@ -84,6 +84,16 @@ class Client < ActiveRecord::Base ...@@ -84,6 +84,16 @@ class Client < ActiveRecord::Base
end end
``` ```
If you want to use a column other than deleted_at, you can pass it as an option:
```ruby
class Client < ActiveRecord::Base
acts_as_paranoid column: :destroyed_at
...
end
```
You can replace the older acts_as_paranoid methods as follows: You can replace the older acts_as_paranoid methods as follows:
| Old Syntax | New Syntax | | Old Syntax | New Syntax |
......
...@@ -12,7 +12,7 @@ module Paranoia ...@@ -12,7 +12,7 @@ module Paranoia
end end
def only_deleted def only_deleted
with_deleted.where.not(deleted_at: nil) with_deleted.where.not(paranoia_column => nil)
end end
alias :deleted :only_deleted alias :deleted :only_deleted
...@@ -49,25 +49,28 @@ module Paranoia ...@@ -49,25 +49,28 @@ module Paranoia
def delete def delete
return if new_record? return if new_record?
destroyed? ? destroy! : update_column(:deleted_at, Time.now) destroyed? ? destroy! : update_column(paranoia_column, Time.now)
end end
def restore! def restore!
run_callbacks(:restore) { update_column :deleted_at, nil } run_callbacks(:restore) { update_column paranoia_column, nil }
end end
def destroyed? def destroyed?
!!deleted_at !!send(paranoia_column)
end end
alias :deleted? :destroyed? alias :deleted? :destroyed?
end end
class ActiveRecord::Base class ActiveRecord::Base
def self.acts_as_paranoid def self.acts_as_paranoid(options={})
alias :destroy! :destroy alias :destroy! :destroy
alias :delete! :delete alias :delete! :delete
include Paranoia include Paranoia
default_scope { where(self.quoted_table_name + '.deleted_at IS NULL') } class_attribute :paranoia_column
self.paranoia_column = options[:column] || :deleted_at
default_scope { where(self.quoted_table_name + ".#{paranoia_column} IS NULL") }
end end
def self.paranoid? ; false ; end def self.paranoid? ; false ; end
...@@ -79,4 +82,10 @@ class ActiveRecord::Base ...@@ -79,4 +82,10 @@ class ActiveRecord::Base
def persisted? def persisted?
paranoid? ? !new_record? : super paranoid? ? !new_record? : super
end end
private
def paranoia_column
self.class.paranoia_column
end
end end
...@@ -17,6 +17,7 @@ ActiveRecord::Base.connection.execute 'CREATE TABLE related_models (id INTEGER N ...@@ -17,6 +17,7 @@ ActiveRecord::Base.connection.execute 'CREATE TABLE related_models (id INTEGER N
ActiveRecord::Base.connection.execute 'CREATE TABLE employers (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME)' ActiveRecord::Base.connection.execute 'CREATE TABLE employers (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME)'
ActiveRecord::Base.connection.execute 'CREATE TABLE employees (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME)' ActiveRecord::Base.connection.execute 'CREATE TABLE employees (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME)'
ActiveRecord::Base.connection.execute 'CREATE TABLE jobs (id INTEGER NOT NULL PRIMARY KEY, employer_id INTEGER NOT NULL, employee_id INTEGER NOT NULL, deleted_at DATETIME)' ActiveRecord::Base.connection.execute 'CREATE TABLE jobs (id INTEGER NOT NULL PRIMARY KEY, employer_id INTEGER NOT NULL, employee_id INTEGER NOT NULL, deleted_at DATETIME)'
ActiveRecord::Base.connection.execute 'CREATE TABLE custom_column_models (id INTEGER NOT NULL PRIMARY KEY, destroyed_at DATETIME)'
class ParanoiaTest < Test::Unit::TestCase class ParanoiaTest < Test::Unit::TestCase
def test_plain_model_class_is_not_paranoid def test_plain_model_class_is_not_paranoid
...@@ -88,6 +89,23 @@ class ParanoiaTest < Test::Unit::TestCase ...@@ -88,6 +89,23 @@ class ParanoiaTest < Test::Unit::TestCase
assert_equal [p1,p3], parent1.paranoid_models.with_deleted assert_equal [p1,p3], parent1.paranoid_models.with_deleted
end end
def test_destroy_behavior_for_custom_column_models
model = CustomColumnModel.new
assert_equal 0, model.class.count
model.save!
assert_nil model.destroyed_at
assert_equal 1, model.class.count
model.destroy
assert_equal false, model.destroyed_at.nil?
assert model.destroyed?
assert_equal 0, model.class.count
assert_equal 1, model.class.unscoped.count
assert_equal 1, model.class.only_deleted.count
assert_equal 1, model.class.deleted.count
end
def test_destroy_behavior_for_featureful_paranoid_models def test_destroy_behavior_for_featureful_paranoid_models
model = get_featureful_model model = get_featureful_model
assert_equal 0, model.class.count assert_equal 0, model.class.count
...@@ -314,3 +332,7 @@ class Job < ActiveRecord::Base ...@@ -314,3 +332,7 @@ class Job < ActiveRecord::Base
belongs_to :employer belongs_to :employer
belongs_to :employee belongs_to :employee
end end
class CustomColumnModel < ActiveRecord::Base
acts_as_paranoid column: :destroyed_at
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment