Skip to content
Browse files

added a new boolean_select input, for boolean fields that work better…

… with a 'yes/no' select box rather than a checkbox -- might even make this the default for booleans?
  • Loading branch information...
1 parent a32935f commit 2007904bedfeb1897f44c3f2c222d8d822654977 @justinfrench committed Jul 6, 2008
Showing with 19 additions and 4 deletions.
  1. +1 −0 README.textile
  2. +18 −4 lib/justin_french/formtastic.rb
View
1 README.textile
@@ -64,6 +64,7 @@ h2. The Available Inputs
* :datetime (a date and time select) - default for :datetime and :timestamp column types
* :time (a time select) - default for :time column types
* :boolean (a checkbox) - default for :boolean column types
+* :boolean_select (a yes/no select box)
* :string (a text field) - default for :string column types
* :numeric (a text field, like string) - default for :integer, :float and :decimal column types
View
22 lib/justin_french/formtastic.rb
@@ -69,6 +69,7 @@ class SemanticFormBuilder < ActionView::Helpers::FormBuilder
# * :datetime (a date and time select) - default for :datetime and :timestamp column types
# * :time (a time select) - default for :time column types
# * :boolean (a checkbox) - default for :boolean column types
+ # * :boolean_select (a yes/no select box)
# * :string (a text field) - default for :string column types
# * :numeric (a text field, like string) - default for :integer, :float and :decimal column types
#
@@ -357,16 +358,29 @@ def date_or_datetime_input(method, options)
end
- # Outputs a label containing a checkbox and the label text.
- #
- # TODO - what about a yes/no boolean?
+ # Outputs a label containing a checkbox and the label text. The label defaults to the column
+ # name (method name) and can be altered with the :label option.
def boolean_input(method, options)
input_label(method, options,
@template.check_box(@object_name, method) +
label_text(method, options)
)
end
-
+
+ # Outputs a label and select box containing two options for "true" and "false". The visible
+ # text defaults to "Yes" and "No" respectively, but can be altered with the :true and :false
+ # options. The label text to the column name (method name), but can be altered with the
+ # :label option. Example:
+ #
+ # f.input :awesome, :as => :boolean_select, :true => "Yeah!", :false => "Nah!", :label => "Awesome?"
+ def boolean_select_input(method, options)
+ options[:true] ||= "Yes"
+ options[:false] ||= "No"
+
+ choices = [ [options[:true],1], [options[:false],0] ]
+ input_label(method, options) + @template.select(@object_name, method, choices)
+ end
+
def inline_errors(method, options) #:nodoc:
errors = @template.instance_eval("@#{@object_name}").errors.on(method).to_a
errors.empty? ? '' : @template.content_tag(:p, errors.to_sentence, :class => 'inline-errors')

0 comments on commit 2007904

Please sign in to comment.
Something went wrong with that request. Please try again.