actionview 6.0.3.1 → 6.1.0.rc2

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of actionview might be problematic. Click here for more details.

Files changed (63) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +160 -209
  3. data/MIT-LICENSE +1 -1
  4. data/lib/action_view.rb +4 -1
  5. data/lib/action_view/base.rb +21 -52
  6. data/lib/action_view/cache_expiry.rb +1 -2
  7. data/lib/action_view/context.rb +0 -1
  8. data/lib/action_view/dependency_tracker.rb +10 -4
  9. data/lib/action_view/digestor.rb +3 -2
  10. data/lib/action_view/gem_version.rb +3 -3
  11. data/lib/action_view/helpers/asset_tag_helper.rb +40 -15
  12. data/lib/action_view/helpers/asset_url_helper.rb +6 -4
  13. data/lib/action_view/helpers/atom_feed_helper.rb +2 -1
  14. data/lib/action_view/helpers/cache_helper.rb +10 -16
  15. data/lib/action_view/helpers/date_helper.rb +4 -4
  16. data/lib/action_view/helpers/form_helper.rb +59 -17
  17. data/lib/action_view/helpers/form_options_helper.rb +7 -16
  18. data/lib/action_view/helpers/form_tag_helper.rb +2 -1
  19. data/lib/action_view/helpers/javascript_helper.rb +3 -3
  20. data/lib/action_view/helpers/number_helper.rb +6 -6
  21. data/lib/action_view/helpers/rendering_helper.rb +11 -3
  22. data/lib/action_view/helpers/tag_helper.rb +92 -17
  23. data/lib/action_view/helpers/tags/base.rb +9 -5
  24. data/lib/action_view/helpers/tags/date_field.rb +1 -1
  25. data/lib/action_view/helpers/tags/date_select.rb +2 -2
  26. data/lib/action_view/helpers/tags/datetime_local_field.rb +1 -1
  27. data/lib/action_view/helpers/tags/label.rb +4 -0
  28. data/lib/action_view/helpers/tags/month_field.rb +1 -1
  29. data/lib/action_view/helpers/tags/select.rb +1 -1
  30. data/lib/action_view/helpers/tags/time_field.rb +1 -1
  31. data/lib/action_view/helpers/tags/week_field.rb +1 -1
  32. data/lib/action_view/helpers/text_helper.rb +1 -1
  33. data/lib/action_view/helpers/translation_helper.rb +94 -49
  34. data/lib/action_view/helpers/url_helper.rb +107 -13
  35. data/lib/action_view/layouts.rb +3 -2
  36. data/lib/action_view/log_subscriber.rb +26 -10
  37. data/lib/action_view/lookup_context.rb +3 -18
  38. data/lib/action_view/path_set.rb +0 -3
  39. data/lib/action_view/railtie.rb +35 -46
  40. data/lib/action_view/renderer/abstract_renderer.rb +93 -14
  41. data/lib/action_view/renderer/collection_renderer.rb +192 -0
  42. data/lib/action_view/renderer/object_renderer.rb +34 -0
  43. data/lib/action_view/renderer/partial_renderer.rb +20 -282
  44. data/lib/action_view/renderer/partial_renderer/collection_caching.rb +25 -26
  45. data/lib/action_view/renderer/renderer.rb +44 -1
  46. data/lib/action_view/renderer/streaming_template_renderer.rb +5 -1
  47. data/lib/action_view/renderer/template_renderer.rb +15 -12
  48. data/lib/action_view/rendering.rb +3 -1
  49. data/lib/action_view/routing_url_for.rb +1 -1
  50. data/lib/action_view/template.rb +9 -49
  51. data/lib/action_view/template/handlers.rb +0 -26
  52. data/lib/action_view/template/handlers/erb.rb +10 -14
  53. data/lib/action_view/template/handlers/erb/erubi.rb +9 -7
  54. data/lib/action_view/template/html.rb +1 -11
  55. data/lib/action_view/template/raw_file.rb +0 -3
  56. data/lib/action_view/template/renderable.rb +24 -0
  57. data/lib/action_view/template/resolver.rb +82 -40
  58. data/lib/action_view/template/text.rb +0 -3
  59. data/lib/action_view/test_case.rb +18 -25
  60. data/lib/action_view/testing/resolvers.rb +10 -31
  61. data/lib/action_view/unbound_template.rb +3 -3
  62. data/lib/action_view/view_paths.rb +34 -36
  63. metadata +17 -14
