Module: Paranoid::Relation

Extended by:
ActiveSupport::Concern
Defined in:
lib/paranoid/relation.rb

Instance Method Summary collapse

Instance Method Details

#add_paranoid_condition?Boolean

Returns true if the relation should be scoped to exclude soft deleted records

Returns:

  • (Boolean)


14
15
16
17
# File 'lib/paranoid/relation.rb', line 14

def add_paranoid_condition?
  @add_paranoid = true unless defined?(@add_paranoid)
  @klass.paranoid? && @add_paranoid
end

#arel_with_paranoidObject

Overrides ActiveRecord::Relation#arel



20
21
22
23
24
25
26
# File 'lib/paranoid/relation.rb', line 20

def arel_with_paranoid
  if add_paranoid_condition?
    @arel ||= without_destroyed.arel_without_paranoid
  else
    arel_without_paranoid
  end
end

#delete_all_with_paranoid(*args) ⇒ Object

Overrides ActiveRecord::Relation#delete_all forcing delete_all to ignore deleted flag



30
31
32
33
34
35
36
# File 'lib/paranoid/relation.rb', line 30

def delete_all_with_paranoid(*args)
  if add_paranoid_condition?
    with_destroyed.delete_all_without_paranoid(*args)
  else
    delete_all_without_paranoid(*args)
  end
end

#except_with_paranoid(*args) ⇒ Object

Overrides ActiveRecord::Relation#except



39
40
41
42
43
# File 'lib/paranoid/relation.rb', line 39

def except_with_paranoid(*args)
  result = except_without_paranoid(*args)
  result.instance_variable_set(:@add_paranoid, @add_paranoid) if defined?(@add_paranoid)
  result
end

#only_with_paranoid(*args) ⇒ Object

Overrides ActiveRecord::Relation#only



46
47
48
49
50
# File 'lib/paranoid/relation.rb', line 46

def only_with_paranoid(*args)
  result = only_without_paranoid(*args)
  result.instance_variable_set(:@add_paranoid, @add_paranoid) if defined?(@add_paranoid)
  result
end

#with_destroyedObject

Returns a new relation scoped to include soft deleted records



53
54
55
# File 'lib/paranoid/relation.rb', line 53

def with_destroyed
  clone.tap {|relation| relation.skip_paranoid_condition }
end

#with_destroyed_onlyObject

Returns a new relation scoped to include only deleted records



58
59
60
# File 'lib/paranoid/relation.rb', line 58

def with_destroyed_only
  where(@klass.paranoid_only_condition).tap {|relation| relation.skip_paranoid_condition }
end

#without_destroyedObject

Can be used to force the exclusion of soft deleted records down the chain from a with_destroyed call. WARNING: with_destroyed will do nothing after this has been called! So Model.without_destroyed.with_destroyed.all will NOT return soft deleted records



67
68
69
# File 'lib/paranoid/relation.rb', line 67

def without_destroyed
  where(@klass.paranoid_condition).tap {|relation| relation.skip_paranoid_condition }
end