def build_criteria(data_cols, i)
filterHTML = ''
typeObjects = ''
data_cols.each do |d|
typeObjects += "<option value='#{d}'>#{d}</option>"
end
filterHTML += "<div id='additionalcriteria#{i}'>"
filterHTML += "<select name='dataType#{i}'>#{typeObjects}</select>"
filterHTML += select_tag ("operator#{i}", "
<option value='<'>is less than</option>
<option value='<='>is less than or equal to</option>
<option value='=='>is equal to</option>
<option value='>='>is greater than or equal to</option>")
filterHTML += text_field_tag("value#{i}")
filterHTML += "</div>"
end
Refactorings
No refactoring yet !
Josh N. Abbott
April 23, 2009, April 23, 2009 21:34, permalink
Here's what I came up with quickly. You may need to tweak it a bit, but it should give you an idea of what I'm going for.
Hope this helps!
-- Josh
def build_criteria(data_cols, i)
OPERATOR_OPTIONS = [
['<', 'is less than'],
['<=', 'is less than or equal to'],
['==', 'is equal to'],
['>=', 'is greater than or equal to']
]
returning filter_html = String.new do |html|
html += select_tag("dataType#{i}", options_for_select(data_cols))
html += select_tag("operator#{i}", options_for_select(OPERATOR_OPTIONS))
html += text_field_tag("value#{i}")
end
content_tag(:div, filter_html, :id => "additional_criteria#{i}")
end
Danny Peck
April 23, 2009, April 23, 2009 22:29, permalink
Looks nice. BTW, the "all caps" OPERATOR_OPTIONS didn't jive with Ruby because it thought it was a constant. Also, within the operator_options, the values are reversed. The first '' will be what appears in the dropdown to the user. The second '' is the actual value.
Any help is appreciated.