Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Something more to work with
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9171 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
- Loading branch information
Showing
3 changed files
with
804 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
module ActiveModel | ||
module DeprecatedErrorMethods | ||
def on(attribute) | ||
ActiveSupport::Deprecation.warn "Errors#on have been deprecated, use Errors#[] instead" | ||
self[attribute] | ||
end | ||
|
||
def on_base | ||
ActiveSupport::Deprecation.warn "Errors#on_base have been deprecated, use Errors#[:base] instead" | ||
on(:base) | ||
end | ||
|
||
def add(attribute, msg = Errors.default_error_messages[:invalid]) | ||
ActiveSupport::Deprecation.warn "Errors#add(attribute, msg) has been deprecated, use Errors#[attribute] << msg instead" | ||
self[attribute] << msg | ||
end | ||
|
||
def add_to_base(msg) | ||
ActiveSupport::Deprecation.warn "Errors#add_to_base(msg) has been deprecated, use Errors#[:base] << msg instead" | ||
self[:base] << msg | ||
end | ||
|
||
def invalid?(attribute) | ||
ActiveSupport::Deprecation.warn "Errors#invalid?(attribute) has been deprecated, use Errors#[attribute].any? instead" | ||
self[attribute].any? | ||
end | ||
|
||
def full_messages | ||
ActiveSupport::Deprecation.warn "Errors#full_messages has been deprecated, use Errors#to_a instead" | ||
to_a | ||
end | ||
|
||
def each_full | ||
ActiveSupport::Deprecation.warn "Errors#each_full has been deprecated, use Errors#to_a.each instead" | ||
to_a.each { |error| yield error } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
module ActiveModel | ||
class Errors < Hash | ||
include DeprecatedErrorMethods | ||
|
||
@@default_error_messages = { | ||
:inclusion => "is not included in the list", | ||
:exclusion => "is reserved", | ||
:invalid => "is invalid", | ||
:confirmation => "doesn't match confirmation", | ||
:accepted => "must be accepted", | ||
:empty => "can't be empty", | ||
:blank => "can't be blank", | ||
:too_long => "is too long (maximum is %d characters)", | ||
:too_short => "is too short (minimum is %d characters)", | ||
:wrong_length => "is the wrong length (should be %d characters)", | ||
:taken => "has already been taken", | ||
:not_a_number => "is not a number", | ||
:greater_than => "must be greater than %d", | ||
:greater_than_or_equal_to => "must be greater than or equal to %d", | ||
:equal_to => "must be equal to %d", | ||
:less_than => "must be less than %d", | ||
:less_than_or_equal_to => "must be less than or equal to %d", | ||
:odd => "must be odd", | ||
:even => "must be even" | ||
} | ||
|
||
# Holds a hash with all the default error messages that can be replaced by your own copy or localizations. | ||
cattr_accessor :default_error_messages | ||
|
||
alias_method :get, :[] | ||
alias_method :set, :[]= | ||
|
||
def [](attribute) | ||
if errors = get(attribute.to_sym) | ||
errors.size == 1 ? errors.first : errors | ||
else | ||
set(attribute.to_sym, []) | ||
end | ||
end | ||
|
||
def []=(attribute, error) | ||
self[attribute.to_sym] << error | ||
end | ||
|
||
def each | ||
each_key do |attribute| | ||
self[attribute].each { |error| yield attribute, error } | ||
end | ||
end | ||
|
||
def size | ||
values.flatten.size | ||
end | ||
|
||
def to_a | ||
inject([]) do |errors_with_attributes, (attribute, errors)| | ||
if error.blank? | ||
errors_with_attributes | ||
else | ||
if attr == :base | ||
errors_with_attributes << error | ||
else | ||
errors_with_attributes << (attribute.to_s.humanize + " " + error) | ||
end | ||
end | ||
end | ||
end | ||
|
||
def to_xml(options={}) | ||
options[:root] ||= "errors" | ||
options[:indent] ||= 2 | ||
options[:builder] ||= Builder::XmlMarkup.new(:indent => options[:indent]) | ||
|
||
options[:builder].instruct! unless options.delete(:skip_instruct) | ||
options[:builder].errors do |e| | ||
to_a.each { |error| e.error(error) } | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.