Skip to content

Commit

Permalink
Refactored @@inline_order to remove if conditional inside the loop.
Browse files Browse the repository at this point in the history
Signed-off-by: Justin French <justin@indent.com.au>
  • Loading branch information
josevalim authored and justinfrench committed Mar 18, 2009
1 parent d8ed2bf commit 9b2657c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 22 deletions.
28 changes: 12 additions & 16 deletions lib/formtastic.rb
Expand Up @@ -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.
#
Expand Down Expand Up @@ -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,
Expand All @@ -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 })
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand Down
12 changes: 6 additions & 6 deletions spec/formtastic_spec.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 9b2657c

Please sign in to comment.