@@ -105,7 +105,7 @@ module ActionView
105
105
  end
106
106
 
107
107
  def tag_name(multiple = false, index = nil)
108
- # a little duplication to construct less strings
108
+ # a little duplication to construct fewer strings
109
109
  case
110
110
  when @object_name.empty?
111
111
  "#{sanitized_method_name}#{multiple ? "[]" : ""}"
@@ -117,7 +117,7 @@ module ActionView
117
117
  end
118
118
 
119
119
  def tag_id(index = nil)
120
- # a little duplication to construct less strings
120
+ # a little duplication to construct fewer strings
121
121
  case
122
122
  when @object_name.empty?
123
123
  sanitized_method_name.dup
@@ -129,11 +129,11 @@ module ActionView
129
129
  end
130
130
 
131
131
  def sanitized_object_name
132
- @sanitized_object_name ||= @object_name.gsub(/\]\[|[^-a-zA-Z0-9:.]/, "_").sub(/_$/, "")
132
+ @sanitized_object_name ||= @object_name.gsub(/\]\[|[^-a-zA-Z0-9:.]/, "_").delete_suffix("_")
133
133
  end
134
134
 
135
135
  def sanitized_method_name
136
- @sanitized_method_name ||= @method_name.sub(/\?$/, "")
136
+ @sanitized_method_name ||= @method_name.delete_suffix("?")
137
137
  end
138
138
 
139
139
  def sanitized_value(value)
@@ -166,8 +166,11 @@ module ActionView
166
166
 
167
167
  def add_options(option_tags, options, value = nil)
168
168
  if options[:include_blank]
169
- option_tags = tag_builder.content_tag_string("option", options[:include_blank].kind_of?(String) ? options[:include_blank] : nil, value: "") + "\n" + option_tags
169
+ content = (options[:include_blank] if options[:include_blank].is_a?(String))
170
+ label = (" " unless content)
171
+ option_tags = tag_builder.content_tag_string("option", content, value: "", label: label) + "\n" + option_tags
170
172
  end
173
+
171
174
  if value.blank? && options[:prompt]
172
175
  tag_options = { value: "" }.tap do |prompt_opts|
173
176
  prompt_opts[:disabled] = true if options[:disabled] == ""
@@ -175,6 +178,7 @@ module ActionView
175
178
  end
176
179
  option_tags = tag_builder.content_tag_string("option", prompt_text(options[:prompt]), tag_options) + "\n" + option_tags
177
180
  end
181
+
178
182
  option_tags
179
183
  end
180
184
 
@@ -6,7 +6,7 @@ module ActionView
6
6
  class DateField < DatetimeField # :nodoc:
7
7
  private
8
8
  def format_date(value)
9
- value.try(:strftime, "%Y-%m-%d")
9
+ value&.strftime("%Y-%m-%d")
10
10
  end
11
11
  end
12
12
  end
@@ -13,7 +13,7 @@ module ActionView
13
13
  end
14
14
 
15
15
  def render
16
- error_wrapping(datetime_selector(@options, @html_options).send("select_#{select_type}").html_safe)
16
+ error_wrapping(datetime_selector(@options, @html_options).public_send("select_#{select_type}").html_safe)
17
17
  end
18
18
 
19
19
  class << self
@@ -58,7 +58,7 @@ module ActionView
58
58
  time = Time.current
59
59
 
60
60
  [:year, :month, :day, :hour, :min, :sec].each do |key|
61
- default[key] ||= time.send(key)
61
+ default[key] ||= time.public_send(key)
62
62
  end
63
63
 
64
64
  Time.utc(
@@ -12,7 +12,7 @@ module ActionView
12
12
 
13
13
  private
14
14
  def format_date(value)
15
- value.try(:strftime, "%Y-%m-%dT%T")
15
+ value&.strftime("%Y-%m-%dT%T")
16
16
  end
17
17
  end
18
18
  end
@@ -25,6 +25,10 @@ module ActionView
25
25
 
26
26
  content
27
27
  end
28
+
29
+ def to_s
30
+ translation
31
+ end
28
32
  end
29
33
 
30
34
  def initialize(object_name, method_name, template_object, content_or_options = nil, options = nil)
@@ -6,7 +6,7 @@ module ActionView
6
6
  class MonthField < DatetimeField # :nodoc:
7
7
  private
8
8
  def format_date(value)
9
- value.try(:strftime, "%Y-%m")
9
+ value&.strftime("%Y-%m")
10
10
  end
11
11
  end
12
12
  end
@@ -15,7 +15,7 @@ module ActionView
15
15
 
16
16
  def render
17
17
  option_tags_options = {
18
- selected: @options.fetch(:selected) { value },
18
+ selected: @options.fetch(:selected) { value.nil? ? "" : value },
19
19
  disabled: @options[:disabled]
20
20
  }
21
21
 
@@ -6,7 +6,7 @@ module ActionView
6
6
  class TimeField < DatetimeField # :nodoc:
7
7
  private
8
8
  def format_date(value)
9
- value.try(:strftime, "%T.%L")
9
+ value&.strftime("%T.%L")
10
10
  end
11
11
  end
12
12
  end
@@ -6,7 +6,7 @@ module ActionView
6
6
  class WeekField < DatetimeField # :nodoc:
7
7
  private
8
8
  def format_date(value)
9
- value.try(:strftime, "%Y-W%V")
9
+ value&.strftime("%Y-W%V")
10
10
  end
11
11
  end
12
12
  end
@@ -228,7 +228,7 @@ module ActionView
228
228
  # pluralize(2, 'Person', locale: :de)
229
229
  # # => 2 Personen
230
230
  def pluralize(count, singular, plural_arg = nil, plural: plural_arg, locale: I18n.locale)
231
- word = if count == 1 || count.to_s =~ /^1(\.0+)?$/
231
+ word = if count == 1 || count.to_s.match?(/^1(\.0+)?$/)
232
232
  singular
233
233
  else
234
234
  plural || singular.pluralize(locale)
@@ -1,8 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "action_view/helpers/tag_helper"
4
- require "active_support/core_ext/string/access"
5
- require "i18n/exceptions"
4
+ require "active_support/core_ext/symbol/starts_ends_with"
6
5
 
7
6
  module ActionView
8
7
  # = Action View Translation Helpers
@@ -57,57 +56,55 @@ module ActionView
57
56
  # that include HTML tags so that you know what kind of output to expect
58
57
  # when you call translate in a template and translators know which keys
59
58
  # they can provide HTML values for.
59
+ #
60
+ # To access the translated text along with the fully resolved
61
+ # translation key, <tt>translate</tt> accepts a block:
62
+ #
63
+ # <%= translate(".relative_key") do |translation, resolved_key| %>
64
+ # <span title="<%= resolved_key %>"><%= translation %></span>
65
+ # <% end %>
66
+ #
67
+ # This enables annotate translated text to be aware of the scope it was
68
+ # resolved against.
69
+ #
60
70
  def translate(key, **options)
61
- if options.has_key?(:default)
62
- remaining_defaults = Array.wrap(options.delete(:default)).compact
63
- options[:default] = remaining_defaults unless remaining_defaults.first.kind_of?(Symbol)
64
- end
71
+ return key.map { |k| translate(k, **options) } if key.is_a?(Array)
65
72
 
66
- # If the user has explicitly decided to NOT raise errors, pass that option to I18n.
67
- # Otherwise, tell I18n to raise an exception, which we rescue further in this method.
68
- # Note: `raise_error` refers to us re-raising the error in this method. I18n is forced to raise by default.
69
- if options[:raise] == false
70
- raise_error = false
71
- i18n_raise = false
72
- else
73
- raise_error = options[:raise] || ActionView::Base.raise_on_missing_translations
74
- i18n_raise = true
73
+ alternatives = if options.key?(:default)
74
+ options[:default].is_a?(Array) ? options.delete(:default).compact : [options.delete(:default)]
75
75
  end
76
76
 
77
- if html_safe_translation_key?(key)
78
- html_safe_options = options.dup
79
- options.except(*I18n::RESERVED_KEYS).each do |name, value|
80
- unless name == :count && value.is_a?(Numeric)
81
- html_safe_options[name] = ERB::Util.html_escape(value.to_s)
82
- end
77
+ options[:raise] = true if options[:raise].nil? && ActionView::Base.raise_on_missing_translations
78
+ default = MISSING_TRANSLATION
79
+
80
+ translation = while key
81
+ if alternatives.blank? && !options[:raise].nil?
82
+ default = NO_DEFAULT # let I18n handle missing translation
83
83
  end
84
- translation = I18n.translate(scope_key_by_partial(key), **html_safe_options.merge(raise: i18n_raise))
85
- if translation.respond_to?(:map)
86
- translation.map { |element| element.respond_to?(:html_safe) ? element.html_safe : element }
84
+
85
+ key = scope_key_by_partial(key)
86
+ first_key ||= key
87
+
88
+ if html_safe_translation_key?(key)
89
+ html_safe_options ||= html_escape_translation_options(options)
90
+ translated = I18n.translate(key, **html_safe_options, default: default)
91
+ break html_safe_translation(translated) unless translated.equal?(MISSING_TRANSLATION)
87
92
  else
88
- translation.respond_to?(:html_safe) ? translation.html_safe : translation
89
- end
90
- else
91
- I18n.translate(scope_key_by_partial(key), **options.merge(raise: i18n_raise))
92
- end
93
- rescue I18n::MissingTranslationData => e
94
- if remaining_defaults.present?
95
- translate remaining_defaults.shift, **options.merge(default: remaining_defaults)
96
- else
97
- raise e if raise_error
98
-
99
- keys = I18n.normalize_keys(e.locale, e.key, e.options[:scope])
100
- title = +"translation missing: #{keys.join('.')}"
101
-
102
- interpolations = options.except(:default, :scope)
103
- if interpolations.any?
104
- title << ", " << interpolations.map { |k, v| "#{k}: #{ERB::Util.html_escape(v)}" }.join(", ")
93
+ translated = I18n.translate(key, **options, default: default)
94
+ break translated unless translated.equal?(MISSING_TRANSLATION)
105
95
  end
106
96
 
107
- return title unless ActionView::Base.debug_missing_translation
97
+ break alternatives.first if alternatives.present? && !alternatives.first.is_a?(Symbol)
98
+
99
+ key = alternatives&.shift
100
+ end
108
101
 
109
- content_tag("span", keys.last.to_s.titleize, class: "translation_missing", title: title)
102
+ if key.nil?
103
+ translation = missing_translation(first_key, options)
104
+ key = first_key
110
105
  end
106
+
107
+ block_given? ? yield(translation, key) : translation
111
108
  end
112
109
  alias :t :translate
113
110
 
@@ -121,13 +118,22 @@ module ActionView
121
118
  alias :l :localize
122
119
 
123
120
  private
121
+ MISSING_TRANSLATION = Object.new
122
+ private_constant :MISSING_TRANSLATION
123
+
124
+ NO_DEFAULT = [].freeze
125
+ private_constant :NO_DEFAULT
126
+
127
+ def self.i18n_option?(name)
128
+ (@i18n_option_names ||= I18n::RESERVED_KEYS.to_set).include?(name)
129
+ end
130
+
124
131
  def scope_key_by_partial(key)
125
- stringified_key = key.to_s
126
- if stringified_key.first == "."
127
- if @virtual_path
132
+ if key.start_with?(".")
133
+ if @current_template&.virtual_path
128
134
  @_scope_key_by_partial_cache ||= {}
129
- @_scope_key_by_partial_cache[@virtual_path] ||= @virtual_path.gsub(%r{/_?}, ".")
130
- "#{@_scope_key_by_partial_cache[@virtual_path]}#{stringified_key}"
135
+ @_scope_key_by_partial_cache[@current_template.virtual_path] ||= @current_template.virtual_path.gsub(%r{/_?}, ".")
136
+ "#{@_scope_key_by_partial_cache[@current_template.virtual_path]}#{key}"
131
137
  else
132
138
  raise "Cannot use t(#{key.inspect}) shortcut because path is not available"
133
139
  end
@@ -136,8 +142,47 @@ module ActionView
136
142
  end
137
143
  end
138
144
 
145
+ def html_escape_translation_options(options)
146
+ return options if options.empty?
147
+ html_safe_options = options.dup
148
+
149
+ options.each do |name, value|
150
+ unless TranslationHelper.i18n_option?(name) || (name == :count && value.is_a?(Numeric))
151
+ html_safe_options[name] = ERB::Util.html_escape(value.to_s)
152
+ end
153
+ end
154
+
155
+ html_safe_options
156
+ end
157
+
139
158
  def html_safe_translation_key?(key)
140
- /(?:_|\b)html\z/.match?(key.to_s)
159
+ /(?:_|\b)html\z/.match?(key)
160
+ end
161
+
162
+ def html_safe_translation(translation)
163
+ if translation.respond_to?(:map)
164
+ translation.map { |element| element.respond_to?(:html_safe) ? element.html_safe : element }
165
+ else
166
+ translation.respond_to?(:html_safe) ? translation.html_safe : translation
167
+ end
168
+ end
169
+
170
+ def missing_translation(key, options)
171
+ keys = I18n.normalize_keys(options[:locale] || I18n.locale, key, options[:scope])
172
+
173
+ title = +"translation missing: #{keys.join(".")}"
174
+
175
+ options.each do |name, value|
176
+ unless name == :scope
177
+ title << ", " << name.to_s << ": " << ERB::Util.html_escape(value)
178
+ end
179
+ end
180
+
181
+ if ActionView::Base.debug_missing_translation
182
+ content_tag("span", keys.last.to_s.titleize, class: "translation_missing", title: title)
183
+ else
184
+ title
185
+ end
141
186
  end
142
187
  end
143
188
  end
@@ -45,7 +45,7 @@ module ActionView
45
45
  def _back_url # :nodoc:
46
46
  _filtered_referrer || "javascript:history.back()"
47
47
  end
48
- protected :_back_url
48
+ private :_back_url
49
49
 
50
50
  def _filtered_referrer # :nodoc:
51
51
  if controller.respond_to?(:request)
@@ -56,12 +56,12 @@ module ActionView
56
56
  end
57
57
  rescue URI::InvalidURIError
58
58
  end
59
- protected :_filtered_referrer
59
+ private :_filtered_referrer
60
60
 
61
61
  # Creates an anchor element of the given +name+ using a URL created by the set of +options+.
62
62
  # See the valid options in the documentation for +url_for+. It's also possible to
63
- # pass a String instead of an options hash, which generates an anchor element that uses the
64
- # value of the String as the href for the link. Using a <tt>:back</tt> Symbol instead
63
+ # pass a \String instead of an options hash, which generates an anchor element that uses the
64
+ # value of the \String as the href for the link. Using a <tt>:back</tt> \Symbol instead
65
65
  # of an options hash will generate a link to the referrer (a JavaScript back link
66
66
  # will be used in place of a referrer if none exists). If +nil+ is passed as the name
67
67
  # the value of the link itself will become the name.
@@ -177,7 +177,7 @@ module ActionView
177
177
  # # => <a href="/searches?query=ruby+on+rails">Ruby on Rails search</a>
178
178
  #
179
179
  # link_to "Nonsense search", searches_path(foo: "bar", baz: "quux")
180
- # # => <a href="/searches?foo=bar&amp;baz=quux">Nonsense search</a>
180
+ # # => <a href="/searches?foo=bar&baz=quux">Nonsense search</a>
181
181
  #
182
182
  # The only option specific to +link_to+ (<tt>:method</tt>) is used as follows:
183
183
  #
@@ -226,7 +226,7 @@ module ActionView
226
226
  # The +options+ hash accepts the same options as +url_for+.
227
227
  #
228
228
  # There are a few special +html_options+:
229
- # * <tt>:method</tt> - Symbol of HTTP verb. Supported verbs are <tt>:post</tt>, <tt>:get</tt>,
229
+ # * <tt>:method</tt> - \Symbol of HTTP verb. Supported verbs are <tt>:post</tt>, <tt>:get</tt>,
230
230
  # <tt>:delete</tt>, <tt>:patch</tt>, and <tt>:put</tt>. By default it will be <tt>:post</tt>.
231
231
  # * <tt>:disabled</tt> - If set to true, it will generate a disabled button.
232
232
  # * <tt>:data</tt> - This option can be used to add custom data attributes.
@@ -235,7 +235,7 @@ module ActionView
235
235
  # * <tt>:form</tt> - This hash will be form attributes
236
236
  # * <tt>:form_class</tt> - This controls the class of the form within which the submit button will
237
237
  # be placed
238
- # * <tt>:params</tt> - Hash of parameters to be rendered as hidden fields within the form.
238
+ # * <tt>:params</tt> - \Hash of parameters to be rendered as hidden fields within the form.
239
239
  #
240
240
  # ==== Data attributes
241
241
  #
@@ -290,7 +290,7 @@ module ActionView
290
290
  #
291
291
  #
292
292
  # <%= button_to('Destroy', 'http://www.example.com',
293
- # method: "delete", remote: true, data: { confirm: 'Are you sure?', disable_with: 'loading...' }) %>
293
+ # method: :delete, remote: true, data: { confirm: 'Are you sure?', disable_with: 'loading...' }) %>
294
294
  # # => "<form class='button_to' method='post' action='http://www.example.com' data-remote='true'>
295
295
  # # <input name='_method' value='delete' type='hidden' />
296
296
  # # <input value='Destroy' type='submit' data-disable-with='loading...' data-confirm='Are you sure?' />
@@ -412,8 +412,7 @@ module ActionView
412
412
  # Creates a link tag of the given +name+ using a URL created by the set of
413
413
  # +options+ if +condition+ is true, otherwise only the name is
414
414
  # returned. To specialize the default behavior, you can pass a block that
415
- # accepts the name or the full argument list for +link_to_unless+ (see the examples
416
- # in +link_to_unless+).
415
+ # accepts the name or the full argument list for +link_to_if+.
417
416
  #
418
417
  # ==== Examples
419
418
  # <%= link_to_if(@current_user.nil?, "Login", { controller: "sessions", action: "new" }) %>
@@ -550,14 +549,14 @@ module ActionView
550
549
  return false unless request.get? || request.head?
551
550
 
552
551
  check_parameters ||= options.is_a?(Hash) && options.delete(:check_parameters)
553
- url_string = URI.parser.unescape(url_for(options)).force_encoding(Encoding::BINARY)
552
+ url_string = URI::DEFAULT_PARSER.unescape(url_for(options)).force_encoding(Encoding::BINARY)
554
553
 
555
554
  # We ignore any extra parameters in the request_uri if the
556
555
  # submitted URL doesn't have any either. This lets the function
557
556
  # work with things like ?order=asc
558
557
  # the behaviour can be disabled with check_parameters: true
559
558
  request_uri = url_string.index("?") || check_parameters ? request.fullpath : request.path
560
- request_uri = URI.parser.unescape(request_uri).force_encoding(Encoding::BINARY)
559
+ request_uri = URI::DEFAULT_PARSER.unescape(request_uri).force_encoding(Encoding::BINARY)
561
560
 
562
561
  if url_string.start_with?("/") && url_string != "/"
563
562
  url_string.chomp!("/")
@@ -571,6 +570,101 @@ module ActionView
571
570
  end
572
571
  end
573
572
 
573
+ # Creates an SMS anchor link tag to the specified +phone_number+, which is
574
+ # also used as the name of the link unless +name+ is specified. Additional
575
+ # HTML attributes for the link can be passed in +html_options+.
576
+ #
577
+ # When clicked, an SMS message is prepopulated with the passed phone number
578
+ # and optional +body+ value.
579
+ #
580
+ # +sms_to+ has a +body+ option for customizing the SMS message itself by
581
+ # passing special keys to +html_options+.
582
+ #
583
+ # ==== Options
584
+ # * <tt>:body</tt> - Preset the body of the message.
585
+ #
586
+ # ==== Examples
587
+ # sms_to "5155555785"
588
+ # # => <a href="sms:5155555785;">5155555785</a>
589
+ #
590
+ # sms_to "5155555785", "Text me"
591
+ # # => <a href="sms:5155555785;">Text me</a>
592
+ #
593
+ # sms_to "5155555785", "Text me",
594
+ # body: "Hello Jim I have a question about your product."
595
+ # # => <a href="sms:5155555785;?body=Hello%20Jim%20I%20have%20a%20question%20about%20your%20product">Text me</a>
596
+ #
597
+ # You can use a block as well if your link target is hard to fit into the name parameter. \ERB example:
598
+ #
599
+ # <%= sms_to "5155555785" do %>
600
+ # <strong>Text me:</strong>
601
+ # <% end %>
602
+ # # => <a href="sms:5155555785;">
603
+ # <strong>Text me:</strong>
604
+ # </a>
605
+ def sms_to(phone_number, name = nil, html_options = {}, &block)
606
+ html_options, name = name, nil if block_given?
607
+ html_options = (html_options || {}).stringify_keys
608
+
609
+ extras = %w{ body }.map! { |item|
610
+ option = html_options.delete(item).presence || next
611
+ "#{item.dasherize}=#{ERB::Util.url_encode(option)}"
612
+ }.compact
613
+ extras = extras.empty? ? "" : "?&" + extras.join("&")
614
+
615
+ encoded_phone_number = ERB::Util.url_encode(phone_number)
616
+ html_options["href"] = "sms:#{encoded_phone_number};#{extras}"
617
+
618
+ content_tag("a", name || phone_number, html_options, &block)
619
+ end
620
+
621
+ # Creates a TEL anchor link tag to the specified +phone_number+, which is
622
+ # also used as the name of the link unless +name+ is specified. Additional
623
+ # HTML attributes for the link can be passed in +html_options+.
624
+ #
625
+ # When clicked, the default app to make calls is opened, and it
626
+ # is prepopulated with the passed phone number and optional
627
+ # +country_code+ value.
628
+ #
629
+ # +phone_to+ has an optional +country_code+ option which automatically adds the country
630
+ # code as well as the + sign in the phone numer that gets prepopulated,
631
+ # for example if +country_code: "01"+ +\+01+ will be prepended to the
632
+ # phone numer, by passing special keys to +html_options+.
633
+ #
634
+ # ==== Options
635
+ # * <tt>:country_code</tt> - Prepends the country code to the number
636
+ #
637
+ # ==== Examples
638
+ # phone_to "1234567890"
639
+ # # => <a href="tel:1234567890">1234567890</a>
640
+ #
641
+ # phone_to "1234567890", "Phone me"
642
+ # # => <a href="tel:134567890">Phone me</a>
643
+ #
644
+ # phone_to "1234567890", "Phone me", country_code: "01"
645
+ # # => <a href="tel:+015155555785">Phone me</a>
646
+ #
647
+ # You can use a block as well if your link target is hard to fit into the name parameter. \ERB example:
648
+ #
649
+ # <%= phone_to "1234567890" do %>
650
+ # <strong>Phone me:</strong>
651
+ # <% end %>
652
+ # # => <a href="tel:1234567890">
653
+ # <strong>Phone me:</strong>
654
+ # </a>
655
+ def phone_to(phone_number, name = nil, html_options = {}, &block)
656
+ html_options, name = name, nil if block_given?
657
+ html_options = (html_options || {}).stringify_keys
658
+
659
+ country_code = html_options.delete("country_code").presence
660
+ country_code = country_code.nil? ? "" : "+#{ERB::Util.url_encode(country_code)}"
661
+
662
+ encoded_phone_number = ERB::Util.url_encode(phone_number)
663
+ html_options["href"] = "tel:#{country_code}#{encoded_phone_number}"
664
+
665
+ content_tag("a", name || phone_number, html_options, &block)
666
+ end
667
+
574
668
  private
575
669
  def convert_options_to_data_attributes(options, html_options)
576
670
  if html_options
@@ -594,7 +688,7 @@ module ActionView
594
688
  end
595
689
 
596
690
  def add_method_to_attributes!(html_options, method)
597
- if method_not_get_method?(method) && html_options["rel"] !~ /nofollow/
691
+ if method_not_get_method?(method) && !html_options["rel"]&.match?(/nofollow/)
598
692
  if html_options["rel"].blank?
599
693
  html_options["rel"] = "nofollow"
600
694
  else