ActsAsParanoid

Public Instance Methods

acts_as_paranoid(options = {}) click to toggle source
# File lib/rails3_acts_as_paranoid.rb, line 20
def acts_as_paranoid(options = {})
  raise ArgumentError, "Hash expected, got #{options.class.name}" if not options.is_a?(Hash) and not options.empty?
  
  class_attribute :paranoid_configuration, :paranoid_column_reference
  
  self.paranoid_configuration = { :column => "deleted_at", :column_type => "time", :recover_dependent_associations => true, :dependent_recovery_window => 2.minutes }
  self.paranoid_configuration.merge!({ :deleted_value => "deleted" }) if options[:column_type] == "string"
  self.paranoid_configuration.merge!(options) # user options

  raise ArgumentError, "'time', 'boolean' or 'string' expected for :column_type option, got #{paranoid_configuration[:column_type]}" unless ['time', 'boolean', 'string'].include? paranoid_configuration[:column_type]

  self.paranoid_column_reference = "#{self.table_name}.#{paranoid_configuration[:column]}"
  
  return if paranoid?
  
  include ActsAsParanoid::Core
  
  # Magic!
  default_scope { where(paranoid_default_scope_sql) }

  if paranoid_configuration[:column_type] == 'time'
    scope :deleted_inside_time_window, lambda {|time, window|
      deleted_after_time((time - window)).deleted_before_time((time + window))
    }

    scope :deleted_after_time, lambda  { |time| where("#{paranoid_column} > ?", time) }
    scope :deleted_before_time, lambda { |time| where("#{paranoid_column} < ?", time) }
  end
end
paranoid?() click to toggle source
# File lib/rails3_acts_as_paranoid.rb, line 12
def paranoid?
  self.included_modules.include?(ActsAsParanoid::Core)
end
validates_as_paranoid() click to toggle source
# File lib/rails3_acts_as_paranoid.rb, line 16
def validates_as_paranoid
  include ActsAsParanoid::Validations
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.