Skip to content

Commit

Permalink
Just show blank option if neither include blank and prompt are sent a…
Browse files Browse the repository at this point in the history
…s options, resolving issue formtastic#16.
  • Loading branch information
josevalim committed May 31, 2009
1 parent a9bd9ec commit 0db1251
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
6 changes: 5 additions & 1 deletion lib/formtastic.rb
Expand Up @@ -554,10 +554,14 @@ def hidden_input(method, options)
#
# By default, all select inputs will have a blank option at the top of the list. You can add
# a prompt with the :prompt option, or disable the blank option with :include_blank => false.
#
def select_input(method, options)
collection = find_collection_for_column(method, options)
html_options = options.delete(:input_html) || {}
options[:include_blank] ||= true

unless options.key?(:include_blank) || options.key?(:prompt)
options[:include_blank] = true
end

reflection = find_reflection(method)
if reflection && [ :has_many, :has_and_belongs_to_many ].include?(reflection.macro)
Expand Down
18 changes: 9 additions & 9 deletions spec/formtastic_spec.rb
Expand Up @@ -1511,7 +1511,7 @@ def custom(arg1, arg2, options = {})
end

it 'should have a blank option by default' do
output_buffer.should have_tag("form li select option[@value='']", //)
output_buffer.should have_tag("form li select option[@value='']", "")
end
end

Expand All @@ -1524,25 +1524,25 @@ def custom(arg1, arg2, options = {})
end

it 'should not have a blank option' do
output_buffer.should have_tag("form li select option[@value='']", //)
output_buffer.should_not have_tag("form li select option[@value='']", "")
end
end
describe 'when :include_blank => true and :prompt => "choose something" is set' do

describe 'when :prompt => "choose something" is set' do
before do
@new_post.stub!(:author_id).and_return(nil)
semantic_form_for(@new_post) do |builder|
concat(builder.input(:author, :as => :select, :include_blank => true, :prompt => "choose author"))
concat(builder.input(:author, :as => :select, :prompt => "choose author"))
end
end

it 'should have a blank select option' do
output_buffer.should have_tag("form li select option[@value='']", //)
end

it 'should have a select with prompt' do
output_buffer.should have_tag("form li select option[@value='']", /choose author/)
end

it 'should not have a blank select option' do
output_buffer.should_not have_tag("form li select option[@value='']", "")
end
end

describe 'when no object is given' do
Expand Down

0 comments on commit 0db1251

Please sign in to comment.