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