Skip to content

Commit

Permalink
Merge 84bc9f9 into 4a885a6
Browse files Browse the repository at this point in the history
  • Loading branch information
davydovanton committed Mar 7, 2016
2 parents 4a885a6 + 84bc9f9 commit 7b76061
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
37 changes: 35 additions & 2 deletions lib/hanami/helpers/form_helper/form_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -751,11 +751,44 @@ def password_field(name, attributes = {})
# # <option value="it" selected="selected">Italy</option>
# # <option value="us">United States</option>
# # </select>
#
# @example Include blank option
# <%=
# # ...
# values = Hash['it' => 'Italy', 'us' => 'United States']
# select :stores, values, options: {include_blank: true}
# %>
#
# # Output:
# # <select name="book[store]" id="book-store">
# # <option></option>
# # <option value="it">Italy</option>
# # <option value="us">United States</option>
# # </select>
#
# @example Prompt option
# <%=
# # ...
# values = Hash['it' => 'Italy', 'us' => 'United States']
# select :stores, values, options: {prompt: 'Select a City'}
# %>
#
# # Output:
# # <select name="book[store]" id="book-store">
# # <option>Select a City</option>
# # <option value="it">Italy</option>
# # <option value="us">United States</option>
# # </select>
def select(name, values, attributes = {})
options = attributes.delete(:options) || {}
attributes = { name: _input_name(name), id: _input_id(name) }.merge(attributes)
options = attributes.delete(:options) { {} }
attributes = { name: _input_name(name), id: _input_id(name) }.merge(attributes)
blank_value = options.delete(:include_blank)
prompt_value = options.delete(:prompt)

super(attributes) do
option if blank_value
option(prompt_value) if prompt_value

values.each do |value, content|
if _value(name) == value
option(content, {value: value, selected: SELECTED}.merge(options))
Expand Down
16 changes: 16 additions & 0 deletions test/form_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1175,6 +1175,22 @@
actual.must_include %(<select name="book[store]" id="book-store">\n<option value="it" class="form-option">Italy</option>\n<option value="us" class="form-option">United States</option>\n</select>)
end

it "allows include_blank option" do
actual = view.form_for(:book, action) do
select :store, values, options: { include_blank: true }
end.to_s

actual.must_include %(<select name="book[store]" id="book-store">\n<option></option>\n<option value="it">Italy</option>\n<option value="us">United States</option>\n</select>)
end

it "allows prompt option" do
actual = view.form_for(:book, action) do
select :store, values, options: { prompt: 'Select City' }
end.to_s

actual.must_include %(<select name="book[store]" id="book-store">\n<option>Select City</option>\n<option value="it">Italy</option>\n<option value="us">United States</option>\n</select>)
end

describe "with filled params" do
let(:params) { Hash[book: { store: val }] }
let(:val) { 'it' }
Expand Down

0 comments on commit 7b76061

Please sign in to comment.