Skip to content

Commit

Permalink
Added :as => :hidden support.
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed May 20, 2009
1 parent b477312 commit 2e9e7c5
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.textile
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 6 additions & 1 deletion generators/formtastic_stylesheets/templates/formtastic.css
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
9 changes: 9 additions & 0 deletions lib/formtastic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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:
#
Expand Down Expand Up @@ -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
Expand Down
31 changes: 30 additions & 1 deletion spec/formtastic_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 2e9e7c5

Please sign in to comment.