Module: ActiveModel::ErrorsDetails

Defined in:
lib/active_model/errors_details.rb,
lib/active_model/errors_details/version.rb

Constant Summary collapse

MESSAGE_OPTIONS =
[:message]
VERSION =
"1.3.1"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/active_model/errors_details.rb', line 70

def self.included(base)
  base.class_eval do
    alias_method :initialize_without_details, :initialize
    alias_method :initialize, :initialize_with_details

    alias_method :initialize_dup_without_details, :initialize_dup
    alias_method :initialize_dup, :initialize_dup_with_details

    alias_method :delete_without_details, :delete
    alias_method :delete, :delete_with_details

    alias_method :clear_without_details, :clear
    alias_method :clear, :clear_with_details

    alias_method :add_without_details, :add
    alias_method :add, :add_with_details
  end
end

Instance Method Details

#add_with_details(attribute, message = :invalid, options = {}) ⇒ Object


113
114
115
116
117
118
119
# File 'lib/active_model/errors_details.rb', line 113

def add_with_details(attribute, message = :invalid, options = {})
  message = message.call if message.respond_to?(:call)

  error = {error: message}.merge(options.except(*::ActiveModel::Errors::CALLBACKS_OPTIONS + MESSAGE_OPTIONS))
  details[attribute.to_sym] << error
  add_without_details(attribute, message, options)
end

#clear_with_detailsObject


108
109
110
111
# File 'lib/active_model/errors_details.rb', line 108

def clear_with_details
  details.clear
  clear_without_details
end

#delete_with_details(key) ⇒ Object


103
104
105
106
# File 'lib/active_model/errors_details.rb', line 103

def delete_with_details(key)
  details.delete(key)
  delete_without_details(key)
end

#detailsObject


94
95
96
# File 'lib/active_model/errors_details.rb', line 94

def details
  @details
end

#initialize_dup_with_details(other) ⇒ Object


89
90
91
92
# File 'lib/active_model/errors_details.rb', line 89

def initialize_dup_with_details(other)
  @details = other.details.deep_dup
  initialize_dup_without_details(other)
end

#initialize_with_details(base) ⇒ Object


98
99
100
101
# File 'lib/active_model/errors_details.rb', line 98

def initialize_with_details(base)
  @details = Hash.new { |details, attribute| details[attribute] = [] }
  initialize_without_details(base)
end

#marshal_dumpObject


121
122
123
# File 'lib/active_model/errors_details.rb', line 121

def marshal_dump
  [@base, without_default_proc(@messages), without_default_proc(@details)]
end

#marshal_load(array) ⇒ Object


125
126
127
128
129
# File 'lib/active_model/errors_details.rb', line 125

def marshal_load(array)
  @base, @messages, @details = array
  apply_default_array(@messages)
  apply_default_array(@details)
end