Skip to content

Commit

Permalink
Merge 9c992f7 into 83e5030
Browse files Browse the repository at this point in the history
  • Loading branch information
kasumionlyway committed Sep 9, 2016
2 parents 83e5030 + 9c992f7 commit 1453ad3
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 16 deletions.
3 changes: 2 additions & 1 deletion lib/hanami/helpers/form_helper/form_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1026,7 +1026,8 @@ def _attributes_for_check_box(name, attributes)

value = _value(name)
attributes[:checked] = CHECKED if !value.nil? &&
(value == attributes[:value] || value.include?(attributes[:value]))
(value == attributes[:value] ||
value.is_a?(Array) && value.include?(attributes[:value]))

attributes
end
Expand Down
90 changes: 75 additions & 15 deletions test/form_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,34 @@
actual.must_include %(<input type="checkbox" name="book[languages][]" id="book-languages" value="italian" checked="checked">\n<input type="checkbox" name="book[languages][]" id="book-languages" value="english">)
end
end

describe 'checked_value is boolean' do
let(:params) { Hash[book: { free_shipping: 'true' }] }

it "renders with 'checked' attribute" do
actual = view.form_for(:book, action) do
check_box :free_shipping, checked_value: true
end.to_s

actual.must_include %(<input type="checkbox" name="book[free_shipping]" id="book-free-shipping" value="true" checked="checked">)
end
end
end

describe 'automatic values' do
describe 'checkbox' do
describe 'value boolean, helper boolean, values differ' do
let(:values) { Hash[book: OpenStruct.new(free_shipping: false)] }

it 'renders' do
actual = view.form_for(:book, action, values: values) do
check_box :free_shipping, checked_value: true
end.to_s

actual.must_include %(<input type="checkbox" name="book[free_shipping]" id="book-free-shipping" value="true">)
end
end
end
end
end

Expand Down Expand Up @@ -1118,16 +1146,32 @@
end

describe 'with filled params' do
let(:params) { Hash[book: { category: val }] }
let(:val) { 'Non-Fiction' }
describe 'string value' do
let(:params) { Hash[book: { category: val }] }
let(:val) { 'Non-Fiction' }

it 'renders with value' do
actual = view.form_for(:book, action) do
radio_button :category, 'Fiction'
radio_button :category, 'Non-Fiction'
end.to_s
it 'renders with value' do
actual = view.form_for(:book, action) do
radio_button :category, 'Fiction'
radio_button :category, 'Non-Fiction'
end.to_s

actual.must_include %(<input type="radio" name="book[category]" value="Fiction">\n<input type="radio" name="book[category]" value="Non-Fiction" checked="checked">)
actual.must_include %(<input type="radio" name="book[category]" value="Fiction">\n<input type="radio" name="book[category]" value="Non-Fiction" checked="checked">)
end
end

describe 'decimal value' do
let(:params) { Hash[book: { price: val }] }
let(:val) { '20.0' }

it 'renders with value' do
actual = view.form_for(:book, action) do
radio_button :price, 10.0
radio_button :price, 20.0
end.to_s

actual.must_include %(<input type="radio" name="book[price]" value="10.0">\n<input type="radio" name="book[price]" value="20.0" checked="checked">)
end
end
end
end
Expand Down Expand Up @@ -1256,15 +1300,31 @@
end

describe 'with filled params' do
let(:params) { Hash[book: { store: val }] }
let(:val) { 'it' }
describe 'string values' do
let(:params) { Hash[book: { store: val }] }
let(:val) { 'it' }

it 'renders with value' do
actual = view.form_for(:book, action) do
select :store, values, options: { prompt: 'Select a store' }
end.to_s
it 'renders with value' do
actual = view.form_for(:book, action) do
select :store, values, options: { prompt: 'Select a store' }
end.to_s

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

describe 'integer values' do
let(:values) { Hash['Brave new world' => 1, 'Solaris' => 2] }
let(:params) { Hash[bookshelf: { book: val }] }
let(:val) { '1' }

actual.must_include %(<select name="book[store]" id="book-store">\n<option>Select a store</option>\n<option value="it" selected="selected">Italy</option>\n<option value="us">United States</option>\n</select>)
it 'renders' do
actual = view.form_for(:bookshelf, action) do
select :book, values
end.to_s

actual.must_include %(<select name="bookshelf[book]" id="bookshelf-book">\n<option value="1" selected="selected">Brave new world</option>\n<option value="2">Solaris</option>\n</select>)
end
end
end
end
Expand Down
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'rubygems'
require 'bundler/setup'
require 'ostruct'

if ENV['COVERALL']
require 'coveralls'
Expand Down

0 comments on commit 1453ad3

Please sign in to comment.