2010-01-09 10:05:02 -05:00
|
|
|
module SimpleForm
|
|
|
|
module Components
|
|
|
|
module Errors
|
2014-03-11 18:41:25 -04:00
|
|
|
def error(wrapper_options = nil)
|
2011-09-04 06:00:20 -04:00
|
|
|
error_text if has_errors?
|
2010-01-09 10:05:02 -05:00
|
|
|
end
|
|
|
|
|
2014-04-03 11:08:29 -04:00
|
|
|
def full_error(wrapper_options = nil)
|
2014-04-03 17:00:07 -04:00
|
|
|
full_error_text if options[:error] != false && has_errors?
|
2014-04-03 11:08:29 -04:00
|
|
|
end
|
|
|
|
|
2011-09-03 07:01:41 -04:00
|
|
|
def has_errors?
|
2014-04-03 17:18:00 -04:00
|
|
|
object && object.respond_to?(:errors) && errors.present?
|
2011-09-03 07:01:41 -04:00
|
|
|
end
|
|
|
|
|
2012-01-26 14:22:05 -05:00
|
|
|
protected
|
2011-09-02 14:08:52 -04:00
|
|
|
|
2010-01-09 10:05:02 -05:00
|
|
|
def error_text
|
2014-04-03 17:24:17 -04:00
|
|
|
text = has_custom_error? ? options[:error] : errors.send(error_method)
|
2014-04-03 17:18:00 -04:00
|
|
|
|
2014-11-20 12:22:59 -05:00
|
|
|
"#{html_escape(options[:error_prefix])} #{html_escape(text)}".lstrip.html_safe
|
2010-07-06 05:38:35 -04:00
|
|
|
end
|
|
|
|
|
2014-04-03 11:08:29 -04:00
|
|
|
def full_error_text
|
2014-11-20 12:22:59 -05:00
|
|
|
has_custom_error? ? options[:error] : full_errors.send(error_method)
|
2014-04-03 11:08:29 -04:00
|
|
|
end
|
|
|
|
|
2010-07-06 05:38:35 -04:00
|
|
|
def error_method
|
|
|
|
options[:error_method] || SimpleForm.error_method
|
2010-01-09 10:05:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def errors
|
|
|
|
@errors ||= (errors_on_attribute + errors_on_association).compact
|
|
|
|
end
|
|
|
|
|
2014-04-03 11:08:29 -04:00
|
|
|
def full_errors
|
|
|
|
@full_errors ||= (full_errors_on_attribute + full_errors_on_association).compact
|
|
|
|
end
|
|
|
|
|
2010-01-09 10:05:02 -05:00
|
|
|
def errors_on_attribute
|
2014-04-03 17:18:00 -04:00
|
|
|
object.errors[attribute_name]
|
2010-01-09 10:05:02 -05:00
|
|
|
end
|
|
|
|
|
2014-04-03 11:08:29 -04:00
|
|
|
def full_errors_on_attribute
|
2014-04-03 17:18:00 -04:00
|
|
|
object.errors.full_messages_for(attribute_name)
|
2014-04-03 11:08:29 -04:00
|
|
|
end
|
|
|
|
|
2010-01-09 10:05:02 -05:00
|
|
|
def errors_on_association
|
2010-07-06 05:38:35 -04:00
|
|
|
reflection ? object.errors[reflection.name] : []
|
2010-01-09 10:05:02 -05:00
|
|
|
end
|
2014-04-03 11:08:29 -04:00
|
|
|
|
|
|
|
def full_errors_on_association
|
2014-09-20 21:51:37 -04:00
|
|
|
reflection ? object.errors.full_messages_for(reflection.name) : []
|
2014-04-03 11:08:29 -04:00
|
|
|
end
|
2014-04-03 16:31:54 -04:00
|
|
|
|
2014-04-03 17:24:17 -04:00
|
|
|
def has_custom_error?
|
|
|
|
options[:error].is_a?(String)
|
2014-04-03 16:31:54 -04:00
|
|
|
end
|
2010-01-09 10:05:02 -05:00
|
|
|
end
|
|
|
|
end
|
2010-10-27 13:28:05 -04:00
|
|
|
end
|