2007-01-28 02:16:55 -05:00
|
|
|
require 'action_view/helpers/form_helper'
|
2009-05-14 20:42:20 -04:00
|
|
|
require 'active_support/core_ext/class/attribute_accessors'
|
2009-06-08 16:33:18 -04:00
|
|
|
require 'active_support/core_ext/enumerable'
|
2010-03-27 15:39:28 -04:00
|
|
|
require 'active_support/core_ext/object/blank'
|
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
|
|
|
|
|
2010-06-24 16:36:54 -04:00
|
|
|
%w(content_tag to_date_select_tag to_datetime_select_tag to_time_select_tag).each do |meth|
|
2010-05-19 15:37:41 -04:00
|
|
|
module_eval "def #{meth}(*) error_wrapping(super) end", __FILE__, __LINE__
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
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?
|
|
|
|
object.respond_to?(:errors) && object.errors.respond_to?(:full_messages) && error_message.any?
|
|
|
|
end
|
|
|
|
|
|
|
|
def tag_generate_errors?(options)
|
|
|
|
options['type'] != 'hidden'
|
|
|
|
end
|
2010-04-10 04:53:05 -04:00
|
|
|
end
|
2010-03-15 14:11:46 -04:00
|
|
|
|
2009-07-19 11:27:04 -04:00
|
|
|
class InstanceTag
|
2010-04-10 04:53:05 -04:00
|
|
|
include ActiveModelInstanceTag
|
2009-07-19 11:27:04 -04:00
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
end
|