Permalink
Browse files

full(ish) implementation of namespacing a form's id prefix; more test…

…s required
  • Loading branch information...
1 parent 3759128 commit 717d8e8bc0b5ce5dfa205b4b605d0b19db6cb7fc Aditya Sanghi committed Oct 17, 2010
View
@@ -625,8 +625,11 @@ def options_require_validation?(options) #nodoc
def basic_input_helper(form_helper_method, type, method, options) #:nodoc:
html_options = options.delete(:input_html) || {}
html_options = default_string_options(method, type).merge(html_options) if [:numeric, :string, :password, :text, :phone, :search, :url, :email].include?(type)
-
- self.label(method, options_for_label(options)) <<
+ field_id = generate_html_id(method, "")
+ html_options[:id] ||= field_id
+ label_options = options_for_label(options)
+ label_options[:for] ||= html_options[:id]
+ self.label(method, label_options) <<
self.send(self.respond_to?(form_helper_method) ? form_helper_method : :text_field, method, html_options)
end
@@ -683,6 +686,7 @@ def search_input(method, options)
def hidden_input(method, options)
options ||= {}
html_options = options.delete(:input_html) || strip_formtastic_options(options)
+ html_options[:id] ||= generate_html_id(method, "")
self.hidden_field(method, html_options)
end
@@ -800,6 +804,7 @@ def select_input(method, options)
end
options = set_include_blank(options)
input_name = generate_association_input_name(method)
+ html_options[:id] ||= generate_html_id(input_name, "")
select_html = if options[:group_by]
# The grouped_options_select is a bit counter intuitive and not optimised (mostly due to ActiveRecord).
@@ -822,7 +827,9 @@ def select_input(method, options)
self.select(input_name, collection, strip_formtastic_options(options), html_options)
end
- self.label(method, options_for_label(options).merge(:input_name => input_name)) << select_html
+ label_options = options_for_label(options).merge(:input_name => input_name)
+ label_options[:for] ||= html_options[:id]
+ self.label(method, label_options) << select_html
end
def boolean_select_input(method, options)
@@ -838,8 +845,11 @@ def boolean_select_input(method, options)
# f.input :time_zone, :as => :time_zone, :priority_zones => /Australia/
def time_zone_input(method, options)
html_options = options.delete(:input_html) || {}
-
- self.label(method, options_for_label(options)) <<
+ field_id = generate_html_id(method, "")
+ html_options[:id] ||= field_id
+ label_options = options_for_label(options)
+ label_options[:for] ||= html_options[:id]
+ self.label(method, label_options) <<
self.time_zone_select(method, options.delete(:priority_zones),
strip_formtastic_options(options), html_options)
end
@@ -1224,7 +1234,12 @@ def country_input(method, options)
html_options = options.delete(:input_html) || {}
priority_countries = options.delete(:priority_countries) || self.class.priority_countries
- self.label(method, options_for_label(options)) <<
+ field_id = generate_html_id(method, "")
+ html_options[:id] ||= field_id
+ label_options = options_for_label(options)
+ label_options[:for] ||= html_options[:id]
+
+ self.label(method, label_options) <<
self.country_select(method, priority_countries, strip_formtastic_options(options), html_options)
end
@@ -1238,13 +1253,15 @@ def boolean_input(method, options)
#input = self.check_box(method, strip_formtastic_options(options).merge(html_options),
# checked_value, unchecked_value)
+ field_id = [@@custom_id_prefix,@object_name,method].reject{|x|x.blank?}.join("_")
input = template.check_box_tag(
"#{@object_name}[#{method}]",
checked_value,
(@object && @object.send(:"#{method}") == checked_value),
- :id => "#{@object_name}_#{method}"
+ :id => field_id
)
options = options_for_label(options)
+ options[:for] ||= field_id
# the label() method will insert this nested input into the label at the last minute
options[:label_prefix_for_nested_input] = input
@@ -64,4 +64,20 @@
output_buffer.should have_tag('form li label input[@name="project[allow_comments]"]')
end
+ describe "when id_prefix is provided" do
+
+ before do
+ @output_buffer = ''
+ mock_everything
+
+ @form = semantic_form_for(@new_post, :id_prefix => "context2") do |builder|
+ concat(builder.input(:allow_comments, :as => :boolean))
+ end
+ end
+
+ it_should_have_input_wrapper_with_id("context2_post_allow_comments_input")
+ it_should_have_label_for("context2_post_allow_comments")
+
+ end
+
end
@@ -33,7 +33,6 @@
output_buffer.should_not have_tag('form li fieldset legend label[@for^="author_post_ids_"]')
end
-
it 'should generate an ordered list with a list item for each choice' do
output_buffer.concat(@form) if Formtastic::Util.rails3?
output_buffer.should have_tag('form li fieldset ol')
@@ -363,5 +362,21 @@
end
end
+ describe "when id_prefix is provided" do
+
+ before do
+ @output_buffer = ''
+ mock_everything
+
+ @form = semantic_form_for(@fred, :id_prefix => "context2") do |builder|
+ concat(builder.input(:posts, :as => :check_boxes))
+ end
+ end
+
+ it_should_have_label_for('context2_author_post_ids_19')
+ it_should_have_input_with_id('context2_author_post_ids_19')
+ it_should_have_input_wrapper_with_id("context2_author_posts_input")
+ end
+
end
@@ -57,7 +57,7 @@
priority_countries = ["Foo", "Bah"]
semantic_form_for(@new_post) do |builder|
builder.stub!(:country_select).and_return(Formtastic::Util.html_safe("<select><option>...</option></select>"))
- builder.should_receive(:country_select).with(:country, priority_countries, {}, {}).and_return(Formtastic::Util.html_safe("<select><option>...</option></select>"))
+ builder.should_receive(:country_select).with(:country, priority_countries, {}, {:id => "post_country"}).and_return(Formtastic::Util.html_safe("<select><option>...</option></select>"))
concat(builder.input(:country, :as => :country, :priority_countries => priority_countries))
end
@@ -70,14 +70,32 @@
semantic_form_for(@new_post) do |builder|
builder.stub!(:country_select).and_return(Formtastic::Util.html_safe("<select><option>...</option></select>"))
- builder.should_receive(:country_select).with(:country, priority_countries, {}, {}).and_return(Formtastic::Util.html_safe("<select><option>...</option></select>"))
+ builder.should_receive(:country_select).with(:country, priority_countries, {}, {:id => "post_country"}).and_return(Formtastic::Util.html_safe("<select><option>...</option></select>"))
concat(builder.input(:country, :as => :country))
end
end
end
+ describe "when id_prefix is provided" do
+
+ before do
+ @output_buffer = ''
+ mock_everything
+
+ @form = semantic_form_for(@new_post, :id_prefix => 'context2') do |builder|
+ builder.stub!(:country_select).and_return(Formtastic::Util.html_safe("<select><option>...</option></select>"))
+ builder.should_receive(:country_select).with(:country, [], {}, {:id => "context2_post_country"}).and_return(Formtastic::Util.html_safe("<select><option>...</option></select>"))
+ concat(builder.input(:country, :priority_countries => []))
+ end
+ end
+
+ it_should_have_input_wrapper_with_id("context2_post_country_input")
+ it_should_have_label_for("context2_post_country")
+
+ end
+
describe "matching" do
describe "when the attribute is 'country'" do
@@ -57,6 +57,22 @@
end
end
+ describe "when id_prefix is provided" do
+
+ before do
+ output_buffer.replace ''
+ @form = semantic_form_for(@new_post, :id_prefix => "context2") do |builder|
+ concat(builder.input(:publish_at, :as => :date, :order => [:year, :month, :day]))
+ end
+ end
+
+ it_should_have_input_wrapper_with_id("context2_post_publish_at_input")
+ it_should_have_select_with_id("context2_post_publish_at_1i")
+ it_should_have_select_with_id("context2_post_publish_at_2i")
+ it_should_have_select_with_id("context2_post_publish_at_3i")
+
+ end
+
describe ':labels option' do
fields = [:year, :month, :day]
fields.each do |field|
@@ -190,6 +190,22 @@
#
#end
+ describe "when id_prefix is provided" do
+
+ before do
+ output_buffer.replace ''
+ @form = semantic_form_for(@new_post, :id_prefix => "context2") do |builder|
+ concat(builder.input(:created_at, :as => :datetime, :include_seconds => true))
+ end
+ end
+
+ it_should_have_input_wrapper_with_id("context2_post_created_at_input")
+ (1..6).each do |i|
+ it_should_have_select_with_id("context2_post_created_at_#{i}i")
+ end
+
+ end
+
describe ':labels option' do
fields = [:year, :month, :day, :hour, :minute]
fields.each do |field|
@@ -27,5 +27,18 @@
end
+ describe "when id_prefix is provided" do
+
+ before do
+ @form = semantic_form_for(@new_post, :id_prefix => 'context2') do |builder|
+ concat(builder.input(:email))
+ end
+ end
+
+ it_should_have_input_wrapper_with_id("context2_post_email_input")
+ it_should_have_label_and_input_with_id("context2_post_email")
+
+ end
+
end
@@ -30,5 +30,21 @@
output_buffer.should have_tag("form li input.myclass")
end
+ describe "when id_prefix is provided" do
+
+ before do
+ @output_buffer = ''
+ mock_everything
+
+ @form = semantic_form_for(@new_post, :id_prefix => 'context2') do |builder|
+ concat(builder.input(:body, :as => :file))
+ end
+ end
+
+ it_should_have_input_wrapper_with_id("context2_post_body_input")
+ it_should_have_label_and_input_with_id("context2_post_body")
+
+ end
+
end
@@ -74,5 +74,31 @@
output_buffer.should_not have_tag("form li ul.hints")
end
+ describe "when id_prefix is provided" do
+
+ before do
+ @output_buffer = ''
+ mock_everything
+
+ @form = semantic_form_for(@new_post, :id_prefix => 'context2') do |builder|
+ concat(builder.input(:secret, :as => :hidden))
+ concat(builder.input(:author_id, :as => :hidden, :value => 99))
+ concat(builder.input(:published, :as => :hidden, :input_html => {:value => true}))
+ concat(builder.input(:reviewer, :as => :hidden, :input_html => {:class => 'new_post_reviewer', :id => 'new_post_reviewer'}))
+ concat(builder.input(:author, :as => :hidden, :value => 'direct_value', :input_html => {:value => "formtastic_value"}))
+ end
+ end
+
+ attributes_to_check = [:secret, :author_id, :published, :reviewer, :author]
+ attributes_to_check.each do |a|
+ it_should_have_input_wrapper_with_id("context2_post_#{a}_input")
+ end
+
+ (attributes_to_check - [:reviewer]).each do |a|
+ it_should_have_input_with_id("context2_post_#{a}")
+ end
+
+ end
+
end
@@ -41,5 +41,16 @@
it_should_have_input_with_name("project[title]")
end
+ describe "when id_prefix provided" do
+ before do
+ @form = semantic_form_for(@new_post, :id_prefix => :context2) do |builder|
+ concat(builder.input(:title, :as => :numeric))
+ end
+ end
+
+ it_should_have_input_wrapper_with_id("context2_post_title_input")
+ it_should_have_label_and_input_with_id("context2_post_title")
+ end
+
end
@@ -42,4 +42,17 @@
it_should_have_input_with_name("project[title]")
end
+ describe "when id_prefix is provided" do
+
+ before do
+ @form = semantic_form_for(@new_post, :id_prefix => "context2") do |builder|
+ concat(builder.input(:title, :as => :password))
+ end
+ end
+
+ it_should_have_input_wrapper_with_id("context2_post_title_input")
+ it_should_have_label_and_input_with_id("context2_post_title")
+
+ end
+
end
@@ -27,5 +27,18 @@
end
+ describe "when id_prefix is provided" do
+
+ before do
+ @form = semantic_form_for(@new_post, :id_prefix => "context2") do |builder|
+ concat(builder.input(:phone))
+ end
+ end
+
+ it_should_have_input_wrapper_with_id("context2_post_phone_input")
+ it_should_have_label_and_input_with_id("context2_post_phone")
+
+ end
+
end
@@ -27,5 +27,18 @@
end
+ describe "when id_prefix is provided" do
+
+ before do
+ @form = semantic_form_for(@new_post, :id_prefix => "context2") do |builder|
+ concat(builder.input(:search))
+ end
+ end
+
+ it_should_have_input_wrapper_with_id("context2_post_search_input")
+ it_should_have_label_and_input_with_id("context2_post_search")
+
+ end
+
end
@@ -493,4 +493,15 @@
end
end
+ describe 'when a id_prefix is provided' do
+ before do
+ @form = semantic_form_for(@freds_post, :id_prefix => 'context2') do |builder|
+ concat(builder.input(:authors, :as => :select))
+ end
+ end
+ it_should_have_input_wrapper_with_id("context2_post_authors_input")
+ it_should_have_select_with_id("context2_post_author_ids")
+ it_should_have_label_for("context2_post_author_ids")
+ end
+
end
Oops, something went wrong.

0 comments on commit 717d8e8

Please sign in to comment.