diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2009-06-17 19:37:08 -0700 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2009-06-17 19:37:56 -0700 |
commit | 5267addd4f986c89df3d31f35e046abc3b1fbe26 (patch) | |
tree | fa0e1df3be7046134c5cf9053096b08620bc5ddc /actionpack | |
parent | fbdf706fffbfb17731a1f459203d242414ef5086 (diff) | |
download | rails-5267addd4f986c89df3d31f35e046abc3b1fbe26.tar.gz rails-5267addd4f986c89df3d31f35e046abc3b1fbe26.tar.bz2 rails-5267addd4f986c89df3d31f35e046abc3b1fbe26.zip |
Use errors[field] instead of errors.on(field)
Diffstat (limited to 'actionpack')
-rw-r--r-- | actionpack/lib/action_view/helpers/active_record_helper.rb | 59 | ||||
-rw-r--r-- | actionpack/test/template/active_record_helper_test.rb | 14 | ||||
-rw-r--r-- | actionpack/test/template/form_helper_test.rb | 4 |
3 files changed, 20 insertions, 57 deletions
diff --git a/actionpack/lib/action_view/helpers/active_record_helper.rb b/actionpack/lib/action_view/helpers/active_record_helper.rb index 8b70200654..75cc651968 100644 --- a/actionpack/lib/action_view/helpers/active_record_helper.rb +++ b/actionpack/lib/action_view/helpers/active_record_helper.rb @@ -122,9 +122,9 @@ module ActionView options.reverse_merge!(:prepend_text => '', :append_text => '', :css_class => 'formError') if (obj = (object.respond_to?(:errors) ? object : instance_variable_get("@#{object}"))) && - (errors = obj.errors.on(method)) + (errors = obj.errors[method]) content_tag("div", - "#{options[:prepend_text]}#{ERB::Util.html_escape(errors.is_a?(Array) ? errors.first : errors)}#{options[:append_text]}", + "#{options[:prepend_text]}#{ERB::Util.html_escape(errors.first)}#{options[:append_text]}", :class => options[:css_class] ) else @@ -247,59 +247,22 @@ module ActionView end end - alias_method :tag_without_error_wrapping, :tag - def tag(name, options) - if object.respond_to?(:errors) && object.errors.respond_to?(:on) - error_wrapping(tag_without_error_wrapping(name, options), object.errors.on(@method_name)) - else - tag_without_error_wrapping(name, options) - end - end - - alias_method :content_tag_without_error_wrapping, :content_tag - def content_tag(name, value, options) - if object.respond_to?(:errors) && object.errors.respond_to?(:on) - error_wrapping(content_tag_without_error_wrapping(name, value, options), object.errors.on(@method_name)) - else - content_tag_without_error_wrapping(name, value, options) - end - end - - alias_method :to_date_select_tag_without_error_wrapping, :to_date_select_tag - def to_date_select_tag(options = {}, html_options = {}) - if object.respond_to?(:errors) && object.errors.respond_to?(:on) - error_wrapping(to_date_select_tag_without_error_wrapping(options, html_options), object.errors.on(@method_name)) - else - to_date_select_tag_without_error_wrapping(options, html_options) - end - end - - alias_method :to_datetime_select_tag_without_error_wrapping, :to_datetime_select_tag - def to_datetime_select_tag(options = {}, html_options = {}) - if object.respond_to?(:errors) && object.errors.respond_to?(:on) - error_wrapping(to_datetime_select_tag_without_error_wrapping(options, html_options), object.errors.on(@method_name)) - else - to_datetime_select_tag_without_error_wrapping(options, html_options) + %w(tag content_tag to_date_select_tag to_datetime_select_tag to_time_select_tag).each do |meth| + without = "#{meth}_without_error_wrapping" + define_method "#{meth}_with_error_wrapping" do |*args| + error_wrapping(send(without, *args)) end + alias_method_chain meth, :error_wrapping end - alias_method :to_time_select_tag_without_error_wrapping, :to_time_select_tag - def to_time_select_tag(options = {}, html_options = {}) - if object.respond_to?(:errors) && object.errors.respond_to?(:on) - error_wrapping(to_time_select_tag_without_error_wrapping(options, html_options), object.errors.on(@method_name)) + def error_wrapping(html_tag) + if object.respond_to?(:errors) && object.errors.respond_to?(:full_messages) && object.errors[@method_name].any? + Base.field_error_proc.call(html_tag, self) else - to_time_select_tag_without_error_wrapping(options, html_options) + html_tag end end - def error_wrapping(html_tag, has_error) - has_error ? Base.field_error_proc.call(html_tag, self) : html_tag - end - - def error_message - object.errors.on(@method_name) - end - def column_type object.send(:column_for_attribute, @method_name).type end diff --git a/actionpack/test/template/active_record_helper_test.rb b/actionpack/test/template/active_record_helper_test.rb index b4b8cbe074..4691049a41 100644 --- a/actionpack/test/template/active_record_helper_test.rb +++ b/actionpack/test/template/active_record_helper_test.rb @@ -33,8 +33,8 @@ class ActiveRecordHelperTest < ActionView::TestCase ["Author name can't be <em>empty</em>"] end - def on(field) - "can't be <em>empty</em>" + def [](field) + ["can't be <em>empty</em>"] end end @@ -47,14 +47,14 @@ class ActiveRecordHelperTest < ActionView::TestCase @post = Post.new def @post.errors Class.new { - def on(field) + def [](field) case field.to_s when "author_name" - "can't be empty" + ["can't be empty"] when "body" - true + ['foo'] else - false + [] end end def empty?() false end @@ -85,7 +85,7 @@ class ActiveRecordHelperTest < ActionView::TestCase @user = User.new def @user.errors Class.new { - def on(field) field == "email" end + def [](field) field == "email" ? ['nonempty'] : [] end def empty?() false end def count() 1 end def full_messages() [ "User email can't be empty" ] end diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb index 947668ccc6..f8215132e3 100644 --- a/actionpack/test/template/form_helper_test.rb +++ b/actionpack/test/template/form_helper_test.rb @@ -50,7 +50,7 @@ class FormHelperTest < ActionView::TestCase @comment = Comment.new def @post.errors() Class.new{ - def on(field); "can't be empty" if field == "author_name"; end + def [](field); field == "author_name" ? ["can't be empty"] : [] end def empty?() false end def count() 1 end def full_messages() [ "Author name can't be empty" ] end @@ -1193,4 +1193,4 @@ class FormHelperTest < ActionView::TestCase def protect_against_forgery? false end -end
\ No newline at end of file +end |