-
-
Save JoshvaR88/b0cb9392ede45505d830 to your computer and use it in GitHub Desktop.
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
module FullErrorMessages | |
extend ActiveSupport::Concern | |
# includes errors on this model as well as any nested models | |
def all_error_messages | |
messages = self.errors.messages.dup | |
messages.each do |column, errors| | |
if self.respond_to?(:"#{column}_attributes=") && (resource = self.send(column)) | |
messages[column] = resource.errors.messages | |
end | |
end | |
end | |
# this is the goodness: | |
#1.9.3-p362 :008 > s.all_full_error_messages | |
# => ["Purchaser can't be blank", "Consumer email can't be blank", "Consumer email is invalid", "Consumer full name can't be blank"] | |
# | |
# this is what we're avoiding: | |
#1.9.3-p362 :009 > s.errors.full_messages | |
# => ["Purchaser can't be blank", "Consumer is invalid"] | |
def all_full_error_messages | |
# this would properly done with full recursion, right now we're limited to a single level | |
formatter = self.errors.method(:full_message) | |
self.all_error_messages.map do |attribute, messages| | |
if messages.is_a? Hash | |
messages.map { |nested_attribute, messages| messages.map { |message| formatter.call("#{attribute} #{nested_attribute}", message) } } | |
else | |
messages.map { |message| formatter.call(attribute, message) } | |
end | |
end.flatten | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment