Class: ActiveRecord::Relation

Inherits:
Object
  • Object
show all
Defined in:
lib/undeletable.rb

Instance Method Summary collapse

Instance Method Details

#delete_all(*args, &block) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/undeletable.rb', line 61

def delete_all(*args, &block)
  if klass.respond_to?(:undeletable?) && klass.undeletable?
    raise ActiveRecord::ReadOnlyRecord.new("#{self} is undeletable") if klass.raise_on_delete?
    logger.debug("will not #{self}.delete_all", e) if Undeletable.debug
  else
    undeletable_orig_relation_delete_all(*args, &block)
  end
end

#destroy_all(*args, &block) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/undeletable.rb', line 72

def destroy_all(*args, &block)
  if klass.respond_to?(:undeletable?) && klass.undeletable?
    raise ActiveRecord::ReadOnlyRecord.new("#{self} is undeletable") if klass.raise_on_delete?
    logger.debug("will not #{self}.destroy_all", e) if Undeletable.debug
    if args.length > 0 && block_given?
      where(*args, &block).to_a.each {|object| object.run_callbacks(:destroy) { delete } }.tap { reset }
    elsif args.length > 0 && !block_given?
      where(*args).to_a.each {|object| object.run_callbacks(:destroy) { delete } }.tap { reset }
    elsif args.length == 0 && block_given?
      where(&block).to_a.each {|object| object.run_callbacks(:destroy) { delete } }.tap { reset }
    else
      to_a.each {|object| object.run_callbacks(:destroy) { delete } }.tap { reset }
    end
  else
    undeletable_orig_relation_destroy_all(*args, &block)
  end
end

#undeletable_orig_relation_delete_allObject

don’t use generic naming to try to avoid conflicts, since this isn’t model class specific



60
# File 'lib/undeletable.rb', line 60

alias_method :undeletable_orig_relation_delete_all, :delete_all

#undeletable_orig_relation_destroy_allObject

don’t use generic naming to try to avoid conflicts, since this isn’t model class specific



71
# File 'lib/undeletable.rb', line 71

alias_method :undeletable_orig_relation_destroy_all, :destroy_all