0
+module ActiveRecord
#:nodoc:0
+ # Overrides some basic methods for the current model so that calling #destroy sets a 'deleted_at' field to the current timestamp.
0
+ # This assumes the table has a deleted_at date/time field. Most normal model operations will work, but there will be some oddities.
0
+ # class Widget < ActiveRecord::Base
0
+ # # SELECT * FROM widgets WHERE widgets.deleted_at IS NULL
0
+ # Widget.find(:first, :conditions => ['title = ?', 'test'], :order => 'title')
0
+ # # SELECT * FROM widgets WHERE widgets.deleted_at IS NULL AND title = 'test' ORDER BY title LIMIT 1
0
+ # Widget.find_with_deleted(:all)
0
+ # # SELECT * FROM widgets
0
+ # # SELECT COUNT(*) FROM widgets WHERE widgets.deleted_at IS NULL
0
+ # Widget.count ['title = ?', 'test']
0
+ # # SELECT COUNT(*) FROM widgets WHERE widgets.deleted_at IS NULL AND title = 'test'
0
+ # Widget.count_with_deleted
0
+ # # SELECT COUNT(*) FROM widgets
0
+ # # UPDATE widgets SET deleted_at = '2005-09-17 17:46:36' WHERE id = 1
0
+ # # DELETE FROM widgets WHERE id = 1
0
def self.included(base) # :nodoc:
0
base.extend ClassMethods
0
- alias_method :destroy
!, :destroy0
+ alias_method :destroy
_without_callbacks!, :destroy_without_callbacks0
alias_method :find_with_deleted, :find
0
alias_method :count_with_deleted, :count
0
@@ -18,38 +47,50 @@ module ActiveRecord
0
- module ParanoidMethods #:nodoc:
0
- def self.included(base) # :nodoc:
0
- base.extend ClassMethods
0
+ module ParanoidMethods #:nodoc:
0
+ def self.included(base) # :nodoc:
0
+ base.extend ClassMethods
0
- if [:all, :first].include?(args.first)
0
- constrain = "#{table_name}.deleted_at IS NULL"
0
- constrains = (scope_constrains.nil? or scope_constrains[:conditions].nil? or scope_constrains[:conditions] == constrain) ?
0
- "#{scope_constrains[:conditions]} AND #{constrain}"
0
- constrain(:conditions => constrains) { return find_with_deleted(*args) }
0
+ if [:all, :first].include?(args.first)
0
+ constrain = "#{table_name}.deleted_at IS NULL"
0
+ constrains = (scope_constrains.nil? or scope_constrains[:conditions].nil? or scope_constrains[:conditions] == constrain) ?
0
+ "#{scope_constrains[:conditions]} AND #{constrain}"
0
+ constrain(:conditions => constrains) { return find_with_deleted(*args) }
0
+ find_with_deleted(*args)
0
- find_with_deleted(*args)
0
- def count(conditions = nil, joins = nil)
0
- constrain(:conditions => "#{table_name}.deleted_at IS NULL") { count_with_deleted(conditions, joins) }
0
+ def count(conditions = nil, joins = nil)
0
+ constrain(:conditions => "#{table_name}.deleted_at IS NULL") { count_with_deleted(conditions, joins) }
0
- sql = self.class.send(:sanitize_sql,
0
- ["UPDATE #{self.class.table_name} SET deleted_at = ? WHERE id = ?",
0
- self.class.default_timezone == :utc ? Time.now.utc : Time.now, id])
0
- self.connection.update(sql)
0
+ def destroy_without_callbacks
0
+ sql = self.class.send(:sanitize_sql,
0
+ ["UPDATE #{self.class.table_name} SET deleted_at = ? WHERE id = ?",
0
+ self.class.default_timezone == :utc ? Time.now.utc : Time.now, id])
0
+ self.connection.update(sql)
0
+ def destroy_with_callbacks!
0
+ return false if callback(:before_destroy) == false
0
+ result = destroy_without_callbacks!
0
+ callback(:after_destroy)
0
+ transaction { destroy_with_callbacks! }
0
ActiveRecord::Base.class_eval do
0
include ActiveRecord::Acts::Paranoid
0
-#ActiveRecord::Associations::HasAndBelongsToManyAssociation.class_eval do
0
-# alias_method :find_with_deleted, :find
0
-# constrain(:conditions => "#{@join_table}.deleted_at IS NULL") { find_with_deleted(*args) }
0
\ No newline at end of file
0
\ No newline at end of file
Comments
No one has commented yet.