Module: ApiableModelErrors
- Defined in:
- lib/apiable_model_errors.rb
Class Method Summary collapse
Instance Method Summary collapse
- #add_with_api_errors(attribute, message = :invalid, options = {}) ⇒ Object
- #clear_with_api_errors ⇒ Object
- #delete_with_api_errors(key) ⇒ Object
- #errors_for_api ⇒ Object
- #has_message?(attribute, message, options = nil) ⇒ Boolean
- #to_api_hash ⇒ Object
Class Method Details
.included(base) ⇒ Object
5 6 7 8 9 |
# File 'lib/apiable_model_errors.rb', line 5 def self.included(base) base.alias_method_chain :add, :api_errors base.alias_method_chain :clear, :api_errors base.alias_method_chain :delete, :api_errors end |
Instance Method Details
#add_with_api_errors(attribute, message = :invalid, options = {}) ⇒ Object
45 46 47 48 49 50 |
# File 'lib/apiable_model_errors.rb', line 45 def add_with_api_errors(attribute, = :invalid, = {}) add_without_api_errors(attribute, , ).tap do errors_for_api[attribute] ||= [] errors_for_api[attribute] << [, || {}] end end |
#clear_with_api_errors ⇒ Object
33 34 35 36 37 |
# File 'lib/apiable_model_errors.rb', line 33 def clear_with_api_errors clear_without_api_errors.tap do @errors_for_api = {} end end |
#delete_with_api_errors(key) ⇒ Object
39 40 41 42 43 |
# File 'lib/apiable_model_errors.rb', line 39 def delete_with_api_errors(key) delete_without_api_errors(key).tap do errors_for_api.delete(key) end end |
#errors_for_api ⇒ Object
29 30 31 |
# File 'lib/apiable_model_errors.rb', line 29 def errors_for_api @errors_for_api ||= {} end |
#has_message?(attribute, message, options = nil) ⇒ Boolean
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/apiable_model_errors.rb', line 52 def (attribute, , = nil) if errors = errors_for_api[attribute] if error = errors.select { |e| e[0] == }.first if .nil? true # No options specified, if it exists, it's here elsif == error[1] true # The options match those on the subejct else false # The options selected don't match those on the error end else false # No errors of this type end else false # No errors for this attribute end end |
#to_api_hash ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/apiable_model_errors.rb', line 11 def to_api_hash errors_for_api.each_with_object({}) do |(attribute, errors), hash| hash[attribute] = [] errors.each_with_index do |(, ), index| if .is_a?(String) error = {:message => } else error = { :code => , :message => [attribute][index], :options => } end hash[attribute] << error end end end |