Skip to content

Commit

Permalink
Text area helper: accept a wider range of objects as options. Additio…
Browse files Browse the repository at this point in the history
…nal tests and docs. Ref #33
  • Loading branch information
jodosha committed Sep 8, 2015
1 parent 6671aa1 commit bb1694b
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 6 deletions.
34 changes: 30 additions & 4 deletions lib/lotus/helpers/form_helper/form_builder.rb
Expand Up @@ -526,15 +526,41 @@ def file_field(name, attributes = {})
#
# # Output:
# # <textarea name="user[hobby]" id="user-hobby">Football</textarea>
#
# @example Set content and HTML attributes
# <%=
# # ...
# text_area :hobby, 'Football', class: 'form-control'
# %>
#
# # Output:
# # <textarea name="user[hobby]" id="user-hobby" class="form-control">Football</textarea>
#
# @example Omit content and specify HTML attributes
# <%=
# # ...
# text_area :hobby, class: 'form-control'
# %>
#
# # Output:
# # <textarea name="user[hobby]" id="user-hobby" class="form-control"></textarea>
#
# @example Force blank value
# <%=
# # ...
# text_area :hobby, '', class: 'form-control'
# %>
#
# # Output:
# # <textarea name="user[hobby]" id="user-hobby" class="form-control"></textarea>
def text_area(name, content = nil, attributes = {})
if content.is_a? Hash
if content.respond_to?(:to_hash)
attributes = content
content = nil
content = nil
end

attributes = {name: _input_name(name), id: _input_id(name)}.merge(attributes)
_content = content || _value(name)
textarea(_content, attributes)
textarea(content || _value(name), attributes)
end

# Text input
Expand Down
38 changes: 36 additions & 2 deletions test/form_helper_test.rb
Expand Up @@ -762,14 +762,24 @@
actual.must_include %(<textarea name="book[description]" id="book-description" class="form-control" cols="5"></textarea>)
end

it "renders all HTML attributes along with the name and id" do
it "allows to omit content" do
actual = view.form_for(:book, action) do
text_area :description, class: 'form-control', cols: '5'
end.to_s

actual.must_include %(<textarea name="book[description]" id="book-description" class="form-control" cols="5"></textarea>)
end

it "allows to omit content, by accepting Hash serializable options" do
options = Lotus::Utils::Hash.new(class: 'form-control', cols: 5)

actual = view.form_for(:book, action) do
text_area :description, options
end.to_s

actual.must_include %(<textarea name="book[description]" id="book-description" class="form-control" cols="5"></textarea>)
end

describe "set content explicitly" do
let(:content) { "A short description of the book" }

Expand All @@ -794,13 +804,37 @@
actual.must_include %(<textarea name="book[description]" id="book-description">#{val}</textarea>)
end

it "allows to override 'value' attribute" do
it "renders with value, when only attributes are specified" do
actual = view.form_for(:book, action) do
text_area :description, class: 'form-control'
end.to_s

actual.must_include %(<textarea name="book[description]" id="book-description" class="form-control">#{val}</textarea>)
end

it "allows to override value" do
actual = view.form_for(:book, action) do
text_area :description, 'Just a simple description'
end.to_s

actual.must_include %(<textarea name="book[description]" id="book-description">Just a simple description</textarea>)
end

it "forces blank value" do
actual = view.form_for(:book, action) do
text_area :description, ''
end.to_s

actual.must_include %(<textarea name="book[description]" id="book-description"></textarea>)
end

it "forces blank value, when also attributes are specified" do
actual = view.form_for(:book, action) do
text_area :description, '', class: 'form-control'
end.to_s

actual.must_include %(<textarea name="book[description]" id="book-description" class="form-control"></textarea>)
end
end
end

Expand Down

0 comments on commit bb1694b

Please sign in to comment.