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(-) (limited to 'actionview') 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(-) (limited to 'actionview') 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(-) (limited to 'actionview') 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(-) (limited to 'actionview') 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(-) (limited to 'actionview') 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(-) (limited to 'actionview') 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(-) (limited to 'actionview') 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(-) (limited to 'actionview') 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(-) (limited to 'actionview') 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 From ecda6dfbdcd3103699465e846deaaf7feb3e9916 Mon Sep 17 00:00:00 2001 From: Godfrey Chan Date: Tue, 4 Mar 2014 03:12:18 -0800 Subject: Added missing changlog entry for #14255 --- actionview/CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'actionview') diff --git a/actionview/CHANGELOG.md b/actionview/CHANGELOG.md index 28508a5dfa..67b953a782 100644 --- a/actionview/CHANGELOG.md +++ b/actionview/CHANGELOG.md @@ -1 +1,8 @@ +* Fixed a problem where the default options for the `button_tag` helper is not + applied correctly. + + Fixes #14254. + + *Sergey Prikhodko* + Please check [4-1-stable](https://github.com/rails/rails/blob/4-1-stable/actionview/CHANGELOG.md) for previous changes. -- cgit v1.2.3 From 8b1ccd5949395530296daf3c0d620a8106e9ac4b Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Tue, 4 Mar 2014 08:21:32 -0300 Subject: Simplify handling of defaults/options in button_tag There's no need to rely on Active Support's Hash#reverse_merge for simple cases with default values, since we can just merge from the default rather than reverse merge from the options. This also avoids the creation of one extra hash object by moving to a Hash#merge! call. --- actionview/lib/action_view/helpers/form_tag_helper.rb | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'actionview') diff --git a/actionview/lib/action_view/helpers/form_tag_helper.rb b/actionview/lib/action_view/helpers/form_tag_helper.rb index 961d01eff4..326e303971 100644 --- a/actionview/lib/action_view/helpers/form_tag_helper.rb +++ b/actionview/lib/action_view/helpers/form_tag_helper.rb @@ -469,16 +469,13 @@ 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 options = content_or_options else options ||= {} end - options = options.stringify_keys - options = options.reverse_merge default_options + options = { 'name' => 'button', 'type' => 'submit' }.merge!(options.stringify_keys) if block_given? content_tag :button, options, &block -- cgit v1.2.3 From 7231a135ea53d306b259782cb93d68e633397be5 Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Tue, 4 Mar 2014 08:25:15 -0300 Subject: Use 1.9 style hash on docs [ci skip] --- actionview/lib/action_view/helpers/form_tag_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'actionview') diff --git a/actionview/lib/action_view/helpers/form_tag_helper.rb b/actionview/lib/action_view/helpers/form_tag_helper.rb index 326e303971..0bbe08166b 100644 --- a/actionview/lib/action_view/helpers/form_tag_helper.rb +++ b/actionview/lib/action_view/helpers/form_tag_helper.rb @@ -465,7 +465,7 @@ module ActionView # # Ask me! # # # - # button_tag "Checkout", data: { :disable_with => "Please wait..." } + # button_tag "Checkout", data: { disable_with: "Please wait..." } # # => # def button_tag(content_or_options = nil, options = nil, &block) -- cgit v1.2.3 From 1858cc60701dcfb801b9634a1da0177eaeba3807 Mon Sep 17 00:00:00 2001 From: Piotr Chmolowski Date: Sat, 1 Mar 2014 18:59:56 +0100 Subject: Variants in ActionView::Digestor Take variants into account when calculating template digests in ActionView::Digest. Digestor#digest now takes a hash as an argument to support variants and allow more flexibility in the future. Old-style arguments have been deprecated. Fixes #14242 --- actionview/CHANGELOG.md | 9 +++ actionview/lib/action_view/digestor.rb | 64 +++++++++++++++++----- actionview/lib/action_view/helpers/cache_helper.rb | 2 +- .../fixtures/digestor/messages/new.html+iphone.erb | 15 +++++ actionview/test/template/digestor_test.rb | 35 +++++++++--- 5 files changed, 103 insertions(+), 22 deletions(-) create mode 100644 actionview/test/fixtures/digestor/messages/new.html+iphone.erb (limited to 'actionview') diff --git a/actionview/CHANGELOG.md b/actionview/CHANGELOG.md index 67b953a782..e46f55a875 100644 --- a/actionview/CHANGELOG.md +++ b/actionview/CHANGELOG.md @@ -5,4 +5,13 @@ *Sergey Prikhodko* +* Take variants into account when calculating template digests in ActionView::Digestor. + + The arguments to ActionView::Digestor#digest are now being passed as a hash + to support variants and allow more flexibility in the future. The support for + regular (required) arguments is deprecated and will be removed in Rails 5.0 or later. + + *Piotr Chmolowski, Łukasz Strzałkowski* + + Please check [4-1-stable](https://github.com/rails/rails/blob/4-1-stable/actionview/CHANGELOG.md) for previous changes. diff --git a/actionview/lib/action_view/digestor.rb b/actionview/lib/action_view/digestor.rb index 5570e2a8dc..da43fef2e3 100644 --- a/actionview/lib/action_view/digestor.rb +++ b/actionview/lib/action_view/digestor.rb @@ -9,23 +9,56 @@ module ActionView @@digest_monitor = Monitor.new class << self - def digest(name, format, finder, options = {}) + # Supported options: + # + # * name - Template name + # * format - Template format + # * variant - Variant of +format+ (optional) + # * finder - An instance of ActionView::LookupContext + # * dependencies - An array of dependent views + # * partial - Specifies whether the template is a partial + def digest(*args) + options = _setup_options(*args) + + name = options[:name] + format = options[:format] + variant = options[:variant] + finder = options[:finder] + details_key = finder.details_key.hash dependencies = Array.wrap(options[:dependencies]) - cache_key = ([name, details_key, format] + dependencies).join('.') + cache_key = ([name, details_key, format, variant].compact + dependencies).join('.') # this is a correctly done double-checked locking idiom # (ThreadSafe::Cache's lookups have volatile semantics) @@cache[cache_key] || @@digest_monitor.synchronize do @@cache.fetch(cache_key) do # re-check under lock - compute_and_store_digest(cache_key, name, format, finder, options) + compute_and_store_digest(cache_key, options) end end end + def _setup_options(*args) + unless args.first.is_a?(Hash) + ActiveSupport::Deprecation.warn("Arguments to ActionView::Digestor should be provided as a hash. The support for regular arguments will be removed in Rails 5.0 or later") + + { + name: args.first, + format: args.second, + finder: args.third, + }.merge(args.fourth || {}) + else + options = args.first + options.assert_valid_keys(:name, :format, :variant, :finder, :dependencies, :partial) + + options + end + end + private - def compute_and_store_digest(cache_key, name, format, finder, options) # called under @@digest_monitor lock - klass = if options[:partial] || name.include?("/_") + + def compute_and_store_digest(cache_key, options) # called under @@digest_monitor lock + klass = if options[:partial] || options[:name].include?("/_") # Prevent re-entry or else recursive templates will blow the stack. # There is no need to worry about other threads seeing the +false+ value, # as they will then have to wait for this thread to let go of the @@digest_monitor lock. @@ -35,20 +68,25 @@ module ActionView Digestor end - digest = klass.new(name, format, finder, options).digest + digest = klass.new(options).digest # Store the actual digest if config.cache_template_loading is true @@cache[cache_key] = stored_digest = digest if ActionView::Resolver.caching? digest ensure # something went wrong or ActionView::Resolver.caching? is false, make sure not to corrupt the @@cache - @@cache.delete_pair(cache_key, false) if pre_stored && !stored_digest + @@cache.delete_pair(cache_key, false) if pre_stored && !stored_digest end end - attr_reader :name, :format, :finder, :options + attr_reader :name, :format, :variant, :finder, :options + + def initialize(*args) + @options = self.class._setup_options(*args) - def initialize(name, format, finder, options={}) - @name, @format, @finder, @options = name, format, finder, options + @name = @options.delete(:name) + @format = @options.delete(:format) + @variant = @options.delete(:variant) + @finder = @options.delete(:finder) end def digest @@ -68,7 +106,7 @@ module ActionView def nested_dependencies dependencies.collect do |dependency| - dependencies = PartialDigestor.new(dependency, format, finder).nested_dependencies + dependencies = PartialDigestor.new(name: dependency, format: format, finder: finder).nested_dependencies dependencies.any? ? { dependency => dependencies } : dependency end end @@ -88,7 +126,7 @@ module ActionView end def template - @template ||= finder.find(logical_name, [], partial?, formats: [ format ]) + @template ||= finder.find(logical_name, [], partial?, formats: [ format ], variants: [ variant ]) end def source @@ -97,7 +135,7 @@ module ActionView def dependency_digest template_digests = dependencies.collect do |template_name| - Digestor.digest(template_name, format, finder, partial: true) + Digestor.digest(name: template_name, format: format, finder: finder, partial: true) end (template_digests + injected_dependencies).join("-") diff --git a/actionview/lib/action_view/helpers/cache_helper.rb b/actionview/lib/action_view/helpers/cache_helper.rb index b3af1d4da4..30e4e5e329 100644 --- a/actionview/lib/action_view/helpers/cache_helper.rb +++ b/actionview/lib/action_view/helpers/cache_helper.rb @@ -167,7 +167,7 @@ module ActionView if @virtual_path [ *Array(name.is_a?(Hash) ? controller.url_for(name).split("://").last : name), - Digestor.digest(@virtual_path, formats.last.to_sym, lookup_context, dependencies: view_cache_dependencies) + Digestor.digest(name: @virtual_path, format: formats.last.to_sym, variant: request.variant, finder: lookup_context, dependencies: view_cache_dependencies) ] else name diff --git a/actionview/test/fixtures/digestor/messages/new.html+iphone.erb b/actionview/test/fixtures/digestor/messages/new.html+iphone.erb new file mode 100644 index 0000000000..791e1d36b4 --- /dev/null +++ b/actionview/test/fixtures/digestor/messages/new.html+iphone.erb @@ -0,0 +1,15 @@ +<%# Template Dependency: messages/message %> + +<%= render "header" %> +<%= render "comments/comments" %> + +<%= render "messages/actions/move" %> + +<%= render @message.history.events %> + +<%# render "something_missing" %> +<%# render "something_missing_1" %> + +<% + # Template Dependency: messages/form +%> \ No newline at end of file diff --git a/actionview/test/template/digestor_test.rb b/actionview/test/template/digestor_test.rb index 779a7fb53c..2406a64310 100644 --- a/actionview/test/template/digestor_test.rb +++ b/actionview/test/template/digestor_test.rb @@ -26,7 +26,11 @@ class FixtureFinder end def find(logical_name, keys, partial, options) - FixtureTemplate.new("digestor/#{partial ? logical_name.gsub(%r|/([^/]+)$|, '/_\1') : logical_name}.#{options[:formats].first}.erb") + partial_name = partial ? logical_name.gsub(%r|/([^/]+)$|, '/_\1') : logical_name + format = options[:formats].first.to_s + format += "+#{options[:variants].first}" if options[:variants].any? + + FixtureTemplate.new("digestor/#{partial_name}.#{format}.erb") end end @@ -194,6 +198,13 @@ class TemplateDigestorTest < ActionView::TestCase end end + def test_variants + assert_digest_difference("messages/new", false, variant: :iphone) do + change_template("messages/new", :iphone) + change_template("messages/_header", :iphone) + end + end + def test_dependencies_via_options_results_in_different_digest digest_plain = digest("comments/_comment") digest_fridge = digest("comments/_comment", dependencies: ["fridge"]) @@ -242,6 +253,11 @@ class TemplateDigestorTest < ActionView::TestCase ActionView::Resolver.caching = resolver_before end + def test_arguments_deprecation + assert_deprecated(/should be provided as a hash/) { ActionView::Digestor.digest('messages/show', :html, finder) } + assert_deprecated(/should be provided as a hash/) { ActionView::Digestor.new('messages/show', :html, finder) } + end + private def assert_logged(message) old_logger = ActionView::Base.logger @@ -258,26 +274,29 @@ class TemplateDigestorTest < ActionView::TestCase end end - def assert_digest_difference(template_name, persistent = false) - previous_digest = digest(template_name) + def assert_digest_difference(template_name, persistent = false, options = {}) + previous_digest = digest(template_name, options) ActionView::Digestor.cache.clear unless persistent yield - assert previous_digest != digest(template_name), "digest didn't change" + assert previous_digest != digest(template_name, options), "digest didn't change" ActionView::Digestor.cache.clear end - def digest(template_name, options={}) - ActionView::Digestor.digest(template_name, :html, finder, options) + def digest(template_name, options = {}) + options = options.dup + ActionView::Digestor.digest({ name: template_name, format: :html, finder: finder }.merge(options)) end def finder @finder ||= FixtureFinder.new end - def change_template(template_name) - File.open("digestor/#{template_name}.html.erb", "w") do |f| + def change_template(template_name, variant = nil) + variant = "+#{variant}" if variant.present? + + File.open("digestor/#{template_name}.html#{variant}.erb", "w") do |f| f.write "\nTHIS WAS CHANGED!" end end -- cgit v1.2.3 From ed88a601f7b37de0f89b64249aaeed884faed836 Mon Sep 17 00:00:00 2001 From: Prem Sichanugrist Date: Fri, 28 Feb 2014 19:39:22 -0500 Subject: Do note remove `Content-Type` when `render :body` `render :body` should just not set the `Content-Type` header. By removing the header, it breaks the compatibility with other parts. After this commit, `render :body` will returns `text/html` content type, sets by default from `ActionDispatch::Response`, and it will preserve the overridden content type if you override it. Fixes #14197, #14238 This partially reverts commit 3047376870d4a7adc7ff15c3cb4852e073c8f1da. --- actionview/lib/action_view/helpers/rendering_helper.rb | 5 +++-- actionview/lib/action_view/rendering.rb | 5 ----- 2 files changed, 3 insertions(+), 7 deletions(-) (limited to 'actionview') diff --git a/actionview/lib/action_view/helpers/rendering_helper.rb b/actionview/lib/action_view/helpers/rendering_helper.rb index 15b88bcda6..ebfc35a4c7 100644 --- a/actionview/lib/action_view/helpers/rendering_helper.rb +++ b/actionview/lib/action_view/helpers/rendering_helper.rb @@ -17,8 +17,9 @@ module ActionView # * :html - Renders the html safe string passed in out, otherwise # performs html escape on the string first. Setting the content type as # text/html. - # * :body - Renders the text passed in, and does not set content - # type in the response. + # * :body - Renders the text passed in, and inherits the content + # type of text/html from ActionDispatch::Response + # object. # # If no options hash is passed or :update specified, the default is to render a partial and use the second parameter # as the locals hash. diff --git a/actionview/lib/action_view/rendering.rb b/actionview/lib/action_view/rendering.rb index f96587c816..017302d40f 100644 --- a/actionview/lib/action_view/rendering.rb +++ b/actionview/lib/action_view/rendering.rb @@ -102,11 +102,6 @@ module ActionView # Assign the rendered format to lookup context. def _process_format(format, options = {}) #:nodoc: super - - if options[:body] - self.no_content_type = true - end - lookup_context.formats = [format.to_sym] lookup_context.rendered_format = lookup_context.formats.first end -- cgit v1.2.3 From 2dd2fcf89673afbcf95240ecebaf34826a195164 Mon Sep 17 00:00:00 2001 From: Prem Sichanugrist Date: Tue, 18 Feb 2014 16:13:23 -0500 Subject: Introduce `Rails.gem_version` This method return `Gem::Version.new(Rails.version)`, suggesting a more reliable way to perform version comparison. Example: Rails.version #=> "4.1.2" Rails.gem_version #=> # Rails.version > "4.1.10" #=> false Rails.gem_version > Gem::Version.new("4.1.10") #=> true Gem::Requirement.new("~> 4.1.2") =~ Rails.gem_version #=> true This was originally introduced as `.version` by @charliesome in #8501 but got reverted in #10002 since it was not backward compatible. Also, updating template for `rake update_versions`. --- actionview/lib/action_view/gem_version.rb | 15 +++++++++++++++ actionview/lib/action_view/version.rb | 11 ++++------- 2 files changed, 19 insertions(+), 7 deletions(-) create mode 100644 actionview/lib/action_view/gem_version.rb (limited to 'actionview') diff --git a/actionview/lib/action_view/gem_version.rb b/actionview/lib/action_view/gem_version.rb new file mode 100644 index 0000000000..9266e55c47 --- /dev/null +++ b/actionview/lib/action_view/gem_version.rb @@ -0,0 +1,15 @@ +module ActionView + # Returns the version of the currently loaded ActionView as a Gem::Version + def self.gem_version + Gem::Version.new VERSION::STRING + end + + module VERSION + MAJOR = 4 + MINOR = 2 + TINY = 0 + PRE = "alpha" + + STRING = [MAJOR, MINOR, TINY, PRE].compact.join(".") + end +end diff --git a/actionview/lib/action_view/version.rb b/actionview/lib/action_view/version.rb index b3ff415775..f55d3fdaef 100644 --- a/actionview/lib/action_view/version.rb +++ b/actionview/lib/action_view/version.rb @@ -1,11 +1,8 @@ +require_relative 'gem_version' + module ActionView - # Returns the version of the currently loaded ActionView as a Gem::Version + # Returns the version of the currently loaded ActionView as a Gem::Version def self.version - Gem::Version.new "4.2.0.alpha" - end - - module VERSION #:nodoc: - MAJOR, MINOR, TINY, PRE = ActionView.version.segments - STRING = ActionView.version.to_s + gem_version end end -- cgit v1.2.3 From 589d1ed722a3add255adc5636eab7dcc262dd0ca Mon Sep 17 00:00:00 2001 From: Hendy Tanata Date: Wed, 5 Mar 2014 11:38:39 -0800 Subject: Update doc for TagHelper. --- actionview/lib/action_view/helpers/tag_helper.rb | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'actionview') diff --git a/actionview/lib/action_view/helpers/tag_helper.rb b/actionview/lib/action_view/helpers/tag_helper.rb index 3528381781..a9f3b0ffbc 100644 --- a/actionview/lib/action_view/helpers/tag_helper.rb +++ b/actionview/lib/action_view/helpers/tag_helper.rb @@ -42,7 +42,8 @@ module ActionView # For example, a key +user_id+ would render as data-user-id and # thus accessed as dataset.userId. # - # Values are encoded to JSON, with the exception of strings and symbols. + # Values are encoded to JSON, with the exception of strings, symbols and + # BigDecimals. # This may come in handy when using jQuery's HTML5-aware .data() # from 1.4.3. # @@ -56,6 +57,9 @@ module ActionView # tag("input", type: 'text', disabled: true) # # => # + # tag("input", type: 'text', class: ["strong", "highlight"]) + # # => + # # tag("img", src: "open & shut.png") # # => # @@ -75,7 +79,7 @@ module ActionView # Set escape to false to disable attribute value escaping. # # ==== Options - # The +options+ hash is used with attributes with no value like (disabled and + # The +options+ hash can be used with attributes with no value like (disabled and # readonly), which you can give a value of true in the +options+ hash. You can use # symbols or strings for the attribute names. # @@ -84,6 +88,8 @@ module ActionView # # =>

