Permalink
Browse files

Refactored @@inline_order to remove if conditional inside the loop.

Signed-off-by: Justin French <justin@indent.com.au>
  • Loading branch information...
1 parent d8ed2bf commit 9b2657c8b22d02b404dcf1041b9afa764e66369d @josevalim josevalim committed with justinfrench Mar 17, 2009
Showing with 18 additions and 22 deletions.
  1. +12 −16 lib/formtastic.rb
  2. +6 −6 spec/formtastic_spec.rb
View
@@ -25,7 +25,6 @@ class SemanticFormBuilder < ActionView::Helpers::FormBuilder
attr_accessor :template
-
# Returns a suitable form input for the given +method+, using the database column information
# and other factors (like the method name) to figure out what you probably want.
#
@@ -67,11 +66,9 @@ class SemanticFormBuilder < ActionView::Helpers::FormBuilder
def input(method, options = {})
raise NoMethodError.new("NoMethodError: form object does not respond to \"#{method}\"") unless @object.respond_to?(method)
-
options[:required] = method_required?(method, options[:required])
options[:label] ||= @object.class.human_attribute_name(method.to_s).send(@@label_str_method)
options[:as] ||= default_input_type(@object, method)
- input_method = "#{options[:as]}_input"
html_class = [
options[:as].to_s,
@@ -82,11 +79,7 @@ def input(method, options = {})
html_id = generate_html_id(method)
list_item_content = @@inline_order.map do |type|
- if type == :input
- send(input_method, method, options)
- else
- send(:"inline_#{type}", method, options)
- end
+ send(:"inline_#{type}_for", method, options)
end.compact.join("\n")
return template.content_tag(:li, list_item_content, { :id => html_id, :class => html_class })
@@ -662,11 +655,18 @@ def boolean_input(method, options)
)
end
- def inline_errors(method, options) #:nodoc:
+ def inline_input_for(method, options)
+ input_method = options.delete(:as)
+ send("#{input_method}_input", method, options)
+ end
+
+ def inline_errors_for(method, options) #:nodoc:
errors = @object.errors.on(method.to_s).to_a
- unless errors.empty?
- send("error_#{@@inline_errors}", errors) if [:sentence, :list].include?(@@inline_errors)
- end
+ send("error_#{@@inline_errors}", errors) if !errors.empty? && [:sentence, :list].include?(@@inline_errors)
+ end
+
+ def inline_hints_for(method, options) #:nodoc:
+ options[:hint].blank? ? '' : template.content_tag(:p, options[:hint], :class => 'inline-hints')
end
def error_sentence(errors) #:nodoc:
@@ -681,10 +681,6 @@ def error_list(errors) #:nodoc:
template.content_tag(:ul, list_elements.join("\n"), :class => 'errors')
end
- def inline_hints(method, options) #:nodoc:
- options[:hint].blank? ? '' : template.content_tag(:p, options[:hint], :class => 'inline-hints')
- end
-
def label_text(method, options) #:nodoc:
[ options[:label], required_or_optional_string(options[:required]) ].join()
end
View
@@ -352,9 +352,9 @@ def custom(arg1, arg2, options = {})
Formtastic::SemanticFormBuilder.inline_order = [:input, :hints, :errors]
semantic_form_for(@new_post) do |builder|
- builder.should_receive(:string_input).once.ordered
- builder.should_receive(:inline_hints).once.ordered
- builder.should_receive(:inline_errors).once.ordered
+ builder.should_receive(:inline_input_for).once.ordered
+ builder.should_receive(:inline_hints_for).once.ordered
+ builder.should_receive(:inline_errors_for).once.ordered
concat(builder.input(:title))
end
end
@@ -363,9 +363,9 @@ def custom(arg1, arg2, options = {})
Formtastic::SemanticFormBuilder.inline_order = [:hints, :input, :errors]
semantic_form_for(@new_post) do |builder|
- builder.should_receive(:inline_hints).once.ordered
- builder.should_receive(:string_input).once.ordered
- builder.should_receive(:inline_errors).once.ordered
+ builder.should_receive(:inline_hints_for).once.ordered
+ builder.should_receive(:inline_input_for).once.ordered
+ builder.should_receive(:inline_errors_for).once.ordered
concat(builder.input(:title))
end
end

0 comments on commit 9b2657c

Please sign in to comment.