Created
February 17, 2009 00:04
-
-
Save joho/65480 to your computer and use it in GitHub Desktop.
shitty helper method for cleaning up multi model's error messages (rails 1.2 blergh)
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
def tidy_error_messages(model, included_model_names) | |
new_errors = ActiveRecord::Errors.new(nil) | |
model.errors.each do |key, message| | |
new_errors.add(key, message) unless included_model_names.include? key | |
end | |
# re-jig the errors for the included models | |
for included_model_name in included_model_names | |
for included_model in model.send(included_model_name) | |
included_model.errors.each do |key, message| | |
# use a custom error message if the model supports it | |
if included_model.respond_to? :create_error_message | |
included_model.create_error_message(new_errors, key, message) | |
else | |
new_errors.add(included_model_name, "#{key} #{message}") | |
end | |
end | |
end | |
# wipe out the old errors and chuck the new ones in | |
model.errors.clear | |
new_errors.each do |key, message| | |
model.errors.add key, message | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment