From 27ba278b6cbfcce7889c481c519043d631c0fe5c Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Wed, 27 Oct 2010 15:28:05 -0200 Subject: [PATCH] Refactor has errors in both error component and error notification --- lib/simple_form.rb | 1 + lib/simple_form/components/errors.rb | 8 +++----- lib/simple_form/error_notification.rb | 7 ++----- lib/simple_form/has_errors.rb | 14 ++++++++++++++ lib/simple_form/inputs/numeric_input.rb | 2 -- 5 files changed, 20 insertions(+), 12 deletions(-) create mode 100644 lib/simple_form/has_errors.rb diff --git a/lib/simple_form.rb b/lib/simple_form.rb index ffc3f3fc..709d3436 100644 --- a/lib/simple_form.rb +++ b/lib/simple_form.rb @@ -6,6 +6,7 @@ module SimpleForm autoload :Components, 'simple_form/components' autoload :ErrorNotification, 'simple_form/error_notification' autoload :FormBuilder, 'simple_form/form_builder' + autoload :HasErrors, 'simple_form/has_errors' autoload :I18nCache, 'simple_form/i18n_cache' autoload :Inputs, 'simple_form/inputs' autoload :MapType, 'simple_form/map_type' diff --git a/lib/simple_form/components/errors.rb b/lib/simple_form/components/errors.rb index ed32d6e6..33c82177 100644 --- a/lib/simple_form/components/errors.rb +++ b/lib/simple_form/components/errors.rb @@ -1,6 +1,8 @@ module SimpleForm module Components module Errors + include SimpleForm::HasErrors + def error template.content_tag(error_tag, error_text, error_html_options) if has_errors? end @@ -23,10 +25,6 @@ module SimpleForm protected - def has_errors? - object && object.respond_to?(:errors) && errors.present? - end - def errors @errors ||= (errors_on_attribute + errors_on_association).compact end @@ -40,4 +38,4 @@ module SimpleForm end end end -end \ No newline at end of file +end diff --git a/lib/simple_form/error_notification.rb b/lib/simple_form/error_notification.rb index bd255d08..5a9869e1 100644 --- a/lib/simple_form/error_notification.rb +++ b/lib/simple_form/error_notification.rb @@ -1,6 +1,7 @@ module SimpleForm class ErrorNotification delegate :object, :object_name, :template, :to => :@builder + include SimpleForm::HasErrors def initialize(builder, options) @builder = builder @@ -24,10 +25,6 @@ module SimpleForm SimpleForm.error_notification_tag end - def has_errors? - object && object.respond_to?(:errors) && object.errors.present? - end - def html_options @options[:class] = "error_notification #{@options[:class]}".strip @options @@ -41,4 +38,4 @@ module SimpleForm I18n.t(lookups.shift, :scope => :"simple_form.error_notification", :default => lookups) end end -end \ No newline at end of file +end diff --git a/lib/simple_form/has_errors.rb b/lib/simple_form/has_errors.rb new file mode 100644 index 00000000..9693ab61 --- /dev/null +++ b/lib/simple_form/has_errors.rb @@ -0,0 +1,14 @@ +module SimpleForm + module HasErrors + + protected + + def errors + object.errors + end + + def has_errors? + object && object.respond_to?(:errors) && errors.present? + end + end +end diff --git a/lib/simple_form/inputs/numeric_input.rb b/lib/simple_form/inputs/numeric_input.rb index eb1462d5..6dc8989e 100644 --- a/lib/simple_form/inputs/numeric_input.rb +++ b/lib/simple_form/inputs/numeric_input.rb @@ -11,9 +11,7 @@ module SimpleForm input_options[:size] ||= SimpleForm.default_input_size input_options[:step] ||= 1 if integer? input_options[:placeholder] ||= placeholder if has_placeholder? - infer_attributes_from_validations(input_options) - input_options end