Commit Graph

480 Commits

Author SHA1 Message Date
John Hawthorn
758ad13c58 Avoid creating helper modules until modified
In applications which use :all helpers (the default), most controllers
won't be making modifications to their _helpers module.

In CRuby this created many ICLASS objects which could cause a large
increase in memory usage in applications with many controllers and
helpers.

To avoid creating unnecessary modules this PR builds modules only when a
modification is being made: ethier by calling `helper`, `helper_method`,
or through having a default helper (one matching the controller's name)
included onto it.
2020-09-08 16:55:53 -07:00
Petrik
080edb444a Shorten inspect on AbstractController::Base
Calling self in an action of a controller generates an endless stream of
characters, including the request object and all instances variables.
This can be frustrating when using a debugger in a controller and
accidentally calling `self` generates output for a couple of seconds.

This shortens inspect to only show the class name.

    MyController.new.inspect # => "#<MyController:0x00000000005028>"
2020-09-05 19:37:31 +02:00
fatkodima
e24d6ecbfd Update rubocop-performance gem and enable Performance/DeletePrefix and Performance/DeleteSuffix cops 2020-05-24 12:51:35 +03:00
fatkodima
6c4f3be929 Unify raise_on_missing_translations for views and controllers 2020-05-20 02:42:59 +03:00
Petrik
3551a95e53 Use DidYouMean for ActionNotFound
If an action isn't found on a controller we can suggest similar actions:

```
The action 'indx' could not be found for SimpleController
Did you mean?  index
```
2020-05-18 15:41:26 +02:00
Ryuta Kamizono
98a1405f07 Dogfooding "active_support/core_ext/symbol/starts_ends_with"
Any missing thing would be found such like #39159.
2020-05-06 14:19:25 +09:00
Xavier Noria
5b28a0e972 Remove require_dependency usage in helper [Closes #37632]
Motivation is twofold:

  * We are gradually removing `require_dependency` from the framework.

  * Let `helper` work if `config.add_autoload_paths_to_load_path` is
    disabled.

Co-authored-by: Jean Boussier <jean.boussier@gmail.com>
2020-05-02 17:57:55 +02:00
Rafael Mendonça França
f1a2c021e3
Make _protected_ivars private
This method is only used internally and it being public it was being
retorned in the `action_methods` list.
2020-04-07 12:45:19 -04:00
Vitalii Khustochka
edcccf4990 Only compute :only and :except callback conditions once
Ref: https://github.com/rails/rails/issues/38679

`_normalize_callback_options` mutates the options hash, but
doesn't remove the `:only` and `:except` conditions.

So if you call `before_action` & al with the same option hash
you end up with multiple instance of identical `:if` / `:unless` procs
2020-04-06 11:51:30 +02:00
Vinicius Stock
9caaf26503
Use array subtraction to compute variables of interest in view_assigns 2020-03-19 15:59:46 -04:00
Vinicius Stock
f6bf17058e
Remove extra loop in view_assigns (#38766)
* Remove extra loop in view_assigns

* Use slice instead of range
2020-03-20 03:04:49 +09:00
Ryuta Kamizono
1ef8c60dfc Avoid extra string allocation in the methods generated by eval 2020-03-10 17:43:35 +09:00
Ryuta Kamizono
051e349041 Prefer faster str.start_with? over str.first ==
```ruby
str = "abc"

Benchmark.ips do |x|
  x.report("start_with?") { str.start_with?("a") }
  x.report("first ==")    { str.first == "a" }
end
```

```
Warming up --------------------------------------
         start_with?   282.381k i/100ms
            first ==   207.305k i/100ms
Calculating -------------------------------------
         start_with?     10.239M (± 2.2%) i/s -     51.393M in   5.022151s
            first ==      4.593M (± 4.5%) i/s -     23.011M in   5.021434s
```
2020-02-05 19:15:33 +09: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
Ryuta Kamizono
a33dbb17b0 Follow up fa986ae0cac423bf1ebcb5caeccbecf00c990094 2020-02-05 14:31:11 +09:00
Xavier Noria
cb3b37b379 Deletes the private method add_template_helper
The method add_template_helper is private and used only in one place.
I guess its purpose was to remove the noise of module_eval at the cost
of an indirection.

However, Module#include is public since Ruby 2.1, and the indirection
is no longer justified for my taste. The loop in the caller is more
straightforward now.
2020-01-26 14:28:40 +01:00
Adam Hess
85f95b2f58 prevent helper_method from calling to_hash
`helper_method` was taking `**kwargs` on all definitions by default.
ruby will assume that this means you want keyword args and call
`to_hash` on what you pass if the object responds to `to_hash`. Instead
we should only take keyword args if the helper method defined intends
to pass keyword args.

This also fixes a warning when you pass a hash to your helper method,

```
warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
```

Also, this would be a good candidate for using `...`, but since `send`
requires the method as the first argument, we can't use it here.
2020-01-14 15:33:33 -08:00
Jared Fine
1ead658616 Switch to standardized argument name 2019-12-27 12:35:21 -05:00
Ryuta Kamizono
abf0465b09 Merge pull request #38051 from Shopify/actionpack-6-0-stable-ruby-2.7-warnings
Fix Ruby 2.7 warnings in Action Pack 6.0
2019-12-20 23:02:54 +09:00
Akira Matsuda
9977b63c47 I18n.translate takes keyword arguments 2019-09-09 01:13:38 +09:00
Akira Matsuda
8ebd6e5ccc No need to dup options hash where it's not modified 2019-08-19 00:09:08 +09:00
Akira Matsuda
5ae814d016 Reduce Array assignment by not giving a name for unused *args 2019-07-31 17:41:34 +09:00
st0012
5745a3c092 Add Vary: Accept header when rendering
Problem description (quoted from @rafaelfranca's excellent explanation in https://github.com/rails/jquery-ujs/issues/318#issuecomment-88129005):

> Let say that we requested /tasks/1 using Ajax, and the previous page has the same url. When we click the back button the browser tries to get the response from its cache and it gets the javascript response. With vary we "fix" this behavior because we are telling the browser that the url is the same but it is not from the same type what will skip the cache.

And there's a Rails issue discussing about this problem as well https://github.com/rails/rails/issues/25842

Also, according to [RFC 7231 7.1.4](https://tools.ietf.org/html/rfc7231#section-7.1.4)

>  An origin server SHOULD send a Vary header field when its algorithm
>  for selecting a representation varies based on aspects of the request
>  message other than the method and request target

we should add `Vary: Accept` header when determining content based on the `Accept` header.

Although adding such header by default could cause unnecessary cache invalidation. But this PR only adds the header if:
- The format param is not provided
- The request is a `xhr` request
- The request has accept headers and the headers are valid

So if the user
- sends request with explicit format, like `/users/1.json`
- or sends a normal request (non xhr)
- or doesn't specify accept headers

then the header won't be added.

See the discussion in https://github.com/rails/rails/issues/25842 and
https://github.com/rails/rails/pull/36213 for more details.
2019-07-26 13:52:06 +08: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
John Hawthorn
4ab00cfac0 Use file/line from call to helper_module 2019-06-03 20:06:57 -07:00
John Hawthorn
17424a7de1 Give HelperMethods module a name 2019-06-03 18:59:51 -07:00
Nazar Matus
341b5176c2
AbstractController::Translation#t: dup options 2019-02-15 18:21:41 +02:00
bogdanvlviv
b8231433d6
Remove unused code
- Remove `fragment_cache_key` helper declaration.
  It was removed in e70d3df7c9b05c129b0fdcca57f66eca316c5cfc

- Remove `by_private_lifo`.
  It is unused since a7becf147afc85c354e5cfa519911a948d25fc4d
2019-01-30 14:29:17 +00:00
Rafael Mendonça França
e70d3df7c9
Remove deprecated fragment_cache_key helper in favor of combined_fragment_cache_key 2019-01-17 16:08:31 -05:00
yuuji.yaginuma
9422433e1a Don't use deprecated Module#parents 2018-10-05 09:58:29 +09:00
schneems
235e734e1e Revert "Merge pull request #33970 from rails/eager-url-helpers"
Until #34050 can be resolved

This reverts commit 7f870a5ba2aa9177aa4a0e03a9d027928ba60e49, reversing
changes made to 6556898884d636c59baae008e42783b8d3e16440.
2018-10-03 16:15:47 -05:00
Yasuo Honda
aa3dcabd87 Add Style/RedundantFreeze to remove redudant .freeze
Since Rails 6.0 will support Ruby 2.4.1 or higher
`# frozen_string_literal: true` magic comment is enough to make string object frozen.
This magic comment is enabled by `Style/FrozenStringLiteralComment` cop.

* Exclude these files not to auto correct false positive `Regexp#freeze`
 - 'actionpack/lib/action_dispatch/journey/router/utils.rb'
 - 'activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb'

It has been fixed by https://github.com/rubocop-hq/rubocop/pull/6333
Once the newer version of RuboCop released and available at Code Climate these exclude entries should be removed.

* Replace `String#freeze` with `String#-@` manually if explicit frozen string objects are required

 - 'actionpack/test/controller/test_case_test.rb'
 - 'activemodel/test/cases/type/string_test.rb'
 - 'activesupport/lib/active_support/core_ext/string/strip.rb'
 - 'activesupport/test/core_ext/string_ext_test.rb'
 - 'railties/test/generators/actions_test.rb'
2018-09-29 07:18:44 +00:00
Aaron Patterson
338c3c45fa
Allow helpers to be deferred until the routes have been finalized
ActiveStorage::BaseController subclasses ActionController::Base.
ActionController::Base has an "inherited" hook set that includes the
routing helpers to any subclass of AC::Base.  Since
ActiveStorage::BaseController is a subclass of AC::Base, it will get
routing helpers included automatically.  Unfortunately, when the
framework is eagerly loaded, ActiveStorage::BaseController is loaded
*before* the applications routes are loaded which means it attempts to
include an "in flight" module so it gets an exception.

This commit allows a class that's interested in being extended with
routing helpers register itself such that when the routes are finalized,
it will get the helpers included.  If the routes are already finalized,
then the helpers get included immediately.
2018-09-25 14:58:39 -07:00
schneems
e92adb3da6 Fewer allocations in caching/fragments.rb
Instead of using a splat on the head and tail we can mutate the array by flattening 1 level. We get further savings by not allocating another via `compact` but instead by using `compact!`
2018-08-29 09:49:24 -05:00
bogdanvlviv
4800a613c0
Remove extra execution of uniq! on action_methods
Execution of `to_set` below should remove duplicated elements.

Follow up #33693
2018-08-22 23:48:00 +03:00
schneems
4efb319141 Two fewer array allocations on action_methods
Instead of creating new arrays for `uniq` and `map` we can instead modify the array in place.
2018-08-22 11:17:50 -05:00
yuuji.yaginuma
f9dbc69c64 Show the ENV value correctly in the doc of combined_fragment_cache_key [ci skip]
It seems to need an escape for the showing `ENV`.
https://api.rubyonrails.org/classes/AbstractController/Caching/Fragments.html#method-i-combined_fragment_cache_key
2018-08-22 12:33:03 +09:00
Bart de Water
eb5fea40a4 Enable Start/EndWith and RegexpMatch cops
In cases where the MatchData object is not used, this provides a speed-up:
https://github.com/JuanitoFatas/fast-ruby/#stringmatch-vs-stringmatch-vs-stringstart_withstringend_with-code-start-code-end
2018-07-28 17:37:17 -04:00
Paul McMahon
7b9c19d94b Rails guides are now served over https
http links will be redirected to the https version, but still better to
just directly link to the https version.
2018-07-24 11:29:31 +09:00
Zachary Wasserman
9c6b3204d4 Add cancellation info to before filter docs
It is important for users to know that a render or redirect in a "before"
filter causes the action to be cancelled. This was addressed in the guide, but
not the API docs
(http://guides.rubyonrails.org/action_controller_overview.html#filters).

[ci skip]
2018-03-14 12:18:10 -07:00
Akira Matsuda
b0d0c9f40d [Action Pack] require => require_relative
This basically reverts e9fca7668b9eba82bcc832cb0061459703368397, d08da958b9ae17d4bbe4c9d7db497ece2450db5f,
d1fe1dcf8ab1c0210a37c2a78c1ee52cf199a66d, and 68eaf7b4d5f2bb56d939f71c5ece2d61cf6680a3
2017-10-21 22:48:28 +09:00
Ryuta Kamizono
a52ff1b084 Keep :api: plugin methods in the doc [ci skip]
`:api:` tag was removed in 5349f231 since RDoc doesn't support `:api:`
tag. But those methods are not private API, they are public API for
renderers. The renderers should be able to know that they can override
this method.
2017-10-20 05:47:34 +09:00
Ryuta Kamizono
2ae89f8b79 Make internal methods in AbstractController::Rendering to private 2017-10-02 06:56:57 +09:00
Ryuta Kamizono
5349f23108 Remove :api: tag that has leaked on the doc directly [ci skip]
Currently `:api:` tag has leaked on the doc directly since RDoc doesn't
support `:api:` tag directive.

http://api.rubyonrails.org/v5.1/classes/AbstractController/Rendering.html

So `:api: private` doesn't work as expected. We are using `:nodoc:` for
the purpose.

Related #13989.
2017-09-30 18:42:46 +09:00
Yoshiyuki Hirano
70756b134f Replace unnecessary link with typewriter text [ci skip] 2017-09-02 11:05:54 +09:00
Kir Shatrov
dfcc766163 Use frozen string literal in actionpack/ 2017-07-29 14:02:40 +03:00
Akira Matsuda
d08da958b9 [Abstract Controller] require => require_relative 2017-07-01 18:38:04 +09:00
fatkodima
2768cd1d74 Remove useless class checking for ActiveSupport::Callbackss result_lambda 2017-06-27 21:34:28 +03:00
Vipul A M
ff3c06f718
Allow translate default option to accept an array similar to i18n.t. Fixes #29441 2017-06-15 01:06:04 +05:30