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



57
58
59
# File 'lib/cms/behaviors/soft_deleting.rb', line 57

def count_with_deleted(* args)
  self.unscoped.count(* args)
end

#delete_all(conditions = nil) ⇒ Object



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

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

#exists?(id_or_conditions) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
68
69
70
71
72
# File 'lib/cms/behaviors/soft_deleting.rb', line 65

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

#find_with_deleted(options) ⇒ Object

Returns a content block even if it is marked as deleted.

Parameters:

  • options (Hash)

    Hash suitable to be passed to ‘#where’



48
49
50
# File 'lib/cms/behaviors/soft_deleting.rb', line 48

def find_with_deleted(options)
  self.unscoped.where(options).first
end