Module: Cms::Behaviors::SoftDeleting::ClassMethods

Defined in:
lib/cms/behaviors/soft_deleting.rb

Overview

TODO: Refactor this class to remove need for overriding count, delete_all, etc. Should not be necessary due to introduction of 'default_scope'.

  1. TODO: Allow a record to define its own default_scope that doesn't 'override' this one.

See github.com/fernandoluizao/acts_as_active for an implementation of this

Instance Method Summary collapse

Instance Method Details

#count_with_deleted(*args) ⇒ Object

Returns a count of all records of this type, including those marked as deleted.

Behaves like ActiveRecord.count is originally implemented.

Parameters:

  • args

    Same params as ActiveRecord.count



60
61
62
# File 'lib/cms/behaviors/soft_deleting.rb', line 60

def count_with_deleted(* args)
  self.with_exclusive_scope { count(* args) }
end

#delete_all(conditions = nil) ⇒ Object



64
65
66
# File 'lib/cms/behaviors/soft_deleting.rb', line 64

def delete_all(conditions=nil)
  update_all(["deleted = ?", true], conditions)
end

#exists?(id_or_conditions) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
71
72
73
74
75
# File 'lib/cms/behaviors/soft_deleting.rb', line 68

def exists?(id_or_conditions)
  if id_or_conditions.is_a?(Hash) || id_or_conditions.is_a?(Array)
    conditions = {:conditions => id_or_conditions}
  else
    conditions = {:conditions => {:id => id_or_conditions}}
  end
  count(conditions) > 0
end

#find_with_deleted(*args) ⇒ Object

Returns all records, including those which are marked as deleted.

Basically 'find' exactly how ActiveRecord originally implements it.

Parameters:

  • args

    Same params as ActiveRecord.find



51
52
53
# File 'lib/cms/behaviors/soft_deleting.rb', line 51

def find_with_deleted(* args)
  self.with_exclusive_scope { find(* args) }
end