Module: HasErrors

Overview

Helper functions for dealing with errors and objects that have child objects with errors

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#conflictObject

Returns the value of attribute conflict.



7
8
9
# File 'lib/has_errors.rb', line 7

def conflict
  @conflict
end

#errorsObject (readonly)

Returns the value of attribute errors.



6
7
8
# File 'lib/has_errors.rb', line 6

def errors
  @errors
end

#forbiddenObject

Returns the value of attribute forbidden.



7
8
9
# File 'lib/has_errors.rb', line 7

def forbidden
  @forbidden
end

#not_foundObject

Returns the value of attribute not_found.



7
8
9
# File 'lib/has_errors.rb', line 7

def not_found
  @not_found
end

Instance Method Details

#add_error(msg) ⇒ Object



29
30
31
# File 'lib/has_errors.rb', line 29

def add_error(msg)
  errors.add(:base, msg) unless errors[:base].include?(msg)
end

#add_errors_from(obj) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/has_errors.rb', line 33

def add_errors_from(obj)
  return if obj.blank?

  return add_error(obj.message) if obj.is_a?(StandardError)

  obj.errors.full_messages.each { |msg| add_error(msg) }
end

#rollback_from_errors!(obj) ⇒ Object

Raises:

  • (ActiveRecord::Rollback)


24
25
26
27
# File 'lib/has_errors.rb', line 24

def rollback_from_errors!(obj)
  add_errors_from(obj)
  raise ActiveRecord::Rollback.new
end

#rollback_with!(obj, error) ⇒ Object



19
20
21
22
# File 'lib/has_errors.rb', line 19

def rollback_with!(obj, error)
  obj.errors.add(:base, error)
  rollback_from_errors!(obj)
end

#validate_child(obj) ⇒ Object



13
14
15
16
17
# File 'lib/has_errors.rb', line 13

def validate_child(obj)
  return true if obj.valid?
  add_errors_from(obj)
  false
end