Hello world!

# content_tag(:div, content_tag(:p, "Hello world!"), class: "strong") # # =>

Hello world!

+ # content_tag(:div, "Hello world!", class: ["strong", "highlight"]) + # # =>
Hello world!
# content_tag("select", options, multiple: true) # # => # -- cgit v1.2.3 From b5dc91deaa0366d09d646d7c863eb32f14ed0610 Mon Sep 17 00:00:00 2001 From: Kenny Meyer Date: Sun, 9 Mar 2014 21:32:26 -0300 Subject: Enhance readability of ActionView DateHelper#distance_of_time_in_words Refactor numerical constants to module constants which give the numbers a contextual meaning. This commit aims to provide quicker understanding for part of the implementation of the DateHelper#distance_of_time_in_words method. --- actionview/lib/action_view/helpers/date_helper.rb | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'actionview') diff --git a/actionview/lib/action_view/helpers/date_helper.rb b/actionview/lib/action_view/helpers/date_helper.rb index 698f0ca31c..1738df9cac 100644 --- a/actionview/lib/action_view/helpers/date_helper.rb +++ b/actionview/lib/action_view/helpers/date_helper.rb @@ -19,6 +19,10 @@ module ActionView # the select_month method would use simply "date" (which can be overwritten using :prefix) instead # of \date[month]. module DateHelper + MINUTES_IN_YEAR = 525600 + MINUTES_IN_QUARTER_YEAR = 131400 + MINUTES_IN_THREE_QUARTERS_YEAR = 394200 + # Reports the approximate distance in time between two Time, Date or DateTime objects or integers as seconds. # Pass include_seconds: true if you want more detailed approximations when distance < 1 min, 29 secs. # Distances are reported based on the following table: @@ -120,11 +124,11 @@ module ActionView else minutes_with_offset = distance_in_minutes end - remainder = (minutes_with_offset % 525600) - distance_in_years = (minutes_with_offset.div 525600) - if remainder < 131400 + remainder = (minutes_with_offset % MINUTES_IN_YEAR) + distance_in_years = (minutes_with_offset.div MINUTES_IN_YEAR) + if remainder < MINUTES_IN_QUARTER_YEAR locale.t(:about_x_years, :count => distance_in_years) - elsif remainder < 394200 + elsif remainder < MINUTES_IN_THREE_QUARTERS_YEAR locale.t(:over_x_years, :count => distance_in_years) else locale.t(:almost_x_years, :count => distance_in_years + 1) -- cgit v1.2.3 From b87962f3a853548b0ba08f31167e1fe86fa91dfd Mon Sep 17 00:00:00 2001 From: Cakey | Buddy Magsipoc Date: Mon, 10 Mar 2014 20:10:38 +0800 Subject: Fix 'fields_for' doc typo. --- actionview/lib/action_view/helpers/form_helper.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'actionview') diff --git a/actionview/lib/action_view/helpers/form_helper.rb b/actionview/lib/action_view/helpers/form_helper.rb index 5235962f9f..1ff090f244 100644 --- a/actionview/lib/action_view/helpers/form_helper.rb +++ b/actionview/lib/action_view/helpers/form_helper.rb @@ -457,7 +457,7 @@ module ActionView # doesn't create the form tags themselves. This makes fields_for suitable # for specifying additional model objects in the same form. # - # Although the usage and purpose of +field_for+ is similar to +form_for+'s, + # Although the usage and purpose of +fields_for+ is similar to +form_for+'s, # its method signature is slightly different. Like +form_for+, it yields # a FormBuilder object associated with a particular model object to a block, # and within the block allows methods to be called on the builder to @@ -1268,7 +1268,7 @@ module ActionView # doesn't create the form tags themselves. This makes fields_for suitable # for specifying additional model objects in the same form. # - # Although the usage and purpose of +field_for+ is similar to +form_for+'s, + # Although the usage and purpose of +fields_for+ is similar to +form_for+'s, # its method signature is slightly different. Like +form_for+, it yields # a FormBuilder object associated with a particular model object to a block, # and within the block allows methods to be called on the builder to -- cgit v1.2.3