Commit Graph

309 Commits

Author SHA1 Message Date
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
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
Ryuta Kamizono
cfb7c16ac4 Fixup CHANGELOGs [ci skip] 2020-06-07 12:58:22 +09: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
fatkodima
6c4f3be929 Unify raise_on_missing_translations for views and controllers 2020-05-20 02:42:59 +03: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
zvkemp
6380aee182 add additional instrumentation block for ActionView layout rendering 2020-05-01 19:15:54 -07:00
Ryuta Kamizono
977ba0e321 Revert "Update actionview CHANGELOG"
This reverts commit 960ceb237ac886711b8d50cdca3ac13c2e147be8.

Ref #39012, #38858.

[ci skip]
2020-04-27 14:15:00 +09:00
John Hawthorn
960ceb237a Update actionview CHANGELOG 2020-04-07 19:23:13 -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
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
Iago Pimenta
e7c3eb11e0 Use default order of PathResolver::EXTENSIONS for sort templates 2020-01-09 12:01:41 -08:00
Joel Hawksley
e1545682eb
Add changelog entry for https://github.com/rails/rails/pull/37918
Per request from @bogdanvlviv.
2019-12-11 09:20:38 -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
214f439343 Fixup CHANGELOGs [ci skip] 2019-11-24 09:20:00 +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
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
Juanito Fatas
52f0b050e2
Update sanitizer in ActionView::Helpers::SanitizeHelper
- The sanitizer has been changed to safe_list_sanitizer.
- deprecate white_list_sanitizer
2019-08-05 03:35:35 +02:00
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
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
Guilherme Mansur
99e52ae7b1 Autoload SyntaxErrorInTemplate
When a SyntaxError is detected in a template we raise this exception. On
a first request to the server the exception we get a NameError since the
exception is not required from `active_view/template/error.rb` yet.
However later on it gets required and a second request will succeed.
On the first request we see the rails "Something Wen Wrong" page and not
the expected syntax error in template error page with the webconsole and
stacktrace. By autoloading the constant we fix this issue.

Co-authored-by: Gannon McGibbon <gannon.mcgibbon@gmail.com>
2019-06-19 14:53:24 -04: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
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
Rafael Mendonça França
9834be6565
Start Rails 6.1 development 2019-04-24 15:57:14 -04:00
Ryuta Kamizono
3a4aa49256 Fix markup in CHANGELOGs [ci skip]
Need to new line to break line in the markdown.
2019-04-25 03:19:15 +09:00
st0012
88c195bf81 Update the changelog to explain the fix 2019-04-20 01:34:53 +09:00
Edward Rudd
c5efbbbccb Fix checking for template variants when using the ActionView::FixtureResolver 2019-04-03 18:09:34 -04:00
Kasper Timm Hansen
beb0bc9907
[ci skip] Follow up c8bf334104 2019-04-01 21:26:13 +02:00
John Hawthorn
c8bf334104 Only clear template caches in dev after changes (#35629) 2019-04-01 21:22:57 +02:00
Ryuta Kamizono
b89a3e7e63 Tweaks CHANGELOGs and docs [ci skip]
* add leading `#` before `=>` since hash rocket is valid Ruby code
* add backticks
* remove trailing spaces
* and more
2019-03-31 08:38:37 +09:00
Shailesh Kalamkar
839328c700 [ci skip] Fixed typo 2019-03-23 07:22:52 +05:30
John Hawthorn
5c2d695993 Update CHANGELOGs for 6.0.0.beta3 release 2019-03-22 13:13:01 -07:00
eileencodes
7c87fd5635 Prep release
* Update RAILS_VERSION
* Bundle
* rake update_versions
* rake changelog:header
2019-03-11 11:58:15 -04:00
Rafael Mendonça França
5e6e505083
Preparing for 6.0.0.beta2 release 2019-02-25 17:45:04 -05:00
Aaron Patterson
cf0dd4a71d
Fix some typos! 2019-02-06 16:57:34 -08:00
Aaron Patterson
7761ddbebb
Deprecate finalizer configuration (it doesn't do anything)
Revert "Remove finalizer and configuration"

This reverts commit 9e7b4a3173788ea43b11e74a4d2f69a5f1565daa.
2019-02-06 16:57:34 -08:00
Gannon McGibbon
7caea98e18 Merge branch 'float_dom_ids'
Closes #34975.
2019-02-05 13:48:25 -05:00
Javan Makhmali
e3d43333db Add CHANGELOG entries for npm package renames [ci skip] 2019-01-28 06:29:26 -05:00
Mark Edmondson
f8696b888e Fix unique DOM IDs for collection inputs 2019-01-25 10:04:23 -08:00
Ryuta Kamizono
9f203c3f81 Single new line is not rendered as new line in the CHANGELOG.md
https://github.com/rails/rails/blob/v6.0.0.beta1/actionview/CHANGELOG.md

[ci skip]
2019-01-19 18:18:05 +09:00
Rafael Mendonça França
5a0230c67f
Preparing for 6.0.0.beta1 release 2019-01-18 15:42:12 -05:00
Rafael Mendonça França
60c8a03c8d
Remove deprecated image_alt helper 2019-01-17 16:08:31 -05:00
Genadi Samokovarov
a58db74c4f Don't expect defined protect_against_forgery? in {token,csrf_meta}_tag
The `#csrf_meta_tags` and `#token_tag` Action View helper methods are
expecting the class in which are included to explicitly define the
method `#protect_against_forgery?` or else they will fail with
`NoMethodError`.

This is a problem if you want to use Action View outside of Rails
applications. For example, in #34788 I used the `#button_to` helper
inside of the error pages templates that have a custom
`ActionView::Base` subclass, which did not defined
`#protect_against_forgery?` and trying to call the button failed.

I had to dig inside of Action View to find-out what's was going on. I
think we should either set a default method implementation in the
helpers or check for the method definition, but don't explicitly require
the presence of `#protect_against_forgery?` in every `ActionViews::Base`
subclass as the errors are hard to figure out.
2018-12-27 11:33:54 +02:00
Kasper Timm Hansen
1b7c3222e8
Require Ruby 2.5 for Rails 6.
Generally followed the pattern for https://github.com/rails/rails/pull/32034

* Removes needless CI configs for 2.4
* Targets 2.5 in rubocop
* Updates existing CHANGELOG entries for fewer merge conflicts
* Removes Hash#slice extension as that's inlined on Ruby 2.5.
* Removes the need for send on define_method in MethodCallAssertions.
2018-12-19 21:47:50 +01:00
WoH
257a1a6373
Do not disable previously disabled elements 2018-12-06 09:02:07 +01:00
WoH
a2612622e8
Prevent unintended mouse keys from firing click events
Firefox fires click events on left-, right-
and scroll-wheel (any non-primary mouse key) clicks while other browsers don't.
2018-12-05 15:08:14 +01:00
Lyle Mullican
4fdc6269b6 Prevent TextHelper#word_wrap from stripping white space on the left
side of long lines; Fixes #34487
2018-11-19 17:16:34 -05:00