Permalink
Browse files

added the :disabled option to #check_boxes_input

  • Loading branch information...
1 parent a5865bf commit 89dca89780d58b5198d85aea6a3c843ee3be3521 @dallas dallas committed with yabawock Feb 26, 2010
Showing with 60 additions and 1 deletion.
  1. +5 −1 lib/formtastic.rb
  2. +55 −0 spec/inputs/check_boxes_input_spec.rb
View
6 lib/formtastic.rb
@@ -1112,14 +1112,18 @@ def check_boxes_input(method, options)
selected_option_is_present = [:selected, :checked].any? { |k| options.key?(k) }
selected_values = (options.key?(:checked) ? options[:checked] : options[:selected]) if selected_option_is_present
selected_values = [*selected_values].compact
-
+
+ disabled_option_is_present = options.key?(:disabled)
+ disabled_values = [*options[:disabled]] if disabled_option_is_present
+
list_item_content = collection.map do |c|
label = c.is_a?(Array) ? c.first : c
value = c.is_a?(Array) ? c.last : c
input_id = generate_html_id(input_name, value.to_s.gsub(/\s/, '_').gsub(/\W/, '').downcase)
input_ids << input_id
html_options[:checked] = selected_values.include?(value) if selected_option_is_present
+ html_options[:disabled] = disabled_values.include?(value) if disabled_option_is_present
html_options[:id] = input_id
li_content = template.content_tag(:label,
View
55 spec/inputs/check_boxes_input_spec.rb
@@ -177,6 +177,61 @@
end
+ describe 'when :disabled is set' do
+ before do
+ @output_buffer = ''
+ end
+
+ describe "no disabled items" do
+ before do
+ @new_post.stub!(:author_ids).and_return(nil)
+
+ semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:authors, :as => :check_boxes, :disabled => nil))
+ end
+ end
+
+ it 'should not have any disabled item(s)' do
+ output_buffer.should_not have_tag("form li fieldset ol li label input[@disabled='disabled']")
+ end
+ end
+
+ describe "single disabled item" do
+ before do
+ @new_post.stub!(:author_ids).and_return(nil)
+
+ semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:authors, :as => :check_boxes, :disabled => @fred.id))
+ end
+ end
+
+ it "should have one item disabled; the specified one" do
+ output_buffer.should have_tag("form li fieldset ol li label input[@disabled='disabled']", :count => 1)
+ output_buffer.should have_tag("form li fieldset ol li label[@for='post_author_ids_#{@fred.id}']", /fred/i)
+ output_buffer.should have_tag("form li fieldset ol li label input[@disabled='disabled'][@value='#{@fred.id}']")
+ end
+ end
+
+ describe "multiple disabled items" do
+ before do
+ @new_post.stub!(:author_ids).and_return(nil)
+
+ semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:authors, :as => :check_boxes, :disabled => [@bob.id, @fred.id]))
+ end
+ end
+
+ it "should have multiple items disabled; the specified ones" do
+ output_buffer.should have_tag("form li fieldset ol li label input[@disabled='disabled']", :count => 2)
+ output_buffer.should have_tag("form li fieldset ol li label[@for='post_author_ids_#{@bob.id}']", /bob/i)
+ output_buffer.should have_tag("form li fieldset ol li label input[@disabled='disabled'][@value='#{@bob.id}']")
+ output_buffer.should have_tag("form li fieldset ol li label[@for='post_author_ids_#{@fred.id}']", /fred/i)
+ output_buffer.should have_tag("form li fieldset ol li label input[@disabled='disabled'][@value='#{@fred.id}']")
+ end
+ end
+
+ end
+
end

0 comments on commit 89dca89

Please sign in to comment.