Module: OnDestroy::Model

Extended by:
ActiveSupport::Concern
Defined in:
lib/on_destroy/model.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#destroyObject

if self.do_not_delete? runs no/empty callback on :destroy, otherwise calls super.



37
38
39
40
41
42
43
44
45
# File 'lib/on_destroy/model.rb', line 37

def destroy
  if self.do_not_delete?
    # don't destroy
    run_callbacks(:destroy) {}
  else
    # destroy
    super
  end
end

#destroyed?Boolean Also known as: deleted?

Returns:

  • (Boolean)


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/on_destroy/model.rb', line 52

def destroyed?
  if self.on_destroy_options
    is_deleted_if = self.on_destroy_options[:is_deleted_if]
    o_set = self.on_destroy_options[:set]
    o_to = self.on_destroy_options[:to]
    if is_deleted_if.is_a?(Proc)
      send(o_set) == is_deleted_if.call
    elsif !(o_set.nil?)
      if o_to.is_a?(Proc)
        # assume that a :to defined as a Proc is going to evaluate to a non-nil to indicate the model is null
        send(o_set) != nil
      else
        send(o_set) == o_to
      end
    end
  else
    super
  end
end

#do_on_destroyObject

if self.set then will use update_attributes! to set the self.set attribute to self.to or self.to.call if it is a Proc.



27
28
29
30
31
32
33
34
# File 'lib/on_destroy/model.rb', line 27

def do_on_destroy
  if self.on_destroy_options
    o_set = self.on_destroy_options[:set]
    o_to = self.on_destroy_options[:to]
    update_attributes! o_set => (o_to.is_a?(Proc) ? o_to.call : o_to) unless o_set.nil?
    yield
  end
end

#really_destroyObject

runs delete callback on :destroy



48
49
50
# File 'lib/on_destroy/model.rb', line 48

def really_destroy
  run_callbacks(:destroy) {delete}
end