module Protective::InstanceMethods

Public Instance Methods

destruction_allowed?() click to toggle source

Returns true if this record may be destroyed or adds possible error messages to the errors object otherwise.

# File lib/protective.rb, line 38
def destruction_allowed?
  success = protect_if_methods.all? do |method, message|
    allowed = protect_method_allows_destruction(method)
    errors.add(:base, message) if !allowed && message
    allowed
  end

  throw(:abort) if !success && ActiveRecord::VERSION::MAJOR >= 5
  success
end
protected?() click to toggle source

Returns true if this record cannot be destroyed.

# File lib/protective.rb, line 30
def protected?
  protect_if_methods.keys.any? do |method|
    !protect_method_allows_destruction(method)
  end
end

Private Instance Methods

protect_method_allows_destruction(method) click to toggle source
# File lib/protective.rb, line 51
def protect_method_allows_destruction(method)
  value = send(method)
  if value.respond_to?(:empty?) # ar *_many association
    value.empty?
  else
    value.blank?
  end
end