From 96d9691e71319f4c166315a36b96c2c3c54ed493 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Wed, 30 Apr 2008 17:14:28 -0500 Subject: FormHelper#label_tag accepts :for option [encoded] [#38 state:resolved] --- actionpack/lib/action_view/helpers/form_helper.rb | 18 ++-- actionpack/test/template/form_helper_test.rb | 116 ++++++++++++---------- 2 files changed, 72 insertions(+), 62 deletions(-) (limited to 'actionpack') diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb index 0e77a7e067..829f84dd64 100644 --- a/actionpack/lib/action_view/helpers/form_helper.rb +++ b/actionpack/lib/action_view/helpers/form_helper.rb @@ -1,6 +1,7 @@ require 'cgi' require 'action_view/helpers/date_helper' require 'action_view/helpers/tag_helper' +require 'action_view/helpers/form_tag_helper' module ActionView module Helpers @@ -51,7 +52,7 @@ module ActionView # # If the object name contains square brackets the id for the object will be inserted. For example: # - # <%= text_field "person[]", "name" %> + # <%= text_field "person[]", "name" %> # # ...will generate the following ERb. # @@ -91,7 +92,7 @@ module ActionView # # Even further, the form_for method allows you to more easily escape the instance variable convention. So while the stand-alone # approach would require text_field :person, :name, :object => person - # to work with local variables instead of instance ones, the form_for calls remain the same. You simply declare once with + # to work with local variables instead of instance ones, the form_for calls remain the same. You simply declare once with # :person, person and all subsequent field calls save :person and :object => person. # # Also note that form_for doesn't create an exclusive scope. It's still possible to use both the stand-alone FormHelper methods @@ -149,7 +150,7 @@ module ActionView # ... # <% end %> # - # And for namespaced routes, like admin_post_url: + # And for namespaced routes, like admin_post_url: # # <% form_for([:admin, @post]) do |f| %> # ... @@ -337,7 +338,7 @@ module ActionView # hash with +options+. These options will be tagged onto the HTML as an HTML element attribute as in the example # shown. # - # ==== Examples + # ==== Examples # hidden_field(:signup, :pass_confirm) # # => # @@ -404,7 +405,7 @@ module ActionView # is set to 0 which is convenient for boolean values. Since HTTP standards say that unchecked checkboxes don't post anything, # we add a hidden value with the same name as the checkbox as a work around. # - # ==== Examples + # ==== Examples # # Let's say that @post.validated? is 1: # check_box("post", "validated") # # => @@ -445,7 +446,7 @@ module ActionView end class InstanceTag #:nodoc: - include Helpers::TagHelper + include Helpers::TagHelper, Helpers::FormTagHelper attr_reader :method_name, :object_name @@ -467,11 +468,12 @@ module ActionView end def to_label_tag(text = nil, options = {}) + options = options.stringify_keys name_and_id = options.dup add_default_name_and_id(name_and_id) - options["for"] = name_and_id["id"] + options["for"] ||= name_and_id["id"] content = (text.blank? ? nil : text.to_s) || method_name.humanize - content_tag("label", content, options) + label_tag(name_and_id["id"], content, options) end def to_input_field_tag(field_type, options = {}) diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb index c48d5dfd2d..87e6ca12a1 100644 --- a/actionpack/test/template/form_helper_test.rb +++ b/actionpack/test/template/form_helper_test.rb @@ -6,11 +6,11 @@ silence_warnings do alias_method :title_before_type_cast, :title unless respond_to?(:title_before_type_cast) alias_method :body_before_type_cast, :body unless respond_to?(:body_before_type_cast) alias_method :author_name_before_type_cast, :author_name unless respond_to?(:author_name_before_type_cast) - + def new_record=(boolean) @new_record = boolean end - + def new_record? @new_record end @@ -36,13 +36,13 @@ class FormHelperTest < ActionView::TestCase def setup @post = Post.new @comment = Comment.new - def @post.errors() - Class.new{ - def on(field); "can't be empty" if field == "author_name"; end - def empty?() false end + def @post.errors() + Class.new{ + def on(field); "can't be empty" if field == "author_name"; end + def empty?() false end def count() 1 end - def full_messages() [ "Author name can't be empty" ] end - }.new + def full_messages() [ "Author name can't be empty" ] end + }.new end def @post.id; 123; end def @post.id_before_type_cast; 123; end @@ -72,11 +72,19 @@ class FormHelperTest < ActionView::TestCase label("post", "title", nil, :class => 'title_label') ) end - + def test_label_with_symbols assert_dom_equal('', label(:post, :title)) end + def test_label_with_for_attribute_as_symbol + assert_dom_equal('', label(:post, :title, nil, :for => "my_for")) + end + + def test_label_with_for_attribute_as_string + assert_dom_equal('', label(:post, :title, nil, "for" => "my_for")) + end + def test_text_field assert_dom_equal( '', text_field("post", "title") @@ -303,7 +311,7 @@ class FormHelperTest < ActionView::TestCase _erbout.concat f.submit('Create post') end - expected = + expected = "
" + "" + "" + @@ -325,7 +333,7 @@ class FormHelperTest < ActionView::TestCase _erbout.concat f.check_box(:secret) end - expected = + expected = "" + "
" + "" + @@ -346,7 +354,7 @@ class FormHelperTest < ActionView::TestCase _erbout.concat f.check_box(:secret) end - expected = + expected = "" + "" + "" + @@ -367,7 +375,7 @@ class FormHelperTest < ActionView::TestCase _erbout.concat f.check_box(:secret) end - expected = + expected = "" + "" + "" + @@ -423,7 +431,7 @@ class FormHelperTest < ActionView::TestCase _erbout.concat f.check_box(:secret) end - expected = + expected = "" + "" + "" + @@ -494,7 +502,7 @@ class FormHelperTest < ActionView::TestCase _erbout.concat f.check_box(:secret) end - expected = + expected = "" + "" + "" + @@ -511,7 +519,7 @@ class FormHelperTest < ActionView::TestCase _erbout.concat f.check_box(:secret) end - expected = + expected = "" + "" + "" + @@ -548,7 +556,7 @@ class FormHelperTest < ActionView::TestCase end end - expected = + expected = "" + "" + "" + @@ -571,7 +579,7 @@ class FormHelperTest < ActionView::TestCase end end - expected = + expected = "" + "" + "" + @@ -601,7 +609,7 @@ class FormHelperTest < ActionView::TestCase _erbout.concat f.check_box(:secret) end - expected = + expected = "" + "
" + "
" + @@ -623,7 +631,7 @@ class FormHelperTest < ActionView::TestCase _erbout.concat f.check_box(:secret) end - expected = + expected = "" + "
" + "
" + @@ -637,39 +645,39 @@ class FormHelperTest < ActionView::TestCase end def test_default_form_builder_with_active_record_helpers - - _erbout = '' + + _erbout = '' form_for(:post, @post) do |f| _erbout.concat f.error_message_on('author_name') _erbout.concat f.error_messages - end - - expected = %() + - %(
can't be empty
) + + end + + expected = %() + + %(
can't be empty
) + %(

1 error prohibited this post from being saved

There were problems with the following fields:

  • Author name can't be empty
) + %(
) - + assert_dom_equal expected, _erbout end - + def test_default_form_builder_no_instance_variable post = @post @post = nil - - _erbout = '' + + _erbout = '' form_for(:post, post) do |f| _erbout.concat f.error_message_on('author_name') _erbout.concat f.error_messages - end - - expected = %(
) + - %(
can't be empty
) + + end + + expected = %() + + %(
can't be empty
) + %(

1 error prohibited this post from being saved

There were problems with the following fields:

  • Author name can't be empty
) + %(
) - + assert_dom_equal expected, _erbout - + end # Perhaps this test should be moved to prototype helper tests. @@ -683,7 +691,7 @@ class FormHelperTest < ActionView::TestCase _erbout.concat f.check_box(:secret) end - expected = + expected = %(
) + "
" + "
" + @@ -693,31 +701,31 @@ class FormHelperTest < ActionView::TestCase assert_dom_equal expected, _erbout end - + def test_fields_for_with_labelled_builder _erbout = '' - + fields_for(:post, @post, :builder => LabelledFormBuilder) do |f| _erbout.concat f.text_field(:title) _erbout.concat f.text_area(:body) _erbout.concat f.check_box(:secret) end - - expected = + + expected = "
" + "
" + " " + "
" - + assert_dom_equal expected, _erbout end def test_form_for_with_html_options_adds_options_to_form_tag _erbout = '' - + form_for(:post, @post, :html => {:id => 'some_form', :class => 'some_class'}) do |f| end expected = "
" - + assert_dom_equal expected, _erbout end @@ -793,16 +801,16 @@ class FormHelperTest < ActionView::TestCase @comment.save _erbout = '' form_for([:admin, @post, @comment]) {} - + expected = %(
) assert_dom_equal expected, _erbout end - + def test_form_for_with_new_object_and_namespace_in_list @post.new_record = false _erbout = '' form_for([:admin, @post, @comment]) {} - + expected = %(
) assert_dom_equal expected, _erbout end @@ -819,10 +827,10 @@ class FormHelperTest < ActionView::TestCase def test_remote_form_for_with_html_options_adds_options_to_form_tag self.extend ActionView::Helpers::PrototypeHelper _erbout = '' - + remote_form_for(:post, @post, :html => {:id => 'some_form', :class => 'some_class'}) do |f| end expected = "
" - + assert_dom_equal expected, _erbout end @@ -837,21 +845,21 @@ class FormHelperTest < ActionView::TestCase "/posts/#{post.id}/comments/#{comment.id}" end alias_method :post_comment_path, :comment_path - + def admin_comments_path(post) "/admin/posts/#{post.id}/comments" end alias_method :admin_post_comments_path, :admin_comments_path - + def admin_comment_path(post, comment) "/admin/posts/#{post.id}/comments/#{comment.id}" end alias_method :admin_post_comment_path, :admin_comment_path - + def posts_path "/posts" - end - + end + def post_path(post) "/posts/#{post.id}" end -- cgit v1.2.3 From c83f75812ef89aea1b8d138aebec25de8057f156 Mon Sep 17 00:00:00 2001 From: Kevin Glowacz Date: Wed, 30 Apr 2008 17:21:18 -0500 Subject: Fixed labels that have a bracketed name and an index [#68 state:resolved] Signed-off-by: Joshua Peek --- actionpack/lib/action_view/helpers/form_helper.rb | 1 + actionpack/test/template/form_helper_test.rb | 12 ++++++++++++ 2 files changed, 13 insertions(+) (limited to 'actionpack') diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb index 829f84dd64..4459ccbce5 100644 --- a/actionpack/lib/action_view/helpers/form_helper.rb +++ b/actionpack/lib/action_view/helpers/form_helper.rb @@ -471,6 +471,7 @@ module ActionView options = options.stringify_keys name_and_id = options.dup add_default_name_and_id(name_and_id) + options.delete("index") options["for"] ||= name_and_id["id"] content = (text.blank? ? nil : text.to_s) || method_name.humanize label_tag(name_and_id["id"], content, options) diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb index 87e6ca12a1..b4857fcb62 100644 --- a/actionpack/test/template/form_helper_test.rb +++ b/actionpack/test/template/form_helper_test.rb @@ -540,6 +540,18 @@ class FormHelperTest < ActionView::TestCase _erbout end + def test_fields_for_object_with_bracketed_name_and_index + _erbout = '' + fields_for("author[post]", @post, :index => 1) do |f| + _erbout.concat f.label(:title) + _erbout.concat f.text_field(:title) + end + + assert_dom_equal "" + + "", + _erbout + end + def test_form_builder_does_not_have_form_for_method assert ! ActionView::Helpers::FormBuilder.instance_methods.include?('form_for') end -- cgit v1.2.3 From 74436d2203eba186baebc1ddc82ff2202d0fc005 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Thu, 1 May 2008 10:21:46 +0100 Subject: Fixed render :template for templates in top level of view path. [#54 state:resolved] --- actionpack/lib/action_view/template_finder.rb | 7 ++++++- actionpack/test/controller/render_test.rb | 20 ++++++++++++++++++++ actionpack/test/fixtures/shared.html.erb | 1 + actionpack/test/template/template_finder_test.rb | 5 +++-- 4 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 actionpack/test/fixtures/shared.html.erb (limited to 'actionpack') diff --git a/actionpack/lib/action_view/template_finder.rb b/actionpack/lib/action_view/template_finder.rb index aaf34de538..83b7e27c09 100644 --- a/actionpack/lib/action_view/template_finder.rb +++ b/actionpack/lib/action_view/template_finder.rb @@ -24,7 +24,12 @@ module ActionView #:nodoc: view_paths.flatten.compact.each do |dir| next if @@processed_view_paths.has_key?(dir) @@processed_view_paths[dir] = [] - Dir.glob("#{dir}/**/*/**").each do |file| + + # + # Dir.glob("#{dir}/**/*/**") reads all the directories in view path and templates inside those directories + # Dir.glob("#{dir}/**") reads templates residing at top level of view path + # + (Dir.glob("#{dir}/**/*/**") | Dir.glob("#{dir}/**")).each do |file| unless File.directory?(file) @@processed_view_paths[dir] << file.split(dir).last.sub(/^\//, '') diff --git a/actionpack/test/controller/render_test.rb b/actionpack/test/controller/render_test.rb index 7491f16a84..066fa6acd4 100644 --- a/actionpack/test/controller/render_test.rb +++ b/actionpack/test/controller/render_test.rb @@ -23,6 +23,14 @@ class TestController < ActionController::Base def render_hello_world_with_forward_slash render :template => "/test/hello_world" end + + def render_template_in_top_directory + render :template => 'shared' + end + + def render_template_in_top_directory_with_slash + render :template => '/shared' + end def render_hello_world_from_variable @person = "david" @@ -243,6 +251,18 @@ class RenderTest < Test::Unit::TestCase get :render_hello_world_with_forward_slash assert_template "test/hello_world" end + + def test_render_in_top_directory + get :render_template_in_top_directory + assert_template "shared" + assert_equal "Elastica", @response.body + end + + def test_render_in_top_directory_with_slash + get :render_template_in_top_directory_with_slash + assert_template "shared" + assert_equal "Elastica", @response.body + end def test_render_from_variable get :render_hello_world_from_variable diff --git a/actionpack/test/fixtures/shared.html.erb b/actionpack/test/fixtures/shared.html.erb new file mode 100644 index 0000000000..af262fc9f8 --- /dev/null +++ b/actionpack/test/fixtures/shared.html.erb @@ -0,0 +1 @@ +Elastica \ No newline at end of file diff --git a/actionpack/test/template/template_finder_test.rb b/actionpack/test/template/template_finder_test.rb index a162640de3..3d6baff5fb 100644 --- a/actionpack/test/template/template_finder_test.rb +++ b/actionpack/test/template/template_finder_test.rb @@ -21,13 +21,14 @@ class TemplateFinderTest < Test::Unit::TestCase assert_equal ["builder", "erb", "rhtml", "rjs", "rxml", "mab"].sort, ActionView::TemplateFinder.file_extension_cache[LOAD_PATH_ROOT].values.flatten.uniq.sort - assert_equal Dir.glob("#{LOAD_PATH_ROOT}/**/*/*.{erb,rjs,rhtml,builder,rxml,mab}").size, + assert_equal (Dir.glob("#{LOAD_PATH_ROOT}/**/*/*.{erb,rjs,rhtml,builder,rxml,mab}") | + Dir.glob("#{LOAD_PATH_ROOT}/**.{erb,rjs,rhtml,builder,rxml,mab}")).size, ActionView::TemplateFinder.file_extension_cache[LOAD_PATH_ROOT].keys.size end def test_should_cache_dir_content_properly assert ActionView::TemplateFinder.processed_view_paths[LOAD_PATH_ROOT] - assert_equal Dir.glob("#{LOAD_PATH_ROOT}/**/*/**").find_all {|f| !File.directory?(f) }.size, + assert_equal (Dir.glob("#{LOAD_PATH_ROOT}/**/*/**") | Dir.glob("#{LOAD_PATH_ROOT}/**")).find_all {|f| !File.directory?(f) }.size, ActionView::TemplateFinder.processed_view_paths[LOAD_PATH_ROOT].size end -- cgit v1.2.3