Skip to content
This repository
Browse code

added the :disabled option to #check_boxes_input

  • Loading branch information...
commit 89dca89780d58b5198d85aea6a3c843ee3be3521 1 parent a5865bf
Dallas Reedy dallas authored yabawock committed
6 lib/formtastic.rb
@@ -1112,7 +1112,10 @@ def check_boxes_input(method, options)
1112 1112 selected_option_is_present = [:selected, :checked].any? { |k| options.key?(k) }
1113 1113 selected_values = (options.key?(:checked) ? options[:checked] : options[:selected]) if selected_option_is_present
1114 1114 selected_values = [*selected_values].compact
1115   -
  1115 +
  1116 + disabled_option_is_present = options.key?(:disabled)
  1117 + disabled_values = [*options[:disabled]] if disabled_option_is_present
  1118 +
1116 1119 list_item_content = collection.map do |c|
1117 1120 label = c.is_a?(Array) ? c.first : c
1118 1121 value = c.is_a?(Array) ? c.last : c
@@ -1120,6 +1123,7 @@ def check_boxes_input(method, options)
1120 1123 input_ids << input_id
1121 1124
1122 1125 html_options[:checked] = selected_values.include?(value) if selected_option_is_present
  1126 + html_options[:disabled] = disabled_values.include?(value) if disabled_option_is_present
1123 1127 html_options[:id] = input_id
1124 1128
1125 1129 li_content = template.content_tag(:label,
55 spec/inputs/check_boxes_input_spec.rb
@@ -177,6 +177,61 @@
177 177 end
178 178
179 179
  180 + describe 'when :disabled is set' do
  181 + before do
  182 + @output_buffer = ''
  183 + end
  184 +
  185 + describe "no disabled items" do
  186 + before do
  187 + @new_post.stub!(:author_ids).and_return(nil)
  188 +
  189 + semantic_form_for(@new_post) do |builder|
  190 + concat(builder.input(:authors, :as => :check_boxes, :disabled => nil))
  191 + end
  192 + end
  193 +
  194 + it 'should not have any disabled item(s)' do
  195 + output_buffer.should_not have_tag("form li fieldset ol li label input[@disabled='disabled']")
  196 + end
  197 + end
  198 +
  199 + describe "single disabled item" do
  200 + before do
  201 + @new_post.stub!(:author_ids).and_return(nil)
  202 +
  203 + semantic_form_for(@new_post) do |builder|
  204 + concat(builder.input(:authors, :as => :check_boxes, :disabled => @fred.id))
  205 + end
  206 + end
  207 +
  208 + it "should have one item disabled; the specified one" do
  209 + output_buffer.should have_tag("form li fieldset ol li label input[@disabled='disabled']", :count => 1)
  210 + output_buffer.should have_tag("form li fieldset ol li label[@for='post_author_ids_#{@fred.id}']", /fred/i)
  211 + output_buffer.should have_tag("form li fieldset ol li label input[@disabled='disabled'][@value='#{@fred.id}']")
  212 + end
  213 + end
  214 +
  215 + describe "multiple disabled items" do
  216 + before do
  217 + @new_post.stub!(:author_ids).and_return(nil)
  218 +
  219 + semantic_form_for(@new_post) do |builder|
  220 + concat(builder.input(:authors, :as => :check_boxes, :disabled => [@bob.id, @fred.id]))
  221 + end
  222 + end
  223 +
  224 + it "should have multiple items disabled; the specified ones" do
  225 + output_buffer.should have_tag("form li fieldset ol li label input[@disabled='disabled']", :count => 2)
  226 + output_buffer.should have_tag("form li fieldset ol li label[@for='post_author_ids_#{@bob.id}']", /bob/i)
  227 + output_buffer.should have_tag("form li fieldset ol li label input[@disabled='disabled'][@value='#{@bob.id}']")
  228 + output_buffer.should have_tag("form li fieldset ol li label[@for='post_author_ids_#{@fred.id}']", /fred/i)
  229 + output_buffer.should have_tag("form li fieldset ol li label input[@disabled='disabled'][@value='#{@fred.id}']")
  230 + end
  231 + end
  232 +
  233 + end
  234 +
180 235 end
181 236
182 237

0 comments on commit 89dca89

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