Module: DeleteSoftly::ClassMethods

Defined in:
lib/class_methods.rb

Instance Method Summary collapse

Instance Method Details

#activeObject

Give the currently active items. When delete_soflty is added this is invoked by default But when false is added, items are shown by default

class Item < ActiveRecord::Base
  delete_softly false
end

Or similar:

class Item < ActiveRecord::Base
  delete_softly :default => false
end

You need to call active on the model where you want to hide deleted items

Item.all #=> SELECT "items".* FROM "items"
Item.active #=> SELECT "items".* FROM "items" WHERE ("items"."deleted_at" IS NULL)


33
34
35
# File 'lib/class_methods.rb', line 33

def active
  where('deleted_at IS NULL')
end

#at_time(date = Time.now.utc) ⇒ Object Also known as: at_date

Give the representation of items at a certain date/time.

class Item < ActiveRecord::Base
  delete_softly
end

Will result in:

Item.at_time(DateTime.parse('2010-01-01')) #=> (SELECT "items".* FROM "items" WHERE (((("items"."deleted_at" > '2010-01-01 00:00:00') OR ("items"."deleted_at" IS NULL)) AND ("items"."created_at" < '2010-01-01 00:00:00')))


11
12
13
14
15
# File 'lib/class_methods.rb', line 11

def at_time(date = Time.now.utc)
  with_deleted do
    where('(deleted_at > :date OR deleted_at IS NULL) AND created_at < :date', {:date => date})
  end
end

#deletedObject



61
62
63
64
65
# File 'lib/class_methods.rb', line 61

def deleted
  with_deleted do 
    where( "deleted_at IS NOT NULL" )
  end
end

#version_at(time) ⇒ Object

Support for paper_trail if it is installed as well. Then you can use:

class Post < ActiveRecord::Base
  default_scope order(:created_at)
  delete_softly
  has_paper_trail
  has_many :comments
end

Then

Post.version_at(1.week.ago)

Will return all post of one week ago, with the appropriate field values at that time



77
78
79
80
81
82
83
# File 'lib/class_methods.rb', line 77

def version_at(time)
  if respond_to?(:at_time)
    at_time(time).map{|i| i.respond_to?(:version_at) ? i.version_at(time) : i}.compact
  else
    scoped.map{|i| i.respond_to?(:version_at) ? i.version_at(time) : i}.compact
  end
end

#with_deleted(&block) ⇒ Object

Include deleted items when performing queries

class Item < ActiveRecord::Base
  default_scope order(:content)
  delete_softly
end

Will result in:

Item.first #=> SELECT "items".* FROM "items" WHERE ("items"."deleted_at" IS NULL) ORDER BY "items"."content" LIMIT 1
Item.with_deleted.first #=> SELECT "items".* FROM "items" ORDER BY "items"."content" LIMIT 1
Item.where(:content.matches => 'a%') #=> SELECT "items".* FROM "items" WHERE ("items"."deleted_at" IS NULL) AND ("items"."content" ILIKE 'a%') ORDER BY "items"."content"
Item.with_deleted do
  Item.where(:content.matches => 'a%') #=> SELECT "items".* FROM "items" WHERE ("items"."content" ILIKE 'a%') ORDER BY "items"."content"
end
IHaveManyItems.first.items #=> SELECT "items".* FROM "items" WHERE ("items"."deleted_at" IS NULL) AND ("items".i_have_many_items_id = 1) ORDER BY "items"."content"
IHaveManyItems.first.items.with_deleted #=> SELECT "items".* FROM "items" WHERE ("items".i_have_many_items_id = 1) ORDER BY "items"."content"


57
58
59
# File 'lib/class_methods.rb', line 57

def with_deleted(&block)
  unscoped(&block)
end

#without_deletedObject

Same as active, but not to be overwritten. Active might become with disabled => false or something like that. Without deleted should remain intact



39
40
41
# File 'lib/class_methods.rb', line 39

def without_deleted
  where('deleted_at IS NULL')
end