Module: HasErrors
- Included in:
- BookmarkManager, EmailUpdater, Embedding, NewPostResult, PostActionResult, PostCreator, TopicCreator
- Defined in:
- lib/has_errors.rb
Overview
Helper functions for dealing with errors and objects that have child objects with errors
Instance Attribute Summary collapse
-
#conflict ⇒ Object
Returns the value of attribute conflict.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#forbidden ⇒ Object
Returns the value of attribute forbidden.
-
#not_found ⇒ Object
Returns the value of attribute not_found.
Instance Method Summary collapse
- #add_error(msg) ⇒ Object
- #add_errors_from(obj) ⇒ Object
- #rollback_from_errors!(obj) ⇒ Object
- #rollback_with!(obj, error) ⇒ Object
- #validate_child(obj) ⇒ Object
Instance Attribute Details
#conflict ⇒ Object
Returns the value of attribute conflict.
7 8 9 |
# File 'lib/has_errors.rb', line 7 def conflict @conflict end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
6 7 8 |
# File 'lib/has_errors.rb', line 6 def errors @errors end |
#forbidden ⇒ Object
Returns the value of attribute forbidden.
7 8 9 |
# File 'lib/has_errors.rb', line 7 def forbidden @forbidden end |
#not_found ⇒ Object
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.) if obj.is_a?(StandardError) obj.errors..each { |msg| add_error(msg) } end |
#rollback_from_errors!(obj) ⇒ Object
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 |