Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Improved :prompt patch #710

Merged
merged 1 commit into from

2 participants

@dim
dim commented

Originally: #708

@sobrinho sobrinho merged commit dbb6e92 into from
@sobrinho
Collaborator

Thanks @dim!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 6, 2011
  1. @dim
This page is out of date. Refresh to see the latest.
View
43 lib/formtastic/inputs/select_input.rb
@@ -138,69 +138,70 @@ class SelectInput
include Base
include Base::Collections
include Base::GroupedCollections
-
+
def to_html
input_wrapping do
label_html <<
(options[:group_by] ? grouped_select_html : select_html)
end
end
-
+
def select_html
builder.select(input_name, collection, input_options, input_html_options)
end
-
+
def grouped_select_html
builder.grouped_collection_select(
- input_name,
+ input_name,
grouped_collection,
- group_association,
+ group_association,
group_label_method,
- value_method,
+ value_method,
label_method,
- input_options,
+ input_options,
input_html_options
)
end
-
+
def include_blank?
- return options[:prompt] if options.key?(:prompt)
- return options[:include_blank] if options.key?(:include_blank)
- return true if (single? && builder.include_blank_for_select_by_default)
- false
+ options.key?(:include_blank) ? !!options[:include_blank] : (single? && builder.include_blank_for_select_by_default)
+ end
+
+ def prompt?
+ !!options[:prompt]
end
def label_html_options
super.merge(:for => input_html_options[:id])
end
-
+
def input_options
- super.merge({:include_blank => include_blank?, :prompt => nil})
+ super.merge :include_blank => include_blank? && !prompt?
end
-
+
def input_html_options
extra_input_html_options.merge(super)
end
-
+
def extra_input_html_options
{
:multiple => multiple?,
:name => "#{object_name}[#{association_primary_key}]#{'[]' if multiple?}"
}
end
-
+
def multiple_by_association?
reflection && [ :has_many, :has_and_belongs_to_many ].include?(reflection.macro)
end
-
+
def multiple_by_options?
options[:multiple] || (options[:input_html] && options[:input_html][:multiple])
end
-
+
def multiple?
- multiple_by_options? || multiple_by_association?
+ multiple_by_options? || multiple_by_association?
end
-
+
def single?
!multiple?
end
View
12 spec/inputs/select_input_spec.rb
@@ -75,7 +75,7 @@
end
end
end
-
+
describe 'using a nil name' do
before do
concat(semantic_form_for(@new_post) do |builder|
@@ -432,8 +432,8 @@
output_buffer.should have_tag("form li select option[@value='']", /choose author/, :count => 1)
end
- it 'should not have a blank select option' do
- output_buffer.should_not have_tag("form li select option[@value='']", "")
+ it 'should not have a second blank select option' do
+ output_buffer.should have_tag("form li select option[@value='']", :count => 1)
end
end
@@ -469,7 +469,7 @@
end)
output_buffer.should have_tag("form li select[@name='project[author_name]']")
end
-
+
describe 'and :multiple is set to true through :input_html' do
it "should make the select a multi-select" do
concat(semantic_form_for(:project, :url => 'http://test.host') do |builder|
@@ -478,7 +478,7 @@
output_buffer.should have_tag("form li select[@multiple]")
end
end
-
+
describe 'and :multiple is set to true' do
it "should make the select a multi-select" do
concat(semantic_form_for(:project, :url => 'http://test.host') do |builder|
@@ -487,7 +487,7 @@
output_buffer.should have_tag("form li select[@multiple]")
end
end
-
+
end
describe 'when a grouped collection collection is given' do
Something went wrong with that request. Please try again.