Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added week_field form helper
  • Loading branch information
jodosha committed Mar 6, 2017
1 parent e1ae650 commit 88cc246
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
19 changes: 19 additions & 0 deletions lib/hanami/helpers/form_helper/form_builder.rb
Expand Up @@ -496,6 +496,25 @@ def month_field(name, attributes = {})
input _attributes(:month, name, attributes)
end

# Week field
#
# @param name [Symbol] the input name
# @param attributes [Hash] HTML attributes to pass to the input tag
#
# @since x.x.x
#
# @example Basic usage
# <%=
# # ...
# week_field :release_week
# %>
#
# # Output:
# # <input type="week" name="book[release_week]" id="book-release-week" value="">
def week_field(name, attributes = {})
input _attributes(:week, name, attributes)
end

# Email input
#
# @param name [Symbol] the input name
Expand Down
1 change: 1 addition & 0 deletions test/fixtures.rb
Expand Up @@ -269,6 +269,7 @@ class Book < Dry::Struct
attribute :published_at, Types::String.optional
attribute :publisher_email, Types::String.optional
attribute :release_date, Types::Form::Date.optional
attribute :release_week, Types::String.optional
attribute :release_month, Types::Form::Date.optional
attribute :store, Types::String.optional
end
Expand Down
84 changes: 84 additions & 0 deletions test/form_helper_test.rb
Expand Up @@ -741,6 +741,90 @@
end
end

describe '#week_field' do
it 'renders' do
actual = view.form_for(:book, action) do
week_field :release_week
end.to_s

actual.must_include %(<input type="week" name="book[release_week]" id="book-release-week" value="">)
end

it "allows to override 'id' attribute" do
actual = view.form_for(:book, action) do
week_field :release_week, id: 'release-week'
end.to_s

actual.must_include %(<input type="week" name="book[release_week]" id="release-week" value="">)
end

it "allows to override 'name' attribute" do
actual = view.form_for(:book, action) do
week_field :release_week, name: 'release_week'
end.to_s

actual.must_include %(<input type="week" name="release_week" id="book-release-week" value="">)
end

it "allows to override 'value' attribute" do
actual = view.form_for(:book, action) do
week_field :release_week, value: '2017-W10'
end.to_s

actual.must_include %(<input type="week" name="book[release_week]" id="book-release-week" value="2017-W10">)
end

it 'allows to specify HTML attributes' do
actual = view.form_for(:book, action) do
week_field :release_week, class: 'form-control'
end.to_s

actual.must_include %(<input type="week" name="book[release_week]" id="book-release-week" value="" class="form-control">)
end

describe 'with values' do
let(:values) { Hash[book: Book.new(release_week: val)] }
let(:val) { '2017-W10' }

it 'renders with value' do
actual = view.form_for(:book, action, values: values) do
week_field :release_week
end.to_s

actual.must_include %(<input type="week" name="book[release_week]" id="book-release-week" value="#{val}">)
end

it "allows to override 'value' attribute" do
actual = view.form_for(:book, action, values: values) do
week_field :release_week, value: '2017-W31'
end.to_s

actual.must_include %(<input type="week" name="book[release_week]" id="book-release-week" value="2017-W31">)
end
end

describe 'with filled params' do
let(:params) { Hash[book: { release_week: val }] }
let(:val) { '2017-W44' }

it 'renders with value' do
actual = view.form_for(:book, action) do
week_field :release_week
end.to_s

actual.must_include %(<input type="week" name="book[release_week]" id="book-release-week" value="#{val}">)
end

it "allows to override 'value' attribute" do
actual = view.form_for(:book, action) do
week_field :release_week, value: '2017-W07'
end.to_s

actual.must_include %(<input type="week" name="book[release_week]" id="book-release-week" value="2017-W07">)
end
end
end

describe '#email_field' do
it 'renders' do
actual = view.form_for(:book, action) do
Expand Down

0 comments on commit 88cc246

Please sign in to comment.