diff --git a/README.textile b/README.textile index dd159cfe0..671350b54 100644 --- a/README.textile +++ b/README.textile @@ -240,6 +240,7 @@ h2. The Available Inputs * :numeric (a text field, like string) - default for :integer, :float and :decimal column types * :file (a file field) - default for paperclip or attachment_fu attributes * :country (a select menu of country names) - default for :string columns named "country", requires a country_select plugin to be installed +* :hidden (a hidden field) - creates a hidden field (added for compatibility) The documentation is pretty good for each of these (what it does, what the output is, what the options are, etc) so go check it out. diff --git a/generators/formtastic_stylesheets/templates/formtastic.css b/generators/formtastic_stylesheets/templates/formtastic.css index 379fb0590..dde2bd947 100644 --- a/generators/formtastic_stylesheets/templates/formtastic.css +++ b/generators/formtastic_stylesheets/templates/formtastic.css @@ -91,7 +91,12 @@ form.formtastic fieldset ol li.numeric input { width:74%; } form.formtastic fieldset ol li.text textarea { width:74%; } -/* CHECKBOX OVERRIDES +/* HIDDEN OVERRIDES +--------------------------------------------------------------------------------------------------*/ +form.formtastic fieldset ol li.hidden { display:none; } + + +/* BOOLEAN OVERRIDES --------------------------------------------------------------------------------------------------*/ form.formtastic fieldset ol li.boolean label { padding-left:25%; width:auto; } form.formtastic fieldset ol li.boolean label input { margin:0 0.5em 0 0.2em; } diff --git a/lib/formtastic.rb b/lib/formtastic.rb index 39680161c..1c39142f2 100644 --- a/lib/formtastic.rb +++ b/lib/formtastic.rb @@ -67,6 +67,7 @@ class SemanticFormBuilder < ActionView::Helpers::FormBuilder # * :string (a text field) - default for :string column types # * :numeric (a text field, like string) - default for :integer, :float and :decimal column types # * :country (a select menu of country names) - requires a country_select plugin to be installed + # * :hidden (a hidden field) - creates a hidden field (added for compatibility) # # Example: # @@ -456,6 +457,14 @@ def input_simple(type, method, options) self.send(INPUT_MAPPINGS[type], method, html_options) end + # Outputs a hidden field inside the wrapper, which should be hidden with CSS. + # Additionals options can be given and will be sent straight to hidden input + # element. + # + def hidden_input(method, options) + self.hidden_field(method, set_options(options)) + end + # Outputs a label and a select box containing options from the parent # (belongs_to, has_many, has_and_belongs_to_many) association. If an association # is has_many or has_and_belongs_to_many the select box will be set as multi-select diff --git a/spec/formtastic_spec.rb b/spec/formtastic_spec.rb index 99b482db6..8a6d93c5f 100644 --- a/spec/formtastic_spec.rb +++ b/spec/formtastic_spec.rb @@ -666,7 +666,7 @@ def custom(arg1, arg2, options = {}) end it 'should call the corresponding input method' do - [:select, :time_zone, :radio, :date, :datetime, :time, :boolean, :check_boxes].each do |input_style| + [:select, :time_zone, :radio, :date, :datetime, :time, :boolean, :check_boxes, :hidden].each do |input_style| @new_post.stub!(:generic_column_name) @new_post.stub!(:column_for_attribute).and_return(mock('column', :type => :string, :limit => 255)) semantic_form_for(@new_post) do |builder| @@ -1078,6 +1078,35 @@ def custom(arg1, arg2, options = {}) end end + describe ":as => :hidden" do + before do + @new_post.stub!(:hidden) + @new_post.stub!(:column_for_attribute).and_return(mock('column', :type => :string)) + + semantic_form_for(@new_post) do |builder| + concat(builder.input(:hidden, :as => :hidden)) + end + end + + it "should have a hidden class on the wrapper" do + output_buffer.should have_tag('form li.hidden') + end + + it 'should have a post_hidden_input id on the wrapper' do + output_buffer.should have_tag('form li#post_hidden_input') + end + + it 'should not generate a label for the input' do + output_buffer.should_not have_tag('form li label') + end + + it "should generate a input field" do + output_buffer.should have_tag("form li input#post_hidden") + output_buffer.should have_tag("form li input[@type=\"hidden\"]") + output_buffer.should have_tag("form li input[@name=\"post[hidden]\"]") + end + end + describe ":as => :time_zone" do before do @new_post.stub!(:time_zone)