2013-12-02 16:36:58 -05:00
|
|
|
require 'active_support/core_ext/module/attribute_accessors'
|
2009-06-08 16:33:18 -04:00
|
|
|
require 'active_support/core_ext/enumerable'
|
2004-11-23 20:04:44 -05:00
|
|
|
|
|
|
|
module ActionView
|
2010-06-20 16:20:08 -04:00
|
|
|
# = Active Model Helpers
|
2004-11-23 20:04:44 -05:00
|
|
|
module Helpers
|
2009-07-19 12:28:15 -04:00
|
|
|
module ActiveModelHelper
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
2010-04-10 04:53:05 -04:00
|
|
|
module ActiveModelInstanceTag
|
2009-07-19 11:27:04 -04:00
|
|
|
def object
|
|
|
|
@active_model_object ||= begin
|
|
|
|
object = super
|
|
|
|
object.respond_to?(:to_model) ? object.to_model : object
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-18 03:39:53 -05:00
|
|
|
def content_tag(*)
|
|
|
|
error_wrapping(super)
|
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2010-06-24 16:36:54 -04:00
|
|
|
def tag(type, options, *)
|
|
|
|
tag_generate_errors?(options) ? error_wrapping(super) : super
|
|
|
|
end
|
|
|
|
|
2009-06-17 22:37:08 -04:00
|
|
|
def error_wrapping(html_tag)
|
2010-06-24 16:36:54 -04:00
|
|
|
if object_has_errors?
|
2009-06-17 22:37:08 -04:00
|
|
|
Base.field_error_proc.call(html_tag, self)
|
2006-12-06 14:15:24 -05:00
|
|
|
else
|
2009-06-17 22:37:08 -04:00
|
|
|
html_tag
|
2006-12-06 14:15:24 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-03-15 14:11:46 -04:00
|
|
|
def error_message
|
|
|
|
object.errors[@method_name]
|
|
|
|
end
|
2010-06-24 16:36:54 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def object_has_errors?
|
2012-02-07 17:10:14 -05:00
|
|
|
object.respond_to?(:errors) && object.errors.respond_to?(:[]) && error_message.present?
|
2010-06-24 16:36:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def tag_generate_errors?(options)
|
|
|
|
options['type'] != 'hidden'
|
|
|
|
end
|
2010-04-10 04:53:05 -04:00
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
end
|