Commit Graph

1253 Commits

Author SHA1 Message Date
Marek Kasztelnik 9e15560191 Failing test for lazy translation in partial with block 2020-12-30 14:07:09 +01:00
Sean Doyle 9af9458396 Consistently render `button_to` as `<button>`
Prior to this commit, the
[ActionView::Helpers::UrlHelper#button_to][button_to] helper rendered
`<input type="submit">` elements when passed its contents as a String
argument, and rendered `<button type="submit">` elements when passed its
contents as a block.

This difference is subtle, and might lead to surprises.

Additionally, a `<form>` element's submitter can encode a `name`/`value`
pairing, which will be submitted as part of the request. When
`button_to` renders an `<input type="submit">` element, the "button"
content is rendered as a `[value]` attribute, which prevents any
meaningful data from being encoded.

Since it's a single `<button>` or `<input type="submit">` within a
`<form>`, missing out on that opportunity to encode information might
not be a show stopper, but ensuring that a `<button>` element is
rendered _without_ a default `[value]` attribute enables applications to
encode additional information that can be accessed JavaScript as
`element.value`, instead of a workaround like
`element.getAttribute("data-value")`.

Support rendering `input` elements with button_to
---

To support the original behavior of `button_to` rendering `<input
type="submit">` elements when invoked _without_ a block, expose the
`app.config.button_to_generates_button_tag` configuration flag.

By default, it's set to `true` and ensures that all `button_to` calls
render `<button>` elements. To revert to the original behavior, set it
to `false`.

[button_to]: https://api.rubyonrails.org/v6.0/classes/ActionView/Helpers/UrlHelper.html#method-i-button_to

Co-authored-by: Dusan Orlovic <duleorlovic@gmail.com>
2020-12-28 19:33:05 -05:00
Andrew White 71bc41477d
Merge pull request #40882 from rails/add-preload-links-header-config
Add `config.action_view.preload_links_header` option
2020-12-21 06:41:13 +00:00
Andrew White 200083c232
Add `config.action_view.preload_links_header` option
PR #39939 added support for the `Link` header being generated
automatically when using `stylesheet_link_tag` and
`javascript_include_tag`. However not everything should be
preloaded, e.g. a link to a legacy IE stylesheet has no need to be
preloaded because IE doesn't support the header and in some browsers it
will trigger the preload even though it's not used since it's inside an
IE conditional comment. This leads to increased bandwith costs and
slower application performance.

To allow more flexibility for sites that may have complex needs for the
`Link` header this commit adds a configuration option that disables it
completely and leaves it up to the application to decide how to handle
generating a `Link` header.
2020-12-19 05:24:09 +00:00
aaron 432698ef2b
Fix `SELECT COUNT` queries when rendering ActiveRecord collections (#40870)
* Fix `SELECT COUNT` queries when rendering ActiveRecord collections

Fixes #40837

When rendering collections, calling `size` when the collection is an
ActiveRecord relation causes unwanted `SELECT COUNT(*)` queries. This
change ensures the collection is an array before getting the size, and
also loads the relation for any further array inspections.

* Test queries when rendering relation collections

* Add `length` support to partial collection iterator

Allows getting the size of a relation without duplicating records, but
still loads the relation. The length method existence needs to be
checked because you can pass in an `Enumerator`, which does not respond
to `length`.

* Ensure unsubscribed from notifications after tests

[Rafael Mendonça França + aar0nr]
2020-12-18 15:58:11 -05:00
Rafael França 9c79623ce9
Merge pull request #40839 from jonathanhefner/config-action_view-image_decoding
Add config.action_view.image_decoding
2020-12-16 20:20:22 -05:00
Rafael França faa27ddafc
Merge pull request #40841 from Shopify/link-preload-integrity
Link preloading should keep integrity hashes in the header
2020-12-16 19:58:35 -05:00
Étienne Barrié 4aa91262f0 Link preloading should keep integrity hashes in the header
When a stylesheet or javascript link tag (or preload link tag) is output
in the view, it also gets sent as a Link header for preloading. This
Link header is missing the integrity attribute even when one is set on
the tag, which prevents the browser from using the preloaded resource.

Co-authored-by: Adrianna Chang <adrianna.chang@shopify.com>
2020-12-14 15:50:26 -05:00
Jonathan Hefner 48c1bc2f65 Add config.action_view.image_decoding
This adds `Rails.application.config.action_view.image_decoding` to
configure the default value of the `image_tag` `:decoding` option.
2020-12-14 11:45:48 -06:00
Jonathan Hefner b8b18bbfad Handle nil translation key
`I18n.translate` returns `nil` when given a `nil` key, *unless* a
`default` is also specified.  If a `default` is specified, the `nil` key
is treated as a missing key.

In Rails 6.0, the `translate` helper always returned `nil` when given a
`nil` key.  After #40773, the `translate` helper always raised an
`I18n::ArgumentError` when given a `nil` key.  This commit fixes the
`translate` helper to mirror the `I18n.translate` behavior when given a
`nil` key, with and without a `default`.
2020-12-10 11:51:32 -06:00
Rafael França e0a362c138
Merge pull request #40168 from igor04/disable_with_and_automatically_disable_submit_tag
Do not use submit_tag auto-disabling when disable_with is set to false
2020-12-09 13:38:44 -05:00
Jonathan Hefner 2c555c8d60 Convert translation key to string as necessary
Follow-up to #39989.

`I18n.translate` converts the initial key (but not `default` keys) to a
string before performing a lookup.  For parity, we should do the same.
2020-12-09 11:49:29 -06:00
Rafael França 3c2a80d8b1
Merge pull request #38452 from jonathanhefner/config-action_view-image_loading
Add config.action_view.image_loading
2020-12-08 19:10:22 -05:00
Taufiq Muhammadi a911dd0e08
Call class method since sanitizer's instance method is private and add tests
revert back to earlier version that call class method of class
returned by #sanitizer_vendor.safe_list_sanitizer
2020-12-08 22:32:49 +00:00
KapilSachdev a908d06c85
feat(rubocop): Add Style/RedundantRegexpEscape
- This cop will help in removing unnecessary escaping inside Regexp literals.
2020-12-08 18:57:09 +00:00
Jonathan Hefner 926129a28b Add config.action_view.image_loading
Browser native support for lazy loading images is now a part of the
official HTML standard.  To indicate to the browser that an image should
be lazily loaded, add the `loading="lazy"` attribute to the `img` tag.
Or, in Rails parlance, add the `loading: "lazy"` option to the
`image_tag` call.

This commit adds `Rails.application.config.action_view.image_loading` to
configure the default value of the `image_tag` `:loading` option.  Thus
by setting `config.action_view.image_loading = "lazy"`, an application
can opt in to lazy loading images sitewide, without changing view code.
2020-12-05 10:06:09 -06:00
Sean Doyle 59ca21c011 Declare ActionView::Helpers::FormBuilder#id
`ActionView::Helpers::FormBuilder#id`
---

Generate an HTML `id` attribute value.

Return the [`<form>` element's][mdn-form] `id` attribute.

```html+erb
<%= form_for @post do |f| %>
  <%# ... %>

  <% content_for :sticky_footer do %>
    <%= form.button(form: f.id) %>
  <% end %>
<% end %>
```

In the example above, the `:sticky_footer` content area will exist
outside of the `<form>` element. [By declaring the `form` HTML
attribute][mdn-button-attr-form], we hint to the browser that the
generated `<button>` element should be treated as the `<form>` element's
submit button, regardless of where it exists in the DOM.

[A similar pattern could be used for `<input>`
elements][mdn-input-attr-form] (or other form controls) that do not
descend from the `<form>` element.

`ActionView::Helpers::FormBuilder#field_id`
---

Generate an HTML <tt>id</tt> attribute value for the given field

Return the value generated by the <tt>FormBuilder</tt> for the given
attribute name.

```html+erb
<%= form_for @post do |f| %>
  <%= f.label :title %>
  <%= f.text_field :title, aria: { describedby: form.field_id(:title, :error) } %>
  <span id="<%= f.field_id(:title, :error) %>">is blank</span>
<% end %>
```

In the example above, the <tt><input type="text"></tt> element built by
the call to <tt>FormBuilder#text_field</tt> declares an
<tt>aria-describedby</tt> attribute referencing the <tt><span></tt>
element, sharing a common <tt>id</tt> root (<tt>post_title</tt>, in this
case).

This method is powered by the `field_id` helper declared in
`action_view/helpers/form_tag_helper`, which is made available for
general template calls, separate from a `FormBuilder` instance.

[mdn-form]: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form
[mdn-button-attr-form]: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attr-form
[mdn-input-attr-form]: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attr-form
[mdn-aria-describedby]: https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/ARIA_Techniques/Using_the_aria-describedby_attribute
[w3c-wai]: https://www.w3.org/WAI/tutorials/forms/notifications/#listing-errors
2020-11-30 18:47:49 -05:00
Guillermo Iguaran 761fea3822
Merge pull request #40348 from rails/no-erb-in-ujsserver
Don't evaluate ERB in the ujs test server
2020-11-30 01:11:28 -08:00
Sean Doyle 7503ef9490
Escape HTML attributes for ERB interpolation (#40657)
Transforms a Hash into HTML Attributes, ready to be interpolated into
ERB.

```html+erb
<input <%= tag.attributes(type: :text, aria: { label: "Search" }) %> >
<%# => <input type="text" aria-label="Search"> %>
```

Utilizes the `ActionView::Helpers::TagHelper#tag_options` implementation
to enable combining ERB with attribute transformation, without requiring
templates to replace HTML strings with `tag` or `content_tag`
invocations.

Co-authored-by: David Heinemeier Hansson <david@loudthinking.com>
2020-11-28 11:01:15 +01:00
Sean Doyle d2e07395b3 ActionView: Serialize Regexp into HTML attribute
When serializing a Regexp instance, encode the [Regexp#source][]. When
encoding a value into a [pattern][] attribute from ERB/Ruby, declaring
the String can be tedious. For example, one might attempt to encode
`\w+` as `"\\\w+"`, but once serialized to the browser, that is not
equivalent to the `"\w+"` HTML attribute.

Instead, enable declaring Regexp and Regexp literals as attributes, and
encoding them as their source String.

[Regexp#source]: https://ruby-doc.org/core-2.7.2/Regexp.html#method-i-source
[pattern]: https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/pattern
2020-11-26 17:52:26 -05:00
Tahsin Hasan 9124a9a551 Remove jquery metadata 2020-11-08 19:27:35 +06:00
Takumi Shotoku 4e097556d3
[v6.1.0.rc1] Fix regression for select tag helper with array (#40522)
* Fix regression for select tag helper with array

v6.1.0.rc1 does not generate DOM with the selected attribute
when the object's method returns an array.
This is because it has been changed by #34809 to always convert
to a string.

This commit fixes the issue.

## Steps to reproduce

```ruby
# frozen_string_literal: true

require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"

  git_source(:github) { |repo| "https://github.com/#{repo}.git" }

  gem "rails", ENV["RAILS_VERSION"]
end

require "action_view"
require "minitest/autorun"

class BugTest < ActionView::TestCase
  Post = Struct.new("Post", :tags)

  def test_stuff
    @post = Post.new
    @post.tags = ["foo", "bar"]
    expected = <<~DOM.strip
      <select name="post[tags]" id="post_tags"><option selected="selected" value="foo">foo</option>
      <option selected="selected" value="bar">bar</option>
      <option value="buz">buz</option></select>
    DOM

    assert_dom_equal(expected, select("post", "tags", %W(foo bar buz), { multiple: true }))
  end
end
```

The test succeeds on v6.0.3.4, but the test fails on v6.1.0.rc1.

* Update actionview/lib/action_view/helpers/tags/select.rb

[Takumi Shotoku + Rafael Mendonça França]
2020-11-03 13:27:51 -05:00
Eugene Kenny 2a8fe4f821 Remove an unused Action View test fixture
The test that used this was deleted in
4642ca9640.
2020-11-01 23:25:29 +00:00
Sean Doyle 99ae95fda1 ARIA attributes: treat empty Hash and Array as nil
As a follow-up to [rails/rails#40479][], ensure that empty Hash and
Array arguments are treated as `nil`.

For example, when conditionally rendering an [aria-describedby][]
attribute to associate an input with a related validation error, treat
an empty Array as `nil`, an omit the attribute entirely:

```html+erb
<% post = Post.new %>

<%= form_with model: post do |form| %>
  <%= form.text_field :title, aria: { describedby: { post_title_error: post.errors[:title].any? } } %>
  <%= tag.span(post.errors[:title].to_sentence, id: :post_title_error) if post.errors[:title].any? %>
<% end %>
```

In this example, when there are no errors, the desired outcome is for
the `<input type="text" name="post[title]">` element to _omit_ the
`[aria-describedby="post_title_error"]` attribute, and to only include
it when there are errors on the `title` attribute.

Without this change, the desired outcome can be achieved with a
combination of a `#token_list` and `#presence` call:

```diff
<% post = Post.new %>

<%= form_with model: post do |form| %>
-  <%= form.text_field :title, aria: { describedby: {post_title_error: post.errors[:title].any?} }
+  <%= form.text_field :title, aria: { describedby: token_list(post_title_error: post.errors[:title].any?).presence } %>
  <%= tag.span(post.errors[:title].to_sentence, id: :post_title_error) if post.errors[:title].any? %>
<% end %>
```

[rails/rails#40479]: https://github.com/rails/rails/pull/40479
[aria-describedby]: https://www.w3.org/WAI/WCAG21/Techniques/aria/ARIA21#example-2-identifying-errors-in-data-format
2020-10-31 16:51:18 -04:00
Sean Doyle a086418283 Yield translation to `FormBuilder#button` block
When translating a `<button>` element's contents, it is tedious  to make
the translation text available to a block scope.

For instance, when rendering a `<button type="submit">` with an SVG
element as its child, passing translated label text to that SVG
element's [`<title>`][svg-title] element requires an extra call to
`I18n.translate`.

Prior to this commit, doing so would require a double lookup of the
translation key:

```erb
<%# one time here, implicitly %>
<%= form.button do %>
  <svg>
    <title>
      <!-- one time here, explicitly -->
      <%= translate("helpers.submit.post.create") %>
    </title>
    <!-- ... -->
  </svg>
<% end %>
```

This commit modifies the `ActionView::Helpers::FormBuilder#button` to
check for invocations that are passed a block, and conditionally yield
the contents of `submit_default_value` as the argument.

The new view code might look something like this:

```erb
<%= form.button do |text| %>
  <svg>
    <title><%= text %></title>
    <!-- ... -->
  </svg>
<% end %>
```

Callers of the helper are still free to omit the block parameter.

[svg-title]: https://developer.mozilla.org/en-US/docs/Web/SVG/Element/title
2020-10-30 16:30:56 -04:00
Sean Doyle d38c214b75 Yield `Tags::Label::LabelBuilder#translations`
When translating a `<label>` element's contents, it is difficult (or
"possible", yet undocumented) to make the translation text available to
a block scope.

For instance, when rendering a `rich_text_area`, passing the
`aria-label` attribute might be important.

Prior to this commit, doing so would require a double lookup of the
translation key:

```erb
<%# one time here, implicitly %>
<%= form.label(:content) do %>
  <%= form.rich_text_area(
    :content,
    # one time here, explicitly
    "aria-label" => translate("helpers.label.post.content"),
  ) %>
<% end %>
```

The current implementation of the `#label` helper method already yields
an instance of `ActionView::Helpers::Tags::Label::LabelBuilder`, but
that class is undocumented. Instance of that class respond to
`#translation` calls, which will return the translated text content.

By aliasing `#translation` to `#to_s`, we're able to expose that value
without the burden of exposing an additional class to the public API.
Instead, view-level interpolation (either `<%= %>`, `#{ }`, or direct
calls to [`capture`][capture] will coerce the value to a String, and
implicitly invoke `#translation`.

The new view code might look something like this:

```erb
<%= form.label(:content) do |label| %>
  <%= form.rich_text_area(:content, "aria-label" => label) %>
<% end %>
```

Callers of the helper are still free to omit the block parameter.

[capture]: https://api.rubyonrails.org/classes/ActionView/Helpers/CaptureHelper.html#method-i-capture
2020-10-30 15:59:56 -04:00
Sean Doyle 67409e03a4 Alias TagHelper#class_names to #token_list
Rename the new `TagHelper#class_names` method to `TagHelper#token_list`,
and make the original available as an alias.

Inspired by a discussion on [rails/rails#40121][]

Once rendered by a browser, [an element's `class` attribute is
represented as a `DOMTokenList`][mdn-domtokenlist]. It's a
space-delimited list with unique elements.

Outside of the HTML `[class]` attribute, there are other attributes that
are represented as space-delimited lists. A non-exhaustive list
includes:

* [aria-labelledby][mdn-aria-labelledby]
* [aria-describedby][mdn-aria-describedby]
* [data-controller][data-controller] (in StimulusJS applications)
* [data-action][data-action] (in StimulusJS applications)

To support these attributes, ensure the collection of values from the
`class_names` helper is a unique Set.

[rails/rails#40121]: https://github.com/rails/rails/pull/40121#issuecomment-683366103
[class_names]: https://edgeapi.rubyonrails.org/classes/ActionView/Helpers/TagHelper.html#method-i-class_names
[mdn-aria-labelledby]: https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/ARIA_Techniques/Using_the_aria-labelledby_attribute#Value
[mdn-aria-describedby]: https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/ARIA_Techniques/Using_the_aria-describedby_attribute#Value
[data-action]: https://stimulusjs.org/reference/actions
[data-controller]: https://stimulusjs.org/reference/controllers
2020-10-30 13:20:13 -04:00
Sean Doyle 8b19d66fc6 Serialize aria- namespaced list attributes
Summary
===

Prior to this commit, calls passing `aria: { labelledby: [...] }`
serialized the `aria-labelledby` Array value as JSON.

This commit introduces special case logic to serialize `aria-` prefixed
`TrueClass`, `FalseClass`, `Hash`, and `Array` values more
appropriately.

An element's [`aria-labelledby` attribute][aria-labelledby] and
[`aria-describedby` attribute][aria-describedby] can accept a
space-delimited list of identifier values (much like the [`class`
attribute][class] accepts a space delimited [`DOMTokenList`
value][DOMTokenList]).

Similarly, there are [no boolean `aria-` attributes][aria-attributes]
(only `true`, `false`, or undefined), so this commit serializes `true`
to `"true"` and `false` to `"false"`.

Testing
---

This change moves an assertion _outside_ of a loop over `["aria",
:aria]`. Prior to this change, the second assertion within the loop
wasn't utilizing the iterated value as a Hash key. That is to say:
`aria:` (where an `aria` local variable is declared) is not equivalent
an equivalent syntax to `aria =>`.

Since the migration to `**options` in response to Ruby 2.7 deprecations,
invoking `tag.a("aria" => {...})` incorrectly coerces the `"aria" =>
{...}` has to be the `TagBuilder#a` method `content = nil` ordered
argument, instead of its `options` keyword arguments. This commit does
not modify that behavior, but it _does_ move the assertion outside the
block so that it isn't run unnecessarily.

[aria-labelledby]: https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/ARIA_Techniques/Using_the_aria-labelledby_attribute
[aria-describedby]: https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/ARIA_Techniques/Using_the_aria-describedby_attribute
[aria-attributes]: https://www.w3.org/TR/wai-aria-1.1/#propcharacteristic_value
[class]: https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/class
[DOMTokenList]: https://developer.mozilla.org/en-US/docs/Web/API/DOMTokenList
[class_names]: https://edgeapi.rubyonrails.org/classes/ActionView/Helpers/TagHelper.html#method-i-class_names
2020-10-29 21:55:33 -04:00
Rafael Mendonça França b57e227f6d
Remove deprecated `rendered_format` from `ActionView::LookupContext` 2020-10-30 00:25:26 +00:00
Rafael Mendonça França 469c4ccad7
Require that `ActionView::Base` subclasses implement `#compiled_method_container` 2020-10-30 00:25:24 +00:00
Rafael Mendonça França cd0c99c991
Make the arguments of ActionView::Base#initialize required 2020-10-30 00:25:24 +00:00
Rafael Mendonça França 05f234dbbd
Remove deprecated support to pass an object that is not a `ActionView::LookupContext` as the first argument in `ActionView::Base#initialize` 2020-10-30 00:25:23 +00:00
Rafael Mendonça França db82d4da62
Remove deprecated `ActionView::Template#refresh` 2020-10-30 00:25:21 +00:00
Rafael Mendonça França 0de9560e3e
Remove deprecated support to calling `ActionView::ViewPaths#with_fallback` with a block 2020-10-30 00:25:16 +00:00
Rafael Mendonça França 2759addf2c
Remove deprecated support to passing absolute paths to `render template:` 2020-10-30 00:25:15 +00:00
Rafael Mendonça França d52d773946
Remove deprecated support to passing relative paths to `render file:` 2020-10-30 00:25:14 +00:00
Rafael Mendonça França 4d77dcad09
Remove support to template handlers that don't accept two arguments 2020-10-30 00:25:13 +00:00
Rafael Mendonça França 320e7f7c95
Remove deprecated pattern argument in `ActionView::Template::PathResolver` 2020-10-30 00:25:13 +00:00
Rafael Mendonça França 5b8fe6c37e
Remove deprecated support to call private methods from object in some view helpers 2020-10-30 00:25:11 +00:00
John Hawthorn e3130f1b84
Revert "Raise when calling render with invalid options" 2020-10-29 13:49:30 -07:00
John Hawthorn e53e2a006b Validate arguments to render calls 2020-10-28 19:30:48 -07:00
John Hawthorn f94d715ad5 Fix key names in abstract_controller_test.rb
These were renamed :action and :layout in 2010
2020-10-28 14:54:20 -07:00
John Hawthorn 8c8707f303 Fix invalid layouts: key in test 2020-10-28 14:54:20 -07:00
Robin Drexler 815e724bea set approriate crossorigin for js and css preload links 2020-10-21 18:26:41 -04:00
Ryuta Kamizono 4194565ddf Remove .DS_Store 2020-10-16 15:23:45 +09:00
Joel Hawksley 136b814144 Include layout when rendering objects from controllers
In https://github.com/rails/rails/pull/37919, support
for rendering objects that respond_to render_in in
controllers was added. However, the implementation
did not support layouts.

This change updates the implementation from #37919
to more closely match the rest of the
ActionView::Template classes, enabling the use of layouts.

Co-authored-by: Felipe Sateler <fsateler@gmail.com>
2020-10-14 11:49:45 -06:00
Aaron Patterson b001cbefa7
Don't evaluate ERB in the ujs test server 2020-10-06 12:14:22 -07:00
Joel Hawksley 36b01da797 Do not add newlines when annotating rendered views
When rendering a view with annotate_rendered_view_with_filenames
enabled, we were inserting newlines after the BEGIN and END
comments. These newlines are not ideal in some use cases, such
as rendering templates to strings that are then passed around
our application.

As the newlines were added for aesthetic purposes, we think it
makes sense to remove them so that rendered template strings do
not contain extraneous newlines.

As a result of removing these newlines, the annotations
no longer affect the reporting of template error on the correct
line, as addressed in #38950. As such, we've reverted those
changes as well.

Co-authored-by: Chris Gavin <chrisgavin@github.com>
2020-10-06 11:29:36 -06:00
Sean Doyle af6f4be040 Ensure `tag.with_options({}).p` builds a `<p>`
Prior to this change, the following call raises:

```ruby
with_options(id: "with-options") { |t| t.p "content" }

```

The `ActionView::Helpers::TagHelper::TagBuilder` implementation relies
on `method_missing` to dispatch calls to `tag_string` where the missing
method name is the resulting element's tagName. Unfortunately,
[`Kernel#p` already exists][Kernel#p] and is invoked before
`method_missing` can intervene.

This commit rectifies this by declaring `TagBuilder#p` and overriding
the existent `#p` instance method.

[Kernel#p]: https://ruby-doc.org/core-2.7.2/Kernel.html#method-i-p
2020-10-05 18:41:49 -04:00
Eugene Kenny 0b244ff44c
Merge pull request #39989 from jonathanhefner/translate-refactor
Improve Action View `translate` helper
2020-09-16 17:01:45 +01:00
Jonathan Hefner d81926fdac Improve Action View `translate` helper
This disentangles the control flow between Action View's `translate` and
I18n's `translate`.  In doing so, it fixes a handful of corner cases,
for which tests have now been added.  It also reduces memory
allocations, and improves speed when using a default:

**Memory**

```ruby
require "benchmark/memory"

Benchmark.memory do |x|
  x.report("warmup") { translate(:"translations.foo"); translate(:"translations.html") }
  x.report("text") { translate(:"translations.foo") }
  x.report("html") { translate(:"translations.html") }
  x.report("text 1 default") { translate(:"translations.missing", default: :"translations.foo") }
  x.report("html 1 default") { translate(:"translations.missing", default: :"translations.html") }
  x.report("text 2 defaults") { translate(:"translations.missing", default: [:"translations.missing", :"translations.foo"]) }
  x.report("html 2 defaults") { translate(:"translations.missing", default: [:"translations.missing", :"translations.html"]) }
end
```

Before:

```
                text     1.240k memsize (     0.000  retained)
                        13.000  objects (     0.000  retained)
                         2.000  strings (     0.000  retained)
                html     1.600k memsize (     0.000  retained)
                        19.000  objects (     0.000  retained)
                         2.000  strings (     0.000  retained)
      text 1 default     4.728k memsize (     1.200k retained)
                        39.000  objects (     4.000  retained)
                         5.000  strings (     0.000  retained)
      html 1 default     5.056k memsize (     1.160k retained)
                        41.000  objects (     3.000  retained)
                         4.000  strings (     0.000  retained)
     text 2 defaults     7.464k memsize (     2.392k retained)
                        54.000  objects (     6.000  retained)
                         4.000  strings (     0.000  retained)
     html 2 defaults     7.944k memsize (     2.384k retained)
                        60.000  objects (     6.000  retained)
                         4.000  strings (     0.000  retained)
```

After:

```
                text   952.000  memsize (     0.000  retained)
                         9.000  objects (     0.000  retained)
                         1.000  strings (     0.000  retained)
                html     1.008k memsize (     0.000  retained)
                        10.000  objects (     0.000  retained)
                         1.000  strings (     0.000  retained)
      text 1 default     2.400k memsize (    40.000  retained)
                        24.000  objects (     1.000  retained)
                         4.000  strings (     0.000  retained)
      html 1 default     2.464k memsize (     0.000  retained)
                        22.000  objects (     0.000  retained)
                         2.000  strings (     0.000  retained)
     text 2 defaults     3.232k memsize (     0.000  retained)
                        30.000  objects (     0.000  retained)
                         2.000  strings (     0.000  retained)
     html 2 defaults     3.456k memsize (     0.000  retained)
                        32.000  objects (     0.000  retained)
                         2.000  strings (     0.000  retained)
```

**Speed**

```ruby
require "benchmark/ips"

Benchmark.ips do |x|
  x.report("text") { translate(:"translations.foo") }
  x.report("html") { translate(:"translations.html") }
  x.report("text 1 default") { translate(:"translations.missing", default: :"translations.foo") }
  x.report("html 1 default") { translate(:"translations.missing", default: :"translations.html") }
  x.report("text 2 defaults") { translate(:"translations.missing", default: [:"translations.missing", :"translations.foo"]) }
  x.report("html 2 defaults") { translate(:"translations.missing", default: [:"translations.missing", :"translations.html"]) }
end
```

Before:

```
                text     35.685k (± 0.7%) i/s -    179.050k in   5.017773s
                html     28.569k (± 3.1%) i/s -    143.871k in   5.040128s
      text 1 default     13.953k (± 2.0%) i/s -     70.737k in   5.071651s
      html 1 default     12.507k (± 0.4%) i/s -     63.546k in   5.080908s
     text 2 defaults      9.103k (± 0.3%) i/s -     46.308k in   5.087323s
     html 2 defaults      8.570k (± 4.3%) i/s -     43.071k in   5.034322s
```

After:

```
                text     36.694k (± 2.0%) i/s -    186.864k in   5.094367s
                html     30.415k (± 0.5%) i/s -    152.900k in   5.027226s
      text 1 default     18.095k (± 2.7%) i/s -     91.086k in   5.036857s
      html 1 default     15.934k (± 1.7%) i/s -     80.223k in   5.036085s
     text 2 defaults     12.179k (± 0.6%) i/s -     61.659k in   5.062910s
     html 2 defaults     11.193k (± 2.1%) i/s -     56.406k in   5.041433s
```
2020-09-16 10:18:54 -05:00
Ryuta Kamizono c848baffd2 ✂️ [ci skip]
895134bcff (diff-7789e18ec0a61c6845755a257e6eb34fR3)
ee9e308f68 (diff-5e9551294914b338d923032fa904c6beR103)
5f63c771f7 (diff-600d5368b55e46ed961abb4295977ac3R506)
48b6bacbc5 (diff-279ac5c088a3ee7e9f954bbc10d1b773R246)
49adb7f4c6 (diff-0cbe7171fdd7821fd5836896849141c0R111)
9bfe89e68e (diff-9c1c95b001e3680ba8a473cf23508b68R8)
a0f18e6090 (diff-bef0ae2c16dacf77bfd2ba099af8a330R47)
8b4d344815 (diff-09660f59aee7ec893ad609315e8843c5R18)
49d1b5a98d (diff-531e71a9d5fdc8181f38ce4416553694R107)
b674f04756 (diff-7521c0bb452244663b689e77658e63e3R98)
a9012af688 (diff-60bdcf1e1954ec56d41fc4c7bd8d3e39R512)
6380aee182 (diff-5158c234d5e19bb1cffadcbc088400f1R262)
c7820d8124 (diff-f4614e7ba8081c0c9e20fe64b6374228R60)
8df7ed3b88 (diff-0495ed68f82d1bbde593ab5491eab24aR169)
6f94260ac6 (diff-7ecb6672f3fd37dfb79c4539395d3857R28)
03e44f9300 (diff-e9234c4c6668852448059c0a35840bcaR185)
2020-09-16 09:52:10 +09:00
Akira Matsuda 47f5459138 Module#const_set is a public method 2020-09-15 17:19:37 +09:00
Jonathan Hefner 98a76a50ed Refactor Action View `translate` helper
This refactor incidentally fixes a corner case when `translate` is
called with a block, the translation is missing, and
`debug_missing_translation` is false.

This commit adds a test for the above corner case, and additional tests
for existing behavior.
2020-09-11 11:56:17 -05:00
Jonathan Hefner e663f08446
Merge pull request from GHSA-cfjv-5498-mph5
Prior to this commit, when a translation key indicated that the
translation text was HTML, the value returned by `I18n.translate` would
always be marked as `html_safe`.  However, the value returned by
`I18n.translate` could be an untrusted value directly from
`options[:default]`.

This commit ensures values directly from `options[:default]` are not
marked as `html_safe`.
2020-09-09 14:41:31 -04:00
igor04 a0fe1e77ad Do not use disable_with when it set to false
If automatically_disable_submit_tag is set to false then disable_with
is ignored, as result in all cases where disable_with is explicitly set
to false will produce unexpected result
2020-09-03 19:00:39 +03:00
John Hawthorn 65e42c91f5 Use unique controller per ActionView::TestCase
Previously the same class, ActionView::TestCase::TestController, was
used to build a controller for every ActionView::TestCase class.

This caused issues when helpers/helper methods were set directly on the
controller (which from one test we seem to want to support).

This commit solves this by creating a new controller class for every
test case, which gives the controller a unique set of helpers to work
with.

Co-authored-by: John Crepezzi <seejohnrun@github.com>
2020-09-02 15:55:01 -07:00
Sean Doyle 7cf52ae981
Extend `ActionView::Helpers#translate` to yield
This commit extends the `ActionView::Helpers#translate` (and by way of
alias, `#t`) helper methods to accept blocks.

When invoked with a block, the `translate` call will yield the
translated text as its first block argument, along with the resolved
translation key as its second:

```erb
<%= translate(".key") do |translation, resolved_key| %>
  <span data-i18n-key="<%= resolved_key %>"><%= translation %></span>
<% end %>
```

In cases where relative translation keys are foregone in lieu of fully
qualified keys, or if the caller is not interested in the resolved key,
the second block argument can be omitted:

```erb
<%= translate("action.template.key") do |translation| %>
  <p><%= translation %></p>
  <p><%= translation %>, but a second time</p>
<% end %>
```

A benefit of yielding the translation is that it enabled template-local
variable re-use. Alternatively, [`Object#tap`][tap] could be used.

Prior to this commit, however, the resolution of the translation key was
internal to `ActionView`, and unavailable to the caller (unless they
were willing to explicitly determine the resolved key themselves). By
making it available as a block parameter, it could be used to annotate
the translated value in the resulting elements.

[tap]: https://ruby-doc.org/core-2.7.0/Object.html#method-i-tap
2020-08-26 20:46:33 +00:00
Jonathan Fleckenstein 0b65e23978 Allow callers to remove nopush 2020-08-23 09:53:42 -04:00
Jonathan Fleckenstein 033ac18590 don't preload for defered scripts and disable push 2020-08-22 20:59:29 -04:00
Eileen M. Uchitelle 443c8470d1
Merge pull request #39939 from Shopify/link-preload-headers
Automatically set Link header for each stylesheet and script
2020-08-17 08:37:22 -04:00
Petrik a7ed92bd1d Use superclass name in inspect of ActionView::Base.with_empty_template_cache
When rendering views an anonymous subclass is created by calling
ActionView::Base.with_empty_template_cache.
This causes inspect to return an unhelpful description when calling
inspect:
`#<#<Class:0x012345012345>:<0x012345012345>`.

This can be confusing when exceptions are raised because it's hard to
figure out where to look. For example calling an undefined method in a
template would raise the following exception:

    undefined method `undefined' for #<#<Class:0x012345012345>:<0x012345012345>

Instead we can return the non-anonymous superclass name.

    undefined method `undefined' for #<ActionView::Base:0x01234502345>

The anonymous class is created in ActionView::Base.with_empty_template_cache.
See f9bea6304d
This seems to be done for performance reasons only, without expecting a
change to calling `inspect`.
2020-08-15 11:04:28 +02:00
Yasuo Honda 2d43d47786 Address `ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true`
This pull request fixes #38697

It is caused by `@controller.singleton_class.include @routes.url_helpers` when `@controller` is nil in `ActionController::TestCase`.

* Without this commit

```ruby
% cd actionview
% PARALLEL_WORKERS=1 bin/test test/actionpack/controller/layout_test.rb test/template/url_helper_test.rb --seed 16702 -n "/^(?:LayoutSetInResponseTest#(?:test_layout_symbol_set_in_controller_returning_nil_falls_back_to_default)|UrlHelperTest#(?:test_url_for_with_array_and_only_path_set_to_false))$/"

Run options: --seed 16702 -n "/^(?:LayoutSetInResponseTest#(?:test_layout_symbol_set_in_controller_returning_nil_falls_back_to_default)|UrlHelperTest#(?:test_url_for_with_array_and_only_path_set_to_false))$/"

.E

Error:
UrlHelperTest#test_url_for_with_array_and_only_path_set_to_false:
ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true
    /Users/yahonda/src/github.com/rails/rails/actionpack/lib/action_dispatch/http/url.rb:64:in `full_url_for'
    /Users/yahonda/src/github.com/rails/rails/actionpack/lib/action_dispatch/http/url.rb:54:in `url_for'
    /Users/yahonda/src/github.com/rails/rails/actionpack/lib/action_dispatch/routing/route_set.rb:333:in `block in <class:RouteSet>'
    /Users/yahonda/src/github.com/rails/rails/actionpack/lib/action_dispatch/routing/route_set.rb:838:in `url_for'
    /Users/yahonda/src/github.com/rails/rails/actionpack/lib/action_dispatch/routing/route_set.rb:270:in `call'
    /Users/yahonda/src/github.com/rails/rails/actionpack/lib/action_dispatch/routing/route_set.rb:213:in `call'
    /Users/yahonda/src/github.com/rails/rails/actionpack/lib/action_dispatch/routing/route_set.rb:326:in `block in define_url_helper'
    /Users/yahonda/src/github.com/rails/rails/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb:233:in `polymorphic_method'
    /Users/yahonda/src/github.com/rails/rails/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb:116:in `polymorphic_url'
    /Users/yahonda/src/github.com/rails/rails/actionview/lib/action_view/routing_url_for.rb:104:in `url_for'
    /Users/yahonda/src/github.com/rails/rails/actionview/test/template/url_helper_test.rb:102:in `test_url_for_with_array_and_only_path_set_to_false'

bin/test test/template/url_helper_test.rb:100

Finished in 0.042275s, 47.3093 runs/s, 47.3093 assertions/s.
2 runs, 2 assertions, 0 failures, 1 errors, 0 skips
%
```
2020-08-12 19:43:02 +09:00
Yasuo Honda 1e84ac4290 Clear Resolvers' cache after AVLogSubscriberTest tests
Addresses the build failure below:
https://buildkite.com/rails/rails/builds/70853#29647167-4db2-4810-810e-d2c3a4e6dcf8

* This pull request addresses this error.

```
$ bin/test test/template/log_subscriber_test.rb test/template/test_case_test.rb -n "/^(?:AVLogSubscriberTest#(?:test_render_collection_template)|ActionView::HelperInclusionTest#(?:test_helper_class_that_is_being_tested_is_always_included_in_view_instance))$/" --seed 54118
Run options: -n "/^(?:AVLogSubscriberTest#(?:test_render_collection_template)|ActionView::HelperInclusionTest#(?:test_helper_class_that_is_being_tested_is_always_included_in_view_instance))$/" --seed 54118

.E

Error:
ActionView::HelperInclusionTest#test_helper_class_that_is_being_tested_is_always_included_in_view_instance:
ActionView::Template::Error: undefined method `_test__customer_erb___3224798146126036163_10260' for #<#<Class:0x00005653f1b35768>:0x00005653f1b348e0>
    /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/base.rb:276:in `_run'
    /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/template.rb:182:in `block in render'
    /home/yahonda/src/github.com/rails/rails/activesupport/lib/active_support/notifications.rb:205:in `instrument'
    /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/template.rb:384:in `instrument_render_template'
    /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/template.rb:180:in `render'
    /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/renderer/collection_renderer.rb:186:in `block in collection_with_template'
    /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/renderer/collection_renderer.rb:66:in `block in each_with_info'
    /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/renderer/collection_renderer.rb:66:in `each'
    /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/renderer/collection_renderer.rb:66:in `each_with_info'
    /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/renderer/collection_renderer.rb:177:in `each'
    /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/renderer/collection_renderer.rb:177:in `map'
    /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/renderer/collection_renderer.rb:177:in `collection_with_template'
    /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/renderer/collection_renderer.rb:159:in `block (2 levels) in render_collection'
    /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/renderer/partial_renderer/collection_caching.rb:21:in `cache_collection_render'
    /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/renderer/collection_renderer.rb:158:in `block in render_collection'
    /home/yahonda/src/github.com/rails/rails/activesupport/lib/active_support/notifications.rb:203:in `block in instrument'
    /home/yahonda/src/github.com/rails/rails/activesupport/lib/active_support/notifications/instrumenter.rb:24:in `instrument'
    /home/yahonda/src/github.com/rails/rails/activesupport/lib/active_support/notifications.rb:203:in `instrument'
    /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/renderer/collection_renderer.rb:144:in `render_collection'
    /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/renderer/collection_renderer.rb:116:in `render_collection_with_partial'
    /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/renderer/renderer.rb:75:in `render_partial_to_object'
    /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/renderer/renderer.rb:27:in `render_to_object'
    /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/renderer/renderer.rb:22:in `render'
    /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/helpers/rendering_helper.rb:38:in `block in render'
    /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/base.rb:306:in `in_rendering_context'
    /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/helpers/rendering_helper.rb:34:in `render'
    /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/test_case.rb:205:in `render'
    /home/yahonda/src/github.com/rails/rails/actionview/test/template/test_case_test.rb:126:in `render_from_helper'
    /home/yahonda/src/github.com/rails/rails/actionview/test/fixtures/test/_from_helper.erb:1:in `__home_yahonda_src_github_com_rails_rails_actionview_test_fixtures_test__from_helper_erb___900298654238864924_10340'
    /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/base.rb:276:in `_run'
    /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/template.rb:182:in `block in render'
    /home/yahonda/src/github.com/rails/rails/activesupport/lib/active_support/notifications.rb:205:in `instrument'
    /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/template.rb:384:in `instrument_render_template'
    /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/template.rb:180:in `render'
    /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/renderer/partial_renderer.rb:285:in `block in render_partial_template'
    /home/yahonda/src/github.com/rails/rails/activesupport/lib/active_support/notifications.rb:203:in `block in instrument'
    /home/yahonda/src/github.com/rails/rails/activesupport/lib/active_support/notifications/instrumenter.rb:24:in `instrument'
    /home/yahonda/src/github.com/rails/rails/activesupport/lib/active_support/notifications.rb:203:in `instrument'
    /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/renderer/partial_renderer.rb:280:in `render_partial_template'
    /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/renderer/partial_renderer.rb:271:in `render'
    /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/renderer/renderer.rb:84:in `render_partial_to_object'
    /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/renderer/renderer.rb:27:in `render_to_object'
    /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/renderer/renderer.rb:22:in `render'
    /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/helpers/rendering_helper.rb:38:in `block in render'
    /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/base.rb:306:in `in_rendering_context'
    /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/helpers/rendering_helper.rb:34:in `render'
    /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/test_case.rb:205:in `render'
    /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/test_case.rb:122:in `render'
    /home/yahonda/src/github.com/rails/rails/actionview/test/template/test_case_test.rb:136:in `block in <class:HelperInclusionTest>'

bin/test test/template/test_case_test.rb:132

Finished in 0.076637s, 26.0970 runs/s, 39.1454 assertions/s.
2 runs, 3 assertions, 0 failures, 1 errors, 0 skips
$
```

Refer #36189 for the similar fix
2020-07-30 12:14:10 +09:00
Jean Boussier a9012af688 Automatically set Link header for each stylesheet and script
<link rel="preload"> elements[0] can be serialized in `Link`
headers[1] to allow the browser to preload them before
it parsed the HTML body.

It is particularly useful for scripts included at the bottom
of the document.

[0] https://developer.mozilla.org/en-US/docs/Web/HTML/Preloading_content
[1] https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Link
2020-07-28 11:57:36 +02:00
Jonathan Hefner e6950a332a Add label attribute to <option> from include_blank
The `:include_blank` option of various `<select>`-related helpers causes
an `<option>` element with no content to be rendered.  However, the
[HTML spec] says that unless an `<option>` element has a `label`
attribute (which must be non-empty), its content must be "Text that is
not inter-element whitespace."

In #24923, this issue was addressed for `select_tag` by adding a `label`
attribute to the `<option>`.  This commit addresses the issue in the
same manner for `FormBuilder#select` and various date / time select
helpers.

[HTML spec]: https://html.spec.whatwg.org/multipage/form-elements.html#the-option-element
2020-07-24 15:45:18 -05:00
Ryuta Kamizono 528b62e386 Address to false negative for Performance/DeletePrefix,DeleteSuffix
Follow up to c07dff7227.

Actually it is not the cop's fault, but we mistakenly use `^`, `$`, and
`\Z` in much places, the cop doesn't correct those conservatively.

I've checked all those usage and replaced all safe ones.
2020-06-14 13:04:47 +09:00
Eugene Kenny 5888c359e4 Add test for HTML format fallback from JS template
When rendering a template with an explicit JS format, typically via
`respond_to :js`, we want to be able to render HTML partials without
having to specify their format, in order to make SJR more ergonomic.
2020-05-29 22:07:17 +01:00
Eugene Kenny 8ae6626b65 Override clear_cache in custom path resolver
https://buildkite.com/rails/rails/builds/69620#ffaa78f1-aba4-42b9-91c8-0fe5d333263a

Since 096d143c8c, when the tests run in
parallel it's possible for `ActionView::LookupContext::DetailsKey.clear`
to be called while this test is running, which wouldn't work correctly.
2020-05-27 21:07:58 +01:00
John Hawthorn db543ba728
Merge pull request #39361 from jhawthorn/path_parser
Introduce Resolver::PathParser
2020-05-26 20:16:18 -07:00
Aaron Lipman dd7a673782
Remove redundant @virtual_path variable
Following the introduction of the @current_template variable in 1581cab,
the @virtual_path variable is now redundant, as the value of the virtual
path may be accessed via @current_template.virtual_path. This commit
removes @virtual_path and replaces any references to @virtual_path with
@current_template.virtual_path.
2020-05-26 15:53:11 -04:00
Aaron Lipman 4671fe2040
Ensure cache fragment digests include all templates
A Rails view may rely on several templates (e.g. layouts and partials)
in addition to the template for the action being rendered (e.g.
"show.html.erb"). To track which view file is currently being rendered
for the purpose of generating template tree digests used in cache
fragment keys, Action View uses a stack, the top item of which is
accessed via the @current_template variable (introduced in 1581cab).

Consider the following template:

    <!-- home.html.erb -->
    <%= render layout: "wrapper" do %>
      <%= cache "foo" %>
        HOME
      <%= end %>
    <%= end %>

Inside the block passed to the render helper, @current_template
corresponds to the wrapper.html.erb template instead of home.html.erb.
As wrapper.html.erb is then used as the root node for generating the
template tree digest used in the cache fragment key, the cache fragment
fails to expire upon changes to home.html.erb. Additionally, should a
second template use the wrapper.html.erb layout and contain a cache
fragment with the same key, the cache fragment keys for both templates
will be identical - causing cached content to "leak" from one view to
another (as described in #38984).

This commit skips adding templates to the stack when rendered as a
layout with a block via the render helper, ensuring correct and unique
cache fragment digests. Additionally, the virtual_path keyword arguments
found in CacheHelper and all references to the are removed as they no
longer possess any function. (Following the introduction of
@current_template, virtual_path is accessed via
@current_template.virtual_path rather than as a standalone variable.)
2020-05-26 15:52:05 -04:00
Jonathan Hefner cd2b3be9a6 Allow Erubi bufvar to be configured
This allows `render_in` components that compile their own templates to
use their view context's current output buffer while a
`with_output_buffer` block is being evaluated.

Partially addresses #39377.
2020-05-21 15:22:51 -05:00
John Hawthorn e53c45b80c Introduce Resolver::PathParser
The template resolver is supposed to determine the handler, format, and
variant from the path in extract_handler_and_format_and_variant.

Previously this behaviour was close but didn't exactly match the
behaviour of finding templates, and in some cases (particularly with
handlers or formats missing) would return incorrect results.

This commit introduces Resolver::PathParser, a class which should be
able to accurately from any path inside a view directory be able to tell
us exactly the prefix, partial, variant, locale, format, variant, and
handler of that template.

This works by building a building a regexp from the known handlers and
file types. This requires that any resolvers have their cache cleared
when new handlers or types are registered (this was already somewhat the
requirement, since resolver lookups are cached, but this makes it
necessary in more situations).
2020-05-19 18:55:10 -07:00
John Hawthorn 8762d93afc Add tests of resolver variant handling
The template resolver is supposed to determine the handler, format, and
variant from the path in extract_handler_and_format_and_variant.

This commit adds tests for how variants are parsed from the filenames
found in the resolver. It includes two skipped tests which currently fail.
2020-05-19 18:55:10 -07:00
John Hawthorn dc778f83d7 Only use valid handlers in resolver_shared_tests
We don't have jbuilder installed (this would work if it was).
2020-05-19 18:55:10 -07:00
Prathamesh Sonpatki a673ce69e7
Rename `annotate_template_file_names` to `annotate_rendered_view_with_filenames`
- Add the configuration option for annotating templates with file names to the generated app.
- Add `annotate_rendered_view_with_filenames` option to configuring guide.
2020-05-19 09:28:14 +05:30
John Hawthorn dd9991bac5 Deprecate rendering templates with . in the name
Allowing templates with "." introduces some ambiguity. Is index.html.erb
a template named "index" with format "html", or is it a template named
"index.html" without a format? We know it's probably the former, but if
we asked ActionView to render "index.html" we would currently get some
combination of the two: a Template with index.html as the name and
virtual path, but with html as the format.

This deprecates having "." anywhere in the template's name, we should
reserve this character for specifying formats. I think in 99% of cases
this will be people specifying `index.html` instead of simply `index`.

This was actually once deprecated in the 3.x series (removed in
6c57177f2c) but I don't think we can rely
on nobody having introduced this in the past 8 years.
2020-05-05 23:21:17 -07:00
John Hawthorn 92d3afe475
Merge pull request #39068 from jhawthorn/dependency_tracker_interpolation
Ignore interpolated strings in DependencyTracker
2020-05-05 23:21:00 -07:00
Godfrey Chan f4538aa586
Merge pull request #38999 from zvkemp/action-view-instrument-layout
add additional instrumentation block for ActionView layout rendering
2020-05-05 11:50:24 -07:00
Ryuta Kamizono 4642ca9640 Fix actionview tests for missing helper 2020-05-03 06:06:09 +09:00
zvkemp 6380aee182 add additional instrumentation block for ActionView layout rendering 2020-05-01 19:15:54 -07:00
John Hawthorn 1e25f02135 Ignore interpolated strings in DependencyTracker
ActionView::DependencyTracker looks through ERB templates using a regex
to find render calls. Previously this would incorrectly pick up
interpolated strings, like `render "foo/#{bar}"`.

This does not attempt to completely correct DependencyTracker, we can't
parse Ruby accurately with a regex, but should avoid a relatively common
case that previously was generating warnings.
2020-04-27 10:10:45 -07:00
Godfrey Chan 1064c51609 Fix typos [ci skip]
I wrote this shell script to find words from the Rails repo,
so I can paste them into https://www.horsepaste.com/ for
the [codenames game](https://en.m.wikipedia.org/wiki/Codenames_(board_game)).

```bash
git grep -Il '' | \
  grep -v -E "CHANGELOG|Gemfile|gemspec|package\.json|yarn\.lock" | \
  xargs cat | \
  sed '/[^ ]\{10,\}/d' | \
  sed 's/\([A-Z]\)/ \1/g' | \
  tr 'A-Z' 'a-z' | \
  tr -c -s 'a-z' '\n' | \
  sed '/^.\{0,3\}$/d' | \
  sort | \
  uniq | \
  tr '\n' ',' | \
  pbcopy
```

You can see the result in https://www.horsepaste.com/rails-fixed.
Click "Next game" to cycle the words.

Found some typos in the codebase from this 😂

This is how I generated the list of possible typos:

```bash
git grep -Il '' | \
  grep -v -E "CHANGELOG|Gemfile|gemspec|package\.json|yarn\.lock" | \
  xargs cat | \
  sed '/[^ ]\{10,\}/d' | \
  sed 's/\([A-Z]\)/ \1/g' | \
  tr 'A-Z' 'a-z' | \
  tr -c -s 'a-z' '\n' | \
  sed '/^.\{0,3\}$/d' | \
  sort | \
  uniq | \
  aspell --ignore-case list
```

I manually reviewed the list and made the corrections
in this commit. The rest on the list are either:

* Bugs in my script: it split things like "doesn't" into
  "doesn" and "t", if find things like `#ffffff` and
  extracts "ffffff" as a word, etc
* British spelling: honour, optimised
* Foreign words: bonjour, espanol
* Names: nginx, hanekawa
* Technical words: mutex, xhtml
* Portmanteau words: autosave, nodelist
* Invented words: camelize, coachee
* Shortened words: attrs, repo
* Deliberate typos: hllo, hillo (used in code examples, etc)
* Lorem ipsum words: arcu, euismod

This is the [output](https://gist.github.com/chancancode/eb0b573d667dc31906f33f1fb0b22313)
of the script *after* fixing the typos included in this
commit. In theory, someone can run that command again in
the future and compare the output to catch new typos (i.e.
using my list to filter out known typos).

Limitations: the aspell dictionary could be wrong, I
could have miss things, and my script ignores words that
are less than 3 characters or longer than 10 characters.
2020-04-22 21:43:37 -07:00
David Heinemeier Hansson 876cc27609
Revert "Deprecate rendering templates with . in the name" (#39012) 2020-04-21 15:59:46 -07:00
Joel Hawksley ab8ea48111 Raise errors on correct line with annotations enabled
Co-authored-by: Aaron Patterson <tenderlove@github.com>
2020-04-14 15:26:26 -06:00
Joel Hawksley 3b28486e93 `ActionView::Base.annotate_template_file_names` only annotates HTML output.
In testing https://github.com/rails/rails/pull/38848 in the
GitHub monolith, we realized that we probably should only
be annotating HTML output with these comments, at least
in their current format. By passing `format` to
`erb_implementation`, we set ourselves up to eventually
support annotations for other formats as well.
2020-04-08 15:22:22 -06:00
John Hawthorn f84773a587 Deprecate rendering templates with . in the name
Allowing templates with "." introduces some ambiguity. Is index.html.erb
a template named "index" with format "html", or is it a template named
"index.html" without a format? We know it's probably the former, but if
we asked ActionView to render "index.html" we would currently get some
combination of the two: a Template with index.html as the name and
virtual path, but with html as the format.

This deprecates having "." anywhere in the template's name, we should
reserve this character for specifying formats. I think in 99% of cases
this will be people specifying `index.html` instead of simply `index`.

This was actually once deprecated in the 3.x series (removed in
6c57177f2c) but I don't think we can rely
on nobody having introduced this in the past 8 years.
2020-03-31 21:55:19 -07:00
Joel Hawksley a59e1de26a .annotate_template_file_names annotates HTML output with template file names
As a developer, when looking at a page in my web browser, it's sometimes
difficult to figure out which template(s) are being used to render the page.

config.action_view.annotate_template_file_names adds HTML comments to the
rendered output indicating where each template begins and ends.

Co-authored-by: Aaron Patterson <tenderlove@github.com>
2020-03-30 14:50:01 -06:00
Aaron Patterson fc4ef77d47
Merge pull request #38594 from rails/collection-refactor
Refactoring PartialRenderer
2020-03-23 17:41:20 -07:00
Aaron Patterson 033a738817
Fix possible XSS vector in JS escape helper
This commit escapes dollar signs and backticks to prevent JS XSS issues
when using the `j` or `javascript_escape` helper

CVE-2020-5267
2020-03-19 09:48:08 -07:00
Joel Hawksley c82a9193df
Document view components support (#38656) 2020-03-13 23:52:49 +01:00
Eugene Kenny d3599d8aff Use index_by and index_with wherever possible
Using `index_by` or `index_with` is more concise than `each_with_object`
and more performant than `map { ... }.to_h` or `Hash[map { ... }]`.
2020-03-05 01:24:14 +00:00
Aaron Patterson 49adb7f4c6
pull preloading behavior in to the collection renderer 2020-02-28 09:23:22 -08:00
Aaron Patterson 232372bfd1
Merge branch 'master' into collection-refactor
* master:
  Add a regression test that ActionText caught
  [ci skip] Use yml extension for locale files
  Fix `helper_method` in `ActionView::TestCase` to allow keyword arguments
  Fix `delegate_missing_to` to allow keyword arguments
  Dump the schema or structure of a database when calling db:migrate:name
  Reset the `ActiveRecord::Base` connection after `rails db:migrate:name`
  Fix `unscope` when an `eq` node which has no arel attribute
  Remove unused argument
  Disallow calling `connected_to` on subclasses of `ActiveRecord::Base`
  More less and lazy allocation for `assign_attributes` and `_assign_attributes`
  Tweak contributing_to_ruby_on_rails.md [ci skip]
  Clarify the difference between (old) `spec_name` and `connection_specification_name`
  Remove duplicate part from deprecation warning
  Fix deprecation warnings in connection_handlers_sharding_db_test.rb
  Fixup CHANGELOGs [ci skip]
  `reset_column_information` does not reset @predicate_builder
  Simplify FixtureResolver to reuse filtering logic
  Mostly remove bad test
  Use type attribute in ActionView::Helpers::JavaScriptHelper#javascript_tag example
  Update some references to finder options [ci skip]
2020-02-27 16:48:15 -08:00
Aaron Patterson 5f596e299e
Add a regression test that ActionText caught
ActionText is using ActionView in a way that ActionView doesn't have in
it's test suite.  This is just a regression test to hit that same use
case.
2020-02-27 16:47:22 -08:00
Aaron Patterson 374c1b2940
fix require 2020-02-27 14:12:09 -08:00
Ryuta Kamizono 4d641a3363 Fix `helper_method` in `ActionView::TestCase` to allow keyword arguments 2020-02-28 04:46:05 +09:00
Aaron Patterson f0a1548433
as: doesn't make sense without an object parameter
The local variable name in `as:` may not be a valid local variable name,
but if there is no object specified to be assigned to the parameter,
then why supply the `as:`?  This commit adds an object for the as param
2020-02-26 16:13:12 -08:00
Aaron Patterson 0c1fbc7e04
collection rendering has its own class 2020-02-26 14:51:31 -08:00
John Hawthorn a9e455f484 Mostly remove bad test
This test was attempting to test how cache keys work by modifying the
templates and seeing when that cache was fresh. This doesn't actually
work for real Resolvers, only FixtureResolver, and isn't desirable. We
absolutely want to share templates if they resolve to the same file.

Instead, this simplifies the test to only check that we get the correct
template for the locale we request.
2020-02-24 16:20:33 -08:00
Steve Thomas ddcc3212c0 Update boolean attributes for actionview tag helper 2020-02-24 10:23:30 -05:00
Joel Hawksley 05a2a939f0 Remove validations from example components
We're moving away from using validations in our
component framework, and feel that it's better
to avoid prescribing their usage in these
example classes, which exist to serve as example
objects that are compatible with the render_in
API.
2020-02-20 09:48:25 -07:00
aminamos 7bb0706f2c update from PR #36222 2020-02-12 13:31:43 -05:00
Stefan Wrobel 8877b5ff16
Fix translate method with default: nil
```ruby
    I18n.translate('missing.translation', default: nil)
    # => nil
    helper.translate('missing.translation', default: nil)
    # Before
    # => "<span class=\"translation_missing\" title=\"translation missing: en.missing.translation\">Translation</span>"
    # After
    # => nil
    ```
2020-02-09 23:22:00 +01:00
Ryuta Kamizono b803ed012c Make `localize` helper takes keyword arguments the same with `I18n.localize` 2020-02-05 18:49:44 +09:00
Ryuta Kamizono 6c02fee08f Make `translate` helper takes keyword arguments the same with `I18n.translate` 2020-02-05 18:04:27 +09:00
Iago Pimenta e7c3eb11e0 Use default order of PathResolver::EXTENSIONS for sort templates 2020-01-09 12:01:41 -08:00
John Hawthorn cd0fbaea01 Define fake_zones on AS::TimeZone, not singleton
Previously this was incorrectly defining this attribute on the singleton
class, which would end up actually using the class variable from Module.
2020-01-04 17:50:59 -08:00
Josef Šimánek 64f3bd0a8d
Use travel_to to in date_helper_test.rb. 2020-01-02 02:11:33 +01:00
Rafael Mendonça França 855cb4bef2
Call method outside the loop
We only need to call this method once.
2019-12-27 15:13:44 -03:00
John Hawthorn 5579871687 Use ActionDispatch mimes in asset_tag_helper_test
We need a more complete understanding of mime types for the asset tags
to render properly, so we need to load this rather than just the stubs
included by actionview by default.

This fixes running these tests in isolation.
2019-12-21 03:38:39 -08:00
Ryuta Kamizono b3da023081 Add `ruby2_keywords` for delegating to `fields` helper 2019-12-21 02:44:26 +09:00
Rafael França afdee462a8
Merge pull request #36147 from pustomytnyk/patch-1
Fix handling of undefined year in DateTimeSelector
2019-12-18 12:24:16 -03:00
Rafael Mendonça França 7c1bf1a474
Merge pull request #25214 from maclover7/jm-av-tests
Cleanup Action View `abstract_unit`
2019-12-18 12:23:11 -03:00
Roman Sokhan 8251651a0d Fix handling of undefined year in DateTimeSelector 2019-12-18 16:56:09 +02:00
Abhay Nikam 2cd84ad745 Fixed typo in the class_names helper documentation and added a spec covering the scenario 2019-12-16 13:48:19 +05:30
Joel Hawksley f24734a7e1
Introduce class_names helper
As a follow-up to https://github.com/rails/rails/pull/37872,
this change introduces a class_names view helper
to make it easier to conditionally apply class names
in views.

Before:
<div class="<%= item.for_sale? ? 'active' : '' %>">

After:
<div class="<%= class_names(active: item.for_sale?) %>">

We've been using this helper in the GitHub monolith
since 2016.

Co-authored-by: Aaron Patterson <tenderlove@github.com>
2019-12-09 16:41:18 -07:00
Edouard CHIN 95bc8d0a23 Fix input value not properly applied:
- #37872 introduced a regression and you can't do

  ```html.erb
    hidden_field_tag('token', value: [1, 2, 3])
  ```

  This will result in a `<input type="hidden" value=""`>.

  I chose `hidden_field_tag` and the `value` attribute as an example
  but this issue applies to any tag helper and any attributes.

  https://github.com/rails/rails/pull/37872#issuecomment-561806468
  mention that the feature should only apply for "class" attribute.

  This commit fix original intent of #37872
2019-12-09 22:54:25 +01:00
Santiago Bartesaghi 8872ec431a
perform_caching config affects collection caching 2019-12-05 21:46:02 +01:00
Joel Hawksley 54f418d96d
appease rubocop 2019-12-03 11:45:51 -07:00
Joel Hawksley f508d4da4d
Merge branch 'master' into content-tag-hash-class-conditional 2019-12-03 11:32:58 -07:00
Joel Hawksley f1c63d8673
Add support for conditional values to TagBuilder
Adds support for conditional values to TagBuilder,
extracting logic we use in the GitHub application,
inspired by https://github.com/JedWatson/classnames.

It’s common practice to conditionally apply CSS classes
in Rails views. This can lead to messy string interpolation,
often using ternaries:

```ruby
content_tag(
  "My username",
  class: "always #{'sometimes' if current_user.special?} another"
)
```

By adding support for hashes to TagBuilder, we can instead write the following:

```ruby
content_tag(
  "My username",
  class: ["always", "another", { 'sometimes' => current_user.special? }]
)
```

cc @JedWatson
2019-12-03 11:30:38 -07:00
Ryuta Kamizono d558febe32 Auto-correct rubocop offences 2019-11-24 09:54:47 +09:00
Tatsuya Hoshino 41f0fb8c6e Remove needless `require "pp"`
In Ruby 2.5 and later, `Kernel#pp` is automatically loaded.

https://bugs.ruby-lang.org/issues/14123

This changes remove the needless `require "pp"`.
2019-11-06 22:50:53 +09:00
Juanjo Bazán b24190807d update https urls [ci skip] 2019-10-03 11:01:32 +02:00
Rafael França 9c6bdb54a3
Merge pull request #34809 from bogdanvlviv/fix-select-with-nil
`ActionView::Helpers::FormOptionsHelper#select` should mark option for `nil` as selected
2019-09-19 15:45:40 -04:00
Akira Matsuda 9bce8c3c02 form_authenticity_token takes keyword arguments 2019-09-15 03:05:52 +09:00
John Hawthorn 2f44c3b00b
Merge pull request #37119 from jonathanhefner/fix-escaping-in-view-path-resolver
Fix escaping in OptimizedFileSystemResolver
2019-09-10 17:36:22 -07:00
Jonathan Hefner 897cdc721e Fix escaping in OptimizedFileSystemResolver
`OptimizedFileSystemResolver` builds a regular expression to match view
template paths.  Prior to this patch, only file globbing special
characters were escaped when building this regular expression, leaving
other regular expression special characters unescaped.

This patch properly escapes all regular expression special characters,
and adds test coverage for paths that include these characters.

Fixes #37107.
2019-09-09 12:20:02 -05:00
Akira Matsuda 7a563f749d Fix Class#new + keyword arguments warnings 2019-09-09 02:35:59 +09:00
bogdanvlviv cd2cbdcb48
`ActionView::Helpers::FormOptionsHelper#select` should mark option for `nil` as selected
```ruby
 @post = Post.new
 @post.category = nil

 # Before
 select("post", "category", none: nil, programming: 1, economics: 2)
 # =>
 # <select name="post[category]" id="post_category">
 #   <option value="">none</option>
 #  <option value="1">programming</option>
 #  <option value="2">economics</option>
 # </select>

 # After
 select("post", "category", none: nil, programming: 1, economics: 2)
 # =>
 # <select name="post[category]" id="post_category">
 #   <option selected="selected" value="">none</option>
 #  <option value="1">programming</option>
 #  <option value="2">economics</option>
 # </select>
 ```

To get the same result without these changes we can set `:selected` as `@post.category.to_s`:
 ```ruby
 select("post", "category", {none: nil, programming: 1, economics: 2}, {selected: @post.category.to_s}
 ```
2019-09-05 21:41:34 +03:00
George Claghorn 3437691708
Merge pull request #37040 from rails/reduce-info-level-logging
Logging at info level should be reserved for top-level concerns
2019-09-05 09:44:20 -04:00
Ryuta Kamizono 0cf609661e Auto-correct rubocop offences 2019-09-05 15:46:58 +09:00
Akira Matsuda d2f4541f12 Passing in a Hash instance as non-kwargs parameters has to be curly braced now 2019-09-05 13:26:58 +09:00
Akira Matsuda 4071115ddd Passing in a Hash instance as kwargs parameters requires the "double splat" prefix 2019-09-05 13:26:58 +09:00
David Heinemeier Hansson 6315a11b90 Logging at info level should be reserved for top-level concerns
Information about partials and cable connection notices are too low level.
2019-08-26 16:43:45 -04:00
Akihito Tsukamoto 0a77c05308 Prevent TagBuilder modify options 2019-08-20 00:13:02 +09:00
Rafael Mendonça França 92cb55ce19
This test needs integer core ext for time 2019-08-02 01:16:50 -04:00
Rafael Mendonça França 4b4bed83e2
Require integer conversion for this test 2019-08-02 00:49:47 -04:00
Rafael Mendonça França 967beb7229
Revert "MethodCallAssertions is a regular player of the team ActiveSupport::TestCase now"
This reverts commit 98d0f7ebd3.
2019-08-02 00:24:21 -04:00
Rafael Mendonça França 6384933994
Revert "You give jruby_skip & rubinius_skip a good name"
This reverts commit 8d2866bb80.
2019-08-02 00:24:11 -04:00
Akira Matsuda 8d2866bb80 You give jruby_skip & rubinius_skip a good name
This hack prevails everywhere in the codebase by being copy & pasted, and it's actually not a negative thing but a necessary thing for framework implementors,
so it should better have a name and be a thing.

And with this commit, activesupport/test/abstract_unit.rb now doesn't silently autoload AS::TestCase,
so we're ready to establish clearner environment for running AS tests (probably in later commits)
2019-08-02 05:36:38 +09:00
Akira Matsuda 98d0f7ebd3 MethodCallAssertions is a regular player of the team ActiveSupport::TestCase now
It's used everywhere, clean and mature enough
2019-08-02 05:36:15 +09:00
hc0208 6a409f7341 Fix typo submited → submitted [ci skip] 2019-07-31 12:22:21 +09:00
Akira Matsuda 8f90ac7827 Add AS::TimeZone#match? 2019-07-29 14:17:36 +09:00
Akira Matsuda 62d089a4ad Suppress Ruby warning: ⚠️ non-nil $, will be deprecated 2019-07-29 12:55:28 +09:00
st0012 e289c8d775 Assert query counts in cache relation test
This is to guard the change in #35982
2019-07-28 14:24:47 +08:00
Pietro Moro c285c15820 Change test description with the correct URL name 2019-07-26 21:25:23 +00:00
Abhay Nikam b34d7692c1 Change the test description to say the URL helper name in test. 2019-07-27 00:41:28 +05:30
Pietro Moro 0eff6956a5 Added a phone_to helper method, on the style of mail_to and sms_to. (#36775)
* Added a phone_to helper method, on the style of mail_to and sms_to.

It creates an anchor tag with the href set to tel: *here your number*
which, when clicked on a mobile phone, or on a desktop with a supported
application, lets the phone app kick in, and it prepopulates it with the
phone number specified.

[Pietro Moro + Rafael Mendonça França]
2019-07-26 14:54:57 -04:00
Rafael França 1760fe919f
Merge pull request #36764 from willianveiga/feature/inputs-inside-disabled-fieldset-are-not-submited-on-remote-true-forms
Inputs inside disabled fieldset are not submited on remote: true forms
2019-07-26 13:48:18 -04:00
Willian Gustavo Veiga 64631d83c5 Add test for submitted fields within disabled fieldsets 2019-07-26 12:59:23 -03:00
Rafael Mendonça França 64f4d7fcb0
Merge pull request #36576 from mtsmfm/mtsmfm/fix-fixture-resolver
Support :any variants for ActionView::FixtureResolver
2019-07-25 22:24:31 -04:00
Guillermo Iguaran ea91f9bc1c
Merge pull request #36511 from aantix/sms_link_helper
Helper method to create an sms link
2019-07-19 15:36:58 -05:00
Guilherme Mansur 526a5eb10c Empty array instead of nil for source_extract
The source_extract method will return nil when it can't find the file name in
the backtrace, methods that consume this method expect an array and the nil ends
up causing type errors down the road like it happened here: #36341. This
patch refactors the source_extract method so that it returns an empty
array instead of nil when it can't find the source code.

Co-authored-by: Kasper Timm Hansen <kaspth@gmail.com>
2019-07-14 15:04:25 -04:00
Fumiaki MATSUSHIMA 92af4aba25 Support :any variants for FixtureResolver 2019-06-30 02:23:08 +09:00
Jim Jones 3115b735ea Helper method to create an sms link - when clicked it opens the phone/desktop's messaging client with the phone number and optional body value prepopulated. 2019-06-18 12:10:16 -05:00
Rafael França c65acad0df
Merge pull request #36482 from Shopify/fix-translation-helper-default-hash
Fix TranslationHelper#translate handling of Hash defaults
2019-06-17 13:38:39 -04:00
Ryuta Kamizono c0af72bf86 Fix rubocop violations 2019-06-14 23:32:15 +09:00
Jean Boussier 5d4a77d324 Fix TranslationHelper#translate handling of Hash defaults
It is sometimes expected of the `translate` methods to return a Hash,
for instance it's the case of the `number.format` key.

As such users might need to specify a Hash default, e.g.

`translate(:'some.format', default: { separator: '.', delimiter: ',' })`.

This works as expected with the `I18n.translate` methods,
however `TranslationHelper#translate` apply `Array()` on the default value.

As a result the default value end up as `[:separator, '.', :delimiter, ',']`.
2019-06-14 14:07:28 +02:00
Gannon McGibbon c9e52d028c
Merge pull request #36437 from sudara/fix_programmatic_clicks_with_data_remote
Fix programmatic clicks with data-remote
2019-06-13 17:30:38 -04:00
Sudara ab4ed8c786 Ensure non-mouse/programmatic clicks work with data-remote 2019-06-13 19:59:17 +02:00
Aaron Patterson 3683a828dc
Merge pull request #36388 from joelhawksley/actionview-component
Introduce ActionView::Component
2019-06-13 09:37:49 -07:00
Ryuta Kamizono c81af6ae72 Enable `Layout/EmptyLinesAroundAccessModifier` cop
We sometimes say "✂️ newline after `private`" in a code review (e.g.
https://github.com/rails/rails/pull/18546#discussion_r23188776,
https://github.com/rails/rails/pull/34832#discussion_r244847195).

Now `Layout/EmptyLinesAroundAccessModifier` cop have new enforced style
`EnforcedStyle: only_before` (https://github.com/rubocop-hq/rubocop/pull/7059).

That cop and enforced style will reduce the our code review cost.
2019-06-13 12:00:45 +09:00
Joel Hawksley c221b5b448
`RenderingHelper` supports rendering objects that `respond_to?` `:render_in`
Co-authored-by: Natasha Umer <natashau@github.com>
Co-authored-by: Aaron Patterson <tenderlove@github.com>
Co-authored-by: Shawn Allen <shawnbot@github.com>
Co-authored-by: Emily Plummer <emplums@github.com>
Co-authored-by: Diana Mounter <broccolini@github.com>
Co-authored-by: John Hawthorn <jhawthorn@github.com>
Co-authored-by: Nathan Herald <myobie@github.com>
Co-authored-by: Zaid Zawaideh <zawaideh@github.com>
Co-authored-by: Zach Ahn <engineering@zachahn.com>
2019-06-12 16:31:01 -06:00
John Hawthorn 1126d14c14
Merge pull request #36422 from jhawthorn/parallelize_actionview
Run actionview tests in parallel
2019-06-06 11:23:31 -07:00
Abhay Nikam 00b3b68602 Bump rubocop to 0.71 2019-06-06 15:34:50 +05:30
John Hawthorn df4ab6e103 Run actionview tests in parallel 2019-06-05 18:40:08 -07:00
John Hawthorn d130ea2ff2 Remove actionview tests which modify fixtures
We shouldn't modify fixtures (or any files which are checked-in). It
prevents us from parallelizing, and probably has other issues.

We could fix these tests by copying the file to a tmpdir and modifying
it there, but I don't think they are testing anything useful anymore.
Re-initializing a resolver isn't representative of "uncached" rendering
(either in dev-mode or using lookup_context.disable_cache).
2019-06-05 18:40:06 -07:00
yuuji.yaginuma ea5f509643 Change `ActionDispatch::Response#content_type` returning Content-Type header as it is
Since #35709, `Response#conten_type` returns only MIME type correctly.
It is a documented behavior that this method only returns MIME type, so
this change seems appropriate.
39de7fac05/actionpack/lib/action_dispatch/http/response.rb (L245-L249)

But unfortunately, some users expect this method to return all
Content-Type that does not contain charset. This seems to be breaking
changes.

We can change this behavior with the deprecate cycle.
But, in that case, a method needs that include Content-Type with
additional parameters. And that method name is probably the
`content_type` seems to properly.

So I changed the new behavior to more appropriate `media_type` method.
And `Response#content_type` changed (as the method name) to return Content-Type
header as it is.

Fixes #35709.

[Rafael Mendonça França & Yuuji Yaginuma ]
2019-06-01 09:20:13 +09:00
Younes SERRAJ a4229a534f Fix select_tag so that is doesn't change options when include_blank is set 2019-05-22 10:21:59 +02:00
Ryuta Kamizono 020856c328 Remove useless `GC.start` in `test/template/render_test.rb`
The `GC.start` was added at b29e893, but the finalizer has been removed
at 7d0ce78 in #35036.
2019-05-06 23:30:07 +09:00
st0012 34a33f8b0c Clear Resolvers' cache after running RenderTestCases' test cases
The templates rendered in RenderTestCases tests will be cached by the
resolvers unexpectedly. And this will break other tests when executed in
certain order. (See https://github.com/rails/rails/issues/36154 for more
detail)

So to fix this issue, we just need to clear the caches on all resolvers.
2019-05-06 22:08:48 +08:00
Rafael França d4d145a679
Merge pull request #32313 from lulalala/model_error_as_object
Model error as object
2019-04-24 16:16:00 -04:00
Koichi ITO c6379fd27f Bump RuboCop to 0.67.2
Performance cops will be extracted from RuboCop to RuboCop Performance
when next RuboCop 0.68 will be released.
https://github.com/rubocop-hq/rubocop/issues/5977

RuboCop 0.67 is its transition period.

Since rails/rails repository uses Performance cops, This PR added
rubocop-performance gem to Gemfile.

And this PR fixes some offenses using the following auto-correct.

```console
% bundle exec rubocop -a

Offenses:

activerecord/test/cases/connection_adapters/connection_handlers_multi_db_test.rb:212:26:
C: [Corrected] Layout/SpaceAroundOperators: Operator =
> should be surrounded by a single space.
              "primary"  => { adapter: "sqlite3", database: "db/primary.sqlite3" }
                         ^^
activerecord/test/cases/connection_adapters/connection_handlers_multi_db_test.rb:239:26:
C: [Corrected] Layout/SpaceAroundOperators: Operator => should be
surrounded by a single space.
              "primary"  => { adapter: "sqlite3", database: "db/primary.sqlite3" }
                         ^^
actionview/test/template/resolver_shared_tests.rb:1:1: C: [Corrected]
Style/FrozenStringLiteralComment: Missing magic comment #
frozen_string_literal: true.
module ResolverSharedTests
^
actionview/test/template/resolver_shared_tests.rb:10:33: C: [Corrected]
Layout/SpaceAroundEqualsInParameterDefault: Surrounding space missing in
default value assignment.
  def with_file(filename, source="File at #{filename}")
                                ^
actionview/test/template/resolver_shared_tests.rb:106:5: C: [Corrected]
Rails/RefuteMethods: Prefer assert_not_same over refute_same.
    refute_same a, b
    ^^^^^^^^^^^

2760 files inspected, 5 offenses detected, 5 offenses corrected
```
2019-04-16 17:58:24 +09:00
John Hawthorn 2455d16a5a Add additional test for sharing templates 2019-04-12 16:08:30 -07:00
John Hawthorn 53e4055a75 Support disabling cache for Digestor
This adds a bit of complexity, but is necessary for now to avoid holding
extra copies of templates which are resolved from ActionView::Digestor
after disabling cache on the lookup context.
2019-04-12 12:30:26 -07:00
John Hawthorn 1fc735e5f5 De-dup Templates, introduce UnboundTemplate
Previously it's possible to have multiple copies of the "same" Template.
For example, if index.html.erb is found both the :en and :fr locale, it
will return a different Template object for each. The same can happen
with formats, variants, and handlers.

This commit de-duplicates templates, there will now only be one template
per file/virtual_path/locals tuple.

We need to consider virtual_path because both `render "index"`, and
`render "index.html"` can both find the same file but will have
different virtual_paths. IMO this is rare and should be
deprecated/removed, but it exists now so we need to consider it in order
to cache correctly.

This commit introduces a new UnboundTemplate class, which represents a
template with unknown locals. Template objects can be built from it by
using `#with_locals`. Currently, this is just a convenience around
caching templates, but I hope it's a helpful concept that could have
more utility in the future.
2019-04-12 12:30:26 -07:00
John Hawthorn 80e2aaa80a Add tests against resolver
We didn't previously have many tests directly against the
OptimizedFileSystemResolver or FileSystemResolver, though usually
failures would be exposed through other tests.

It's easier to test some specifics of the behaviour with unit tests.
This also lets us test FileSystemResolver (non-optimized) which I don't
think previously had much testing (other than from classses inheriting
it).
2019-04-11 17:09:00 -07:00
John Hawthorn 151cac65f1 Deprecate Template#refresh 2019-04-04 15:07:14 -07:00
John Hawthorn b8c90c3644 Don't discard source after rendering
Previously, we would discard the template source after rendering, if we
had a virtual path, in hopes that the virtual path would let us find our
same template again going through the Resolver.

Previously we discarded the source as an optimization, to avoid keeping
it around in memory. By instead just reading the file every time source
is called, as FileTemplate does, this is unnecessary.
2019-04-04 14:25:31 -07:00
Prathamesh Sonpatki 6a4bf486c3
Fix deprecation warning about variants and formats
- After https://github.com/rails/rails/pull/35408 and
  https://github.com/rails/rails/pull/35406, the `formats` and
  `variants` methods are deprecated in favor of `format` and `variant`.
2019-04-04 19:29:27 +05:30
Fumiaki MATSUSHIMA 61c4be4777 Output junit format test report 2019-04-04 14:34:46 +09:00
Rafael França eda2d7d7b9
Merge pull request #35145 from st0012/fix-35114
Fix partial caching ignore repeated items issue
2019-04-03 23:03:30 -04:00
st0012 e8688ddb33 Fix partial caching ignore repeated items issue
This is because we only use hash to maintain the result. So when the key
are the same, the result would be skipped. The solution is to maintain
an array for tracking every item's position to restructure the result.
2019-04-04 09:59:06 +08:00
Edward Rudd c5efbbbccb Fix checking for template variants when using the ActionView::FixtureResolver 2019-04-03 18:09:34 -04:00
John Hawthorn eb52904eb5 Always reject files external to app
Previously, when using `render file:`, it was possible to render files
not only at an absolute path or relative to the current directory, but
relative to ANY view paths. This was probably done for absolutely
maximum compatibility when addressing CVE-2016-0752, but I think is
unlikely to be used in practice.

Tihs commit removes the ability to `render file:` with a path relative
to a non-fallback view path.

Make FallbackResolver.new private

To ensure nobody is making FallbackResolvers other than "/" and "".

Make reject_files_external_... no-op for fallbacks

Because there are only two values used for path: "" and "/", and
File.join("", "") == File.join("/", "") == "/", this method was only
testing that the absolute paths started at "/" (which of course all do).

This commit doesn't change any behaviour, but it makes it explicit that
the FallbackFileSystemResolver works this way.

Remove outside_app_allowed argument

Deprecate find_all_anywhere

This is now equivalent to find_all

Remove outside_app argument

Deprecate find_file for find

Both LookupContext#find_file and PathSet#find_file are now equivalent to
their respective #find methods.
2019-04-03 09:02:28 -07:00
Sharang Dashputre 771973c13d url -> URL where apt except inside actionpack/ 2019-04-01 22:56:35 +05:30
Kasper Timm Hansen 563bf5771f
Merge pull request #35793 from jhawthorn/deprecate_layout_absolute_path
Deprecate render layout with an absolute path
2019-03-31 19:05:27 +02:00
lulalala abee034368 Raise deprecation for calling `[:f] = 'b'` or `[:f] << 'b'`
Revert some tests to ensure back compatibility
2019-03-31 22:59:12 +08:00
Aaron Patterson 2bf5517981
Merge pull request #35688 from jhawthorn/render_file_rfc
RFC: Introduce Template::File
2019-03-30 13:33:52 -07:00
John Hawthorn 6841d0cc6d Deprecate render layout with an absolute path
This has similar problems to render file:.

I've never seen this used, and believe it's a relic from when all
templates could be rendered from an absolute path.
2019-03-29 11:39:40 -07:00
Prathamesh Sonpatki 12701d5a46
Fix annotated typo 2019-03-29 20:42:35 +05:30
Rafael França e3f5f1c936
Merge pull request #35308 from erose/better-error-reporting-for-syntax-errors-in-templates
Display a more helpful error message when an ERB template has a Ruby syntax error.
2019-03-28 14:53:22 -04:00
John Hawthorn c7820d8124 Introduce Template::File as new render file:
The previous behaviour of render file: was essentially the same as
render template:, except that templates can be specified as an absolute
path on the filesystem.

This makes sense for historic reasons, but now render file: is almost
exclusively used to render raw files (not .erb) like public/404.html. In
addition to complicating the code in template/resolver.rb, I think the
current behaviour is surprising to developers.

This commit deprecates the existing "lookup a template from anywhere"
behaviour and replaces it with "render this file exactly as it is on
disk". Handlers will no longer be used (it will render the same as if
the :raw handler was used), but formats (.html, .xml, etc) will still be
detected (and will default to :plain).

The existing render file: behaviour was the path through which Rails
apps were vulnerable in the recent CVE-2019-5418. Although the
vulnerability has been patched in a fully backwards-compatible way, I
think it's a strong hint that we should drop the existing
previously-vulnerable behaviour if it isn't a benefit to developers.
2019-03-27 15:51:25 -07:00
Ryuta Kamizono bde5f3bfc6
Merge pull request #35761 from koic/bump_rubocop_to_0_66_0
Bump RuboCop to 0.66.0
2019-03-27 08:38:57 +09:00
Koichi ITO 1e1adadb41 Bump RuboCop to 0.66.0
### Summary

RuboCop 0.66.0 has been released.
https://github.com/rubocop-hq/rubocop/releases/tag/v0.66.0

And rubocop-0-66 channel is available in Code Climate.
https://github.com/codeclimate/codeclimate/releases/tag/v0.84.0

RuboCop 0.66.0 fixed the false negative to indentation for
modifier. And this PR applied the auto-correction fixed by it.
https://github.com/rubocop-hq/rubocop/pull/6792

In addtion, this PR is also updating the following 4 gems that
RuboCop depends on.

- Update Psych gem ... https://github.com/rubocop-hq/rubocop/pull/6766
- Update Parser gem to 2.6.2.0 that supports Ruby 2.5.5 and 2.6.2 ...
 https://github.com/whitequark/parser/blob/v2.6.2.0/CHANGELOG.md#changelog
- Remove powerpack gem ... https://github.com/rubocop-hq/rubocop/pull/6806
- Update unicode-display_width gem ... https://github.com/rubocop-hq/rubocop/pull/6813
2019-03-27 07:57:43 +09:00
John Hawthorn 573e361a3c Deprecate custom patterns for PathResolver
Custom glob patterns tie the implementation (Using Dir.glob) to the API
we provide.

It also doesn't really work. extract_handler_and_format_and_variant
expects the handler, format, and variant to be at the end of the
template path, and in the same order as they are in the default pattern.

This deprecates specifying a custom path for FileSystemResolver and
removes the pattern argument of OptimizedFileSystemResolver#initialize,
which does not work with a custom pattern.
2019-03-26 14:19:31 -07:00
John Hawthorn f35631d466 Prefer render template: in tests
Many tests were using `render file:`, but were only testing the
behaviour of `render template:` (file: just allows more paths/ is less
secure then template:).

The reason for so many `render file:` is probably that they were the old
default.

This commit replaces `render file:` with `render template:` anywhere the
test wasn't specifically interested in using `render file:`.
2019-03-21 13:44:56 -07:00