From c9d81c92c8a088be6847e1e11544217c70c59f5c Mon Sep 17 00:00:00 2001 From: Joel Moss Date: Mon, 9 Aug 2010 11:51:42 +0100 Subject: [PATCH] Created DynamicErrors --- dynamic_form.gemspec | 1 - lib/active_model/dynamic_errors.rb | 47 ++++++++++++++++++++++++++++++ lib/active_model/locale/en.yml | 4 +++ lib/dynamic_form.rb | 1 + test/dynamic_form_test.rb | 6 ++++ 5 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 lib/active_model/dynamic_errors.rb create mode 100644 lib/active_model/locale/en.yml diff --git a/dynamic_form.gemspec b/dynamic_form.gemspec index b37308c..9db1d02 100644 --- a/dynamic_form.gemspec +++ b/dynamic_form.gemspec @@ -23,7 +23,6 @@ Gem::Specification.new do |s| "README", "Rakefile", "VERSION", - "dynamic-form.gemspec", "dynamic_form.gemspec", "init.rb", "lib/action_view/helpers/dynamic_form.rb", diff --git a/lib/active_model/dynamic_errors.rb b/lib/active_model/dynamic_errors.rb new file mode 100644 index 0000000..e5447d4 --- /dev/null +++ b/lib/active_model/dynamic_errors.rb @@ -0,0 +1,47 @@ +module ActiveModel + class Errors + # Redefine the ActiveModel::Errors::full_messages method: + # Returns all the full error messages in an array. 'Base' messages are handled as usual. + # Non-base messages are prefixed with the attribute name as usual UNLESS + # (1) they begin with '^' in which case the attribute name is omitted. + # E.g. validates_acceptance_of :accepted_terms, :message => '^Please accept the terms of service' + # (2) the message is a proc, in which case the proc is invoked on the model object. + # E.g. validates_presence_of :assessment_answer_option_id, + # :message => Proc.new { |aa| "#{aa.label} (#{aa.group_label}) is required" } + # which gives an error message like: + # Rate (Accuracy) is required + def full_messages + full_messages = [] + + each do |attribute, messages| + messages = Array.wrap(messages) + next if messages.empty? + + if attribute == :base + messages.each {|m| full_messages << m } + else + attr_name = attribute.to_s.gsub('.', '_').humanize + attr_name = @base.class.human_attribute_name(attribute, :default => attr_name) + options = { :default => "%{attribute} %{message}", :attribute => attr_name } + + messages.each do |m| + if m =~ /^\^/ + options[:default] = "%{message}" + full_messages << I18n.t(:"errors.dynamic_format", options.merge(:message => m[1..-1])) + elsif m.is_a? Proc + options[:default] = "%{message}" + full_messages << I18n.t(:"errors.dynamic_format", options.merge(:message => m.call(@base))) + else + full_messages << I18n.t(:"errors.format", options.merge(:message => m)) + end + end + end + end + + full_messages + end + end +end + +require 'active_support/i18n' +I18n.load_path << File.dirname(__FILE__) + '/locale/en.yml' \ No newline at end of file diff --git a/lib/active_model/locale/en.yml b/lib/active_model/locale/en.yml new file mode 100644 index 0000000..7856468 --- /dev/null +++ b/lib/active_model/locale/en.yml @@ -0,0 +1,4 @@ +en: + errors: + # The default format to use in full dynamic error messages. + dynamic_format: "%{message}" \ No newline at end of file diff --git a/lib/dynamic_form.rb b/lib/dynamic_form.rb index 9ee9c5f..f1592e5 100644 --- a/lib/dynamic_form.rb +++ b/lib/dynamic_form.rb @@ -1,3 +1,4 @@ +require "active_model/dynamic_errors" require 'action_view/helpers/dynamic_form' class ActionView::Base diff --git a/test/dynamic_form_test.rb b/test/dynamic_form_test.rb index e6b1f7e..81e2746 100644 --- a/test/dynamic_form_test.rb +++ b/test/dynamic_form_test.rb @@ -369,4 +369,10 @@ def test_default_form_builder_no_instance_variable assert_dom_equal expected, output_buffer end + + def test_error_messages_without_prefixed_attribute_name + error = error_messages_for(@post) + assert_dom_equal %(

1 error prohibited this post from being saved

There were problems with the following fields:

), + error + end end \ No newline at end of file