Module: Caboose::Acts::ParanoidFindWrapper

Defined in:
lib/caboose/acts/paranoid_find_wrapper.rb

Overview

Adds a wrapper find method which can identify :with_deleted or :only_deleted options and would call the corresponding acts_as_paranoid finders find_with_deleted or find_only_deleted methods.

With this wrapper you can easily change from using this pattern:

if some_condition_enabling_access_to_deleted_records?
  @post = Post.find_with_deleted(params[:id])
else
  @post = Post.find(params[:id])
end

to this:

@post = Post.find(params[:id], :with_deleted => some_condition_enabling_access_to_deleted_records?)

Examples

class Widget < ActiveRecord::Base
  acts_as_paranoid
end

Widget.find(:all)
# SELECT * FROM widgets WHERE widgets.deleted_at IS NULL

Widget.find(:all, :with_deleted => false)
# SELECT * FROM widgets WHERE widgets.deleted_at IS NULL

Widget.find_with_deleted(:all)
# SELECT * FROM widgets

Widget.find(:all, :with_deleted => true)
# SELECT * FROM widgets

Widget.find_only_deleted(:all)
# SELECT * FROM widgets WHERE widgets.deleted_at IS NOT NULL

Widget.find(:all, :only_deleted => true)
# SELECT * FROM widgets WHERE widgets.deleted_at IS NOT NULL

Widget.find(:all, :only_deleted => false)
# SELECT * FROM widgets WHERE widgets.deleted_at IS NULL

Defined Under Namespace

Modules: ClassMethods, InstanceMethods

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object

:nodoc:



47
48
49
# File 'lib/caboose/acts/paranoid_find_wrapper.rb', line 47

def self.included(base) # :nodoc:
  base.extend ClassMethods
end