Class: ActiveRecord::Base::EnsureNotUsedBy

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

Overview

ActiveRecord Callback class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*attribute) ⇒ EnsureNotUsedBy


34
35
36
37
# File 'lib/core_extensions.rb', line 34

def initialize *attribute
  @klasses = attribute
  @logger  = Rails.logger
end

Instance Attribute Details

#klassesObject (readonly)

Returns the value of attribute klasses


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

def klasses
  @klasses
end

#loggerObject (readonly)

Returns the value of attribute logger


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

def logger
  @logger
end

Instance Method Details

#before_destroy(record) ⇒ Object


39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/core_extensions.rb', line 39

def before_destroy(record)
  klasses.each do |klass|
    record.send(klass.to_sym).each do |what|
      record.errors.add :base, "#{record} is used by #{what}"
    end
  end
  if record.errors.empty?
    true
  else
    logger.error "You may not destroy #{record.to_label} as it is in use!"
    false
  end
end