提交 1727f433 编写于 作者: F Francesco Rodriguez

add documentation and examples to ActiveModel::Errors [ci skip]

上级 bc818e4b
...@@ -99,6 +99,7 @@ def clear ...@@ -99,6 +99,7 @@ def clear
def include?(error) def include?(error)
(v = messages[error]) && v.any? (v = messages[error]) && v.any?
end end
# aliases include?
alias :has_key? :include? alias :has_key? :include?
# Get messages for +key+. # Get messages for +key+.
...@@ -218,6 +219,7 @@ def count ...@@ -218,6 +219,7 @@ def count
def empty? def empty?
all? { |k, v| v && v.empty? && !v.is_a?(String) } all? { |k, v| v && v.empty? && !v.is_a?(String) }
end end
# aliases empty?
alias_method :blank?, :empty? alias_method :blank?, :empty?
# Returns an xml formatted representation of the Errors hash. # Returns an xml formatted representation of the Errors hash.
...@@ -235,10 +237,9 @@ def to_xml(options={}) ...@@ -235,10 +237,9 @@ def to_xml(options={})
to_a.to_xml({ :root => "errors", :skip_types => true }.merge!(options)) to_a.to_xml({ :root => "errors", :skip_types => true }.merge!(options))
end end
# Returns an Hash that can be used as the JSON representation for this # Returns a Hash that can be used as the JSON representation for this
# object. Also, You can pass the <tt>:full_messages</tt> option. This # object. You can pass the <tt>:full_messages</tt> option. This determines
# determines if the json object should contain full messages or not (false # if the json object should contain full messages or not (false by default).
# by default).
# #
# person.as_json # => { :name => ["can not be nil"] } # person.as_json # => { :name => ["can not be nil"] }
# person.as_json(full_messages: true) # => { :name => ["name can not be nil"] } # person.as_json(full_messages: true) # => { :name => ["name can not be nil"] }
...@@ -246,6 +247,11 @@ def as_json(options=nil) ...@@ -246,6 +247,11 @@ def as_json(options=nil)
to_hash(options && options[:full_messages]) to_hash(options && options[:full_messages])
end end
# Returns a Hash of attributes with their error messages. If +full_messages+
# is +true+, it will contain full messages (see +full_message+).
#
# person.to_hash # => { :name => ["can not be nil"] }
# person.to_hash(true) # => { :name => ["name can not be nil"] }
def to_hash(full_messages = false) def to_hash(full_messages = false)
if full_messages if full_messages
messages = {} messages = {}
...@@ -262,6 +268,14 @@ def to_hash(full_messages = false) ...@@ -262,6 +268,14 @@ def to_hash(full_messages = false)
# can be added to the same +attribute+. If no +message+ is supplied, # can be added to the same +attribute+. If no +message+ is supplied,
# <tt>:invalid</tt> is assumed. # <tt>:invalid</tt> is assumed.
# #
# person.errors.add(:name)
# # => ["is invalid"]
# person.errors.add(:name, 'must be implemented')
# # => ["is invalid", "must be implemented"]
#
# person.errors.messages
# # => { :name => ["must be implemented", "is invalid"] }
#
# If +message+ is a symbol, it will be translated using the appropriate # If +message+ is a symbol, it will be translated using the appropriate
# scope (see +generate_message+). # scope (see +generate_message+).
# #
...@@ -278,6 +292,10 @@ def add(attribute, message = nil, options = {}) ...@@ -278,6 +292,10 @@ def add(attribute, message = nil, options = {})
# Will add an error message to each of the attributes in +attributes+ # Will add an error message to each of the attributes in +attributes+
# that is empty. # that is empty.
#
# person.errors.add_on_empty(:name)
# person.errors.messages
# # => { :name => ["can't be empty"] }
def add_on_empty(attributes, options = {}) def add_on_empty(attributes, options = {})
[attributes].flatten.each do |attribute| [attributes].flatten.each do |attribute|
value = @base.send(:read_attribute_for_validation, attribute) value = @base.send(:read_attribute_for_validation, attribute)
...@@ -288,6 +306,10 @@ def add_on_empty(attributes, options = {}) ...@@ -288,6 +306,10 @@ def add_on_empty(attributes, options = {})
# Will add an error message to each of the attributes in +attributes+ that # Will add an error message to each of the attributes in +attributes+ that
# is blank (using Object#blank?). # is blank (using Object#blank?).
#
# person.errors.add_on_blank(:name)
# person.errors.messages
# # => { :name => ["can't be blank"] }
def add_on_blank(attributes, options = {}) def add_on_blank(attributes, options = {})
[attributes].flatten.each do |attribute| [attributes].flatten.each do |attribute|
value = @base.send(:read_attribute_for_validation, attribute) value = @base.send(:read_attribute_for_validation, attribute)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册