From 60bbbce7a302c18d3ab8b7450f94cabc9bbea835 Mon Sep 17 00:00:00 2001 From: Sergey Prikhodko Date: Mon, 3 Mar 2014 12:23:51 +0400 Subject: fixes default attributes for button_tag --- .../lib/action_view/helpers/form_tag_helper.rb | 20 +++++++++++++------- actionview/test/template/form_tag_helper_test.rb | 5 +++++ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/actionview/lib/action_view/helpers/form_tag_helper.rb b/actionview/lib/action_view/helpers/form_tag_helper.rb index 80f066b3be..bb38ae8555 100644 --- a/actionview/lib/action_view/helpers/form_tag_helper.rb +++ b/actionview/lib/action_view/helpers/form_tag_helper.rb @@ -469,13 +469,13 @@ module ActionView # # => # def button_tag(content_or_options = nil, options = nil, &block) - options = content_or_options if block_given? && content_or_options.is_a?(Hash) - options ||= {} - options = options.stringify_keys - - options.reverse_merge! 'name' => 'button', 'type' => 'submit' - - content_tag :button, content_or_options || 'Button', options, &block + if block_given? && content_or_options.is_a?(Hash) + options = button_tag_options_with_defaults(content_or_options) + content_tag :button, options, &block + else + options = button_tag_options_with_defaults(options) + content_tag :button, content_or_options || 'Button', options + end end # Displays an image which when clicked will submit the form. @@ -741,6 +741,12 @@ module ActionView def sanitize_to_id(name) name.to_s.delete(']').gsub(/[^-a-zA-Z0-9:.]/, "_") end + + def button_tag_options_with_defaults(options = {}) + default_options = { 'name' => 'button', 'type' => 'submit' } + options.stringify_keys! + options.reverse_merge! default_options + end end end end diff --git a/actionview/test/template/form_tag_helper_test.rb b/actionview/test/template/form_tag_helper_test.rb index 0d5831dc6f..cf824e2733 100644 --- a/actionview/test/template/form_tag_helper_test.rb +++ b/actionview/test/template/form_tag_helper_test.rb @@ -476,6 +476,11 @@ class FormTagHelperTest < ActionView::TestCase assert_dom_equal('', output) end + def test_button_tag_defaults_with_block_and_options + output = button_tag(:name => 'temptation', :value => 'within') { content_tag(:strong, 'Do not press me') } + assert_dom_equal('', output) + end + def test_button_tag_with_confirmation assert_dom_equal( %(), -- cgit v1.2.3 From aa2101ed33438fac494e7fc5510e4465f730ad2b Mon Sep 17 00:00:00 2001 From: Sergey Prikhodko Date: Mon, 3 Mar 2014 14:57:41 +0400 Subject: fix accept nil options --- actionview/lib/action_view/helpers/form_tag_helper.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/actionview/lib/action_view/helpers/form_tag_helper.rb b/actionview/lib/action_view/helpers/form_tag_helper.rb index bb38ae8555..b3f681e6a7 100644 --- a/actionview/lib/action_view/helpers/form_tag_helper.rb +++ b/actionview/lib/action_view/helpers/form_tag_helper.rb @@ -742,10 +742,12 @@ module ActionView name.to_s.delete(']').gsub(/[^-a-zA-Z0-9:.]/, "_") end - def button_tag_options_with_defaults(options = {}) - default_options = { 'name' => 'button', 'type' => 'submit' } + def button_tag_options_with_defaults(options) + options = options || {} options.stringify_keys! - options.reverse_merge! default_options + + default_options = { 'name' => 'button', 'type' => 'submit' } + options.reverse_merge default_options end end end -- cgit v1.2.3 From 4a7c8ef793dd5b13026efcc4a5a004bf96b0d1ea Mon Sep 17 00:00:00 2001 From: Sergey Prikhodko Date: Mon, 3 Mar 2014 15:40:16 +0400 Subject: fix button tag without options --- actionview/lib/action_view/helpers/form_tag_helper.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/actionview/lib/action_view/helpers/form_tag_helper.rb b/actionview/lib/action_view/helpers/form_tag_helper.rb index b3f681e6a7..ccb5c036dc 100644 --- a/actionview/lib/action_view/helpers/form_tag_helper.rb +++ b/actionview/lib/action_view/helpers/form_tag_helper.rb @@ -469,7 +469,7 @@ module ActionView # # => # def button_tag(content_or_options = nil, options = nil, &block) - if block_given? && content_or_options.is_a?(Hash) + if block_given? options = button_tag_options_with_defaults(content_or_options) content_tag :button, options, &block else @@ -744,7 +744,7 @@ module ActionView def button_tag_options_with_defaults(options) options = options || {} - options.stringify_keys! + options = options.stringify_keys default_options = { 'name' => 'button', 'type' => 'submit' } options.reverse_merge default_options -- cgit v1.2.3 From 51ff88cf6ae72c8c09145b0c0cba1e154d3068f1 Mon Sep 17 00:00:00 2001 From: Sergey Prikhodko Date: Mon, 3 Mar 2014 16:33:24 +0400 Subject: fix form button --- actionview/lib/action_view/helpers/form_tag_helper.rb | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/actionview/lib/action_view/helpers/form_tag_helper.rb b/actionview/lib/action_view/helpers/form_tag_helper.rb index ccb5c036dc..9f2eb49f72 100644 --- a/actionview/lib/action_view/helpers/form_tag_helper.rb +++ b/actionview/lib/action_view/helpers/form_tag_helper.rb @@ -469,12 +469,9 @@ module ActionView # # => # def button_tag(content_or_options = nil, options = nil, &block) - if block_given? - options = button_tag_options_with_defaults(content_or_options) - content_tag :button, options, &block - else + options = content_or_options.is_a?(Hash) ? content_or_options : options options = button_tag_options_with_defaults(options) - content_tag :button, content_or_options || 'Button', options + content_tag :button, content_or_options || 'Button', options, &block end end -- cgit v1.2.3 From 28c0177524d68844713086ea1343718916ad97a8 Mon Sep 17 00:00:00 2001 From: Sergey Prikhodko Date: Mon, 3 Mar 2014 16:42:04 +0400 Subject: fix indentations --- actionview/lib/action_view/helpers/form_tag_helper.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/actionview/lib/action_view/helpers/form_tag_helper.rb b/actionview/lib/action_view/helpers/form_tag_helper.rb index 9f2eb49f72..5136e4dda4 100644 --- a/actionview/lib/action_view/helpers/form_tag_helper.rb +++ b/actionview/lib/action_view/helpers/form_tag_helper.rb @@ -469,10 +469,9 @@ module ActionView # # => # def button_tag(content_or_options = nil, options = nil, &block) - options = content_or_options.is_a?(Hash) ? content_or_options : options - options = button_tag_options_with_defaults(options) - content_tag :button, content_or_options || 'Button', options, &block - end + options = content_or_options.is_a?(Hash) ? content_or_options : options + options = button_tag_options_with_defaults(options) + content_tag :button, content_or_options || 'Button', options, &block end # Displays an image which when clicked will submit the form. -- cgit v1.2.3 From ed4fc43287562efab01a903507cd0b9d19388a74 Mon Sep 17 00:00:00 2001 From: Sergey Prikhodko Date: Mon, 3 Mar 2014 17:16:28 +0400 Subject: fix content_or_options to be replaced by options and set to nil --- actionview/lib/action_view/helpers/form_tag_helper.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/actionview/lib/action_view/helpers/form_tag_helper.rb b/actionview/lib/action_view/helpers/form_tag_helper.rb index 5136e4dda4..4113acc6e9 100644 --- a/actionview/lib/action_view/helpers/form_tag_helper.rb +++ b/actionview/lib/action_view/helpers/form_tag_helper.rb @@ -469,7 +469,12 @@ module ActionView # # => # def button_tag(content_or_options = nil, options = nil, &block) - options = content_or_options.is_a?(Hash) ? content_or_options : options + + if content_or_options.is_a?(Hash) + options = content_or_options + content_or_options = nil + end + options = button_tag_options_with_defaults(options) content_tag :button, content_or_options || 'Button', options, &block end -- cgit v1.2.3 From d0d2497c2a00da7d96f12b3240af266428a1d579 Mon Sep 17 00:00:00 2001 From: Sergey Prikhodko Date: Mon, 3 Mar 2014 17:59:57 +0400 Subject: cleanup and move extracted method right into the helper --- actionview/lib/action_view/helpers/form_tag_helper.rb | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/actionview/lib/action_view/helpers/form_tag_helper.rb b/actionview/lib/action_view/helpers/form_tag_helper.rb index 4113acc6e9..330f74882d 100644 --- a/actionview/lib/action_view/helpers/form_tag_helper.rb +++ b/actionview/lib/action_view/helpers/form_tag_helper.rb @@ -469,13 +469,17 @@ module ActionView # # => # def button_tag(content_or_options = nil, options = nil, &block) + options ||= {} + default_options = { 'name' => 'button', 'type' => 'submit' } if content_or_options.is_a?(Hash) options = content_or_options content_or_options = nil end - options = button_tag_options_with_defaults(options) + options = options.stringify_keys + options.reverse_merge default_options + content_tag :button, content_or_options || 'Button', options, &block end @@ -742,14 +746,6 @@ module ActionView def sanitize_to_id(name) name.to_s.delete(']').gsub(/[^-a-zA-Z0-9:.]/, "_") end - - def button_tag_options_with_defaults(options) - options = options || {} - options = options.stringify_keys - - default_options = { 'name' => 'button', 'type' => 'submit' } - options.reverse_merge default_options - end end end end -- cgit v1.2.3 From ca1b98a5599d25636aa07ac441579143a187bb85 Mon Sep 17 00:00:00 2001 From: Sergey Prikhodko Date: Mon, 3 Mar 2014 18:32:29 +0400 Subject: rollback to private method --- actionview/lib/action_view/helpers/form_tag_helper.rb | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/actionview/lib/action_view/helpers/form_tag_helper.rb b/actionview/lib/action_view/helpers/form_tag_helper.rb index 330f74882d..4113acc6e9 100644 --- a/actionview/lib/action_view/helpers/form_tag_helper.rb +++ b/actionview/lib/action_view/helpers/form_tag_helper.rb @@ -469,17 +469,13 @@ module ActionView # # => # def button_tag(content_or_options = nil, options = nil, &block) - options ||= {} - default_options = { 'name' => 'button', 'type' => 'submit' } if content_or_options.is_a?(Hash) options = content_or_options content_or_options = nil end - options = options.stringify_keys - options.reverse_merge default_options - + options = button_tag_options_with_defaults(options) content_tag :button, content_or_options || 'Button', options, &block end @@ -746,6 +742,14 @@ module ActionView def sanitize_to_id(name) name.to_s.delete(']').gsub(/[^-a-zA-Z0-9:.]/, "_") end + + def button_tag_options_with_defaults(options) + options = options || {} + options = options.stringify_keys + + default_options = { 'name' => 'button', 'type' => 'submit' } + options.reverse_merge default_options + end end end end -- cgit v1.2.3 From e447ed6902e0134128728db7baaa48cdf74dc788 Mon Sep 17 00:00:00 2001 From: Sergey Prikhodko Date: Tue, 4 Mar 2014 13:18:32 +0400 Subject: remove private method and rewrite into more precise notation --- .../lib/action_view/helpers/form_tag_helper.rb | 24 +++++++++++----------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/actionview/lib/action_view/helpers/form_tag_helper.rb b/actionview/lib/action_view/helpers/form_tag_helper.rb index 4113acc6e9..961d01eff4 100644 --- a/actionview/lib/action_view/helpers/form_tag_helper.rb +++ b/actionview/lib/action_view/helpers/form_tag_helper.rb @@ -469,14 +469,22 @@ module ActionView # # => # def button_tag(content_or_options = nil, options = nil, &block) + default_options = { 'name' => 'button', 'type' => 'submit' } - if content_or_options.is_a?(Hash) + if content_or_options.is_a? Hash options = content_or_options - content_or_options = nil + else + options ||= {} end - options = button_tag_options_with_defaults(options) - content_tag :button, content_or_options || 'Button', options, &block + options = options.stringify_keys + options = options.reverse_merge default_options + + if block_given? + content_tag :button, options, &block + else + content_tag :button, content_or_options || 'Button', options + end end # Displays an image which when clicked will submit the form. @@ -742,14 +750,6 @@ module ActionView def sanitize_to_id(name) name.to_s.delete(']').gsub(/[^-a-zA-Z0-9:.]/, "_") end - - def button_tag_options_with_defaults(options) - options = options || {} - options = options.stringify_keys - - default_options = { 'name' => 'button', 'type' => 'submit' } - options.reverse_merge default_options - end end end end -- cgit v1.2.3