Skip to content

Commit

Permalink
MultiValueInput should pass an index to build_text_field
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed Jan 13, 2015
1 parent b69fa13 commit 92f9e68
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
9 changes: 6 additions & 3 deletions app/inputs/multi_value_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ def input(wrapper_options)
<ul class="listing">
HTML

collection.each do |value|
collection.each_with_index do |value, index|
markup << <<-HTML
<li class="field-wrapper">
#{build_text_field(value)}
#{build_text_field(value, index)}
</li>
HTML
end
Expand All @@ -25,7 +25,10 @@ def input(wrapper_options)

private

def build_text_field(value)
# Although the 'index' parameter is not used in this implementation it is useful in an
# an overridden version of this method, especially when the field is a complex object and
# the override defines nested fields.
def build_text_field(value, index)

This comment has been minimized.

Copy link
@awead

awead Jan 14, 2015

Contributor

@jcoyne are they overriding this method? It's private, which strikes me as odd.

This comment has been minimized.

Copy link
@jcoyne

jcoyne Jan 14, 2015

Author Member

Ruby has no problem with overriding private methods. Private in ruby means the method can not be called with an explicit receiver.

options = input_html_options.dup

options[:value] = value
Expand Down
15 changes: 15 additions & 0 deletions spec/inputs/multi_value_input_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,19 @@ def [](val)
expect(subject).to have_selector('.form-group.foo_bar.multi_value ul.listing li input.foo_bar')
end
end

describe '#build_text_field' do
let(:foo) { Foo.new }
before { foo.bar = ['bar1', 'bar2'] }
let(:builder) { double("builder", object: foo, object_name: 'foo') }

subject { MultiValueInput.new(builder, :bar, nil, :multi_value, {}) }

it 'renders multi-value' do
expect(subject).to receive(:build_text_field).with('bar1', 0)
expect(subject).to receive(:build_text_field).with('bar2', 1)
expect(subject).to receive(:build_text_field).with('', 2)
subject.input({})
end
end
end

0 comments on commit 92f9e68

Please sign in to comment.