Skip to content

Commit

Permalink
Add an optional ":join" parameter to multiparameter_field
Browse files Browse the repository at this point in the history
If specified, the fields will be separated by the supplied value.
  • Loading branch information
rspeicher committed Mar 15, 2011
1 parent bc135d5 commit 5029371
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
10 changes: 5 additions & 5 deletions lib/meta_search/helpers/form_builder.rb
Expand Up @@ -41,20 +41,20 @@ def self.included(base)
def multiparameter_field(method, *args)
defaults = has_multiparameter_defaults?(args) ? args.pop : {}
raise ArgumentError, "No multiparameter fields specified" if args.blank?
html = ''.html_safe
join_str = defaults.delete(:join) || ''
html = []
args.each_with_index do |field, index|
type = field.delete(:field_type) || raise(ArgumentError, "No :field_type specified.")
cast = field.delete(:type_cast) || ''
opts = defaults.merge(field)
html.safe_concat(
html <<
@template.send(
type.to_s,
@object_name,
(method.to_s + "(#{index + 1}#{cast})"),
objectify_options(opts))
)
end
html
html.join(join_str).html_safe
end

# Behaves almost exactly like the select method, but instead of generating a select tag,
Expand Down Expand Up @@ -163,4 +163,4 @@ def has_multiparameter_defaults?(args)
end
end
end
end
end
16 changes: 14 additions & 2 deletions test/test_view_helpers.rb
Expand Up @@ -81,7 +81,7 @@ def setup
end
end

context "A form using mutiparameter_field with default size option" do
context "A form using multiparameter_field with default size option" do
setup do
@s = Developer.search
form_for @s do |f|
Expand All @@ -99,6 +99,18 @@ def setup
'size="10" type="text" />',
html
end

should "join multiple fields together using a specified string" do
html = @f.multiparameter_field :salary_in,
{:field_type => :text_field, :type_cast => 'i'},
{:field_type => :text_field, :type_cast => 'i'}, :size => 10, :join => " and "
assert_dom_equal '<input id="search_salary_in(1i)" name="search[salary_in(1i)]" ' +
'size="10" type="text" />' +
' and ' +
'<input id="search_salary_in(2i)" name="search[salary_in(2i)]" ' +
'size="10" type="text" />',
html
end
end

context "A form using checks with three choices" do
Expand Down Expand Up @@ -365,4 +377,4 @@ def setup
end
end
end
end
end

0 comments on commit 5029371

Please sign in to comment.