2010-01-09 15:05:02 +00:00
|
|
|
module SimpleForm
|
|
|
|
module Components
|
|
|
|
module Errors
|
|
|
|
def error
|
2011-09-04 10:00:20 +00:00
|
|
|
error_text if has_errors?
|
2010-01-09 15:05:02 +00:00
|
|
|
end
|
|
|
|
|
2011-09-03 11:01:41 +00:00
|
|
|
def has_errors?
|
|
|
|
object && object.respond_to?(:errors) && errors.present?
|
|
|
|
end
|
|
|
|
|
2011-09-02 18:08:52 +00:00
|
|
|
protected
|
|
|
|
|
2011-09-04 10:00:20 +00:00
|
|
|
alias :enabled_error :error
|
2011-09-02 18:08:52 +00:00
|
|
|
|
|
|
|
def disabled_error
|
|
|
|
nil
|
2010-01-09 15:05:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def error_text
|
2011-04-27 19:58:59 +00:00
|
|
|
if options[:error_prefix]
|
|
|
|
options[:error_prefix] + " " + errors.send(error_method)
|
|
|
|
else
|
|
|
|
errors.send(error_method)
|
|
|
|
end
|
2010-07-06 09:38:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
def errors_on_attribute
|
2010-08-14 03:19:59 +00:00
|
|
|
object.errors[attribute_name]
|
2010-01-09 15:05:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
end
|
|
|
|
end
|
2010-10-27 17:28:05 +00:00
|
|
|
end
|