Commit Graph

5814 Commits

Author SHA1 Message Date
Jeremy Daer
2b96d5822b Cache: write_multi (#29366)
Rails.cache.write_multi foo: 'bar', baz: 'qux'

Plus faster `fetch_multi` with stores that implement `write_multi_entries`.
Keys that aren't found may be written to the cache store in one shot
instead of separate writes.

The default implementation simply calls `write_entry` for each entry.
Stores may override if they're capable of one-shot bulk writes, like
Redis `MSET`.
2017-06-06 16:39:20 -07:00
Kasper Timm Hansen
704209b017 Merge pull request #29294 from gsamokovarov/attributes-default
Introduce mattr_accessor default option
2017-06-04 16:12:40 +02:00
Vipul A M
3168da0af7
Don't create extra assignment, just return 2017-06-04 15:42:16 +05:30
Genadi Samokovarov
b6b0c99ff3 Use mattr_accessor default: option throughout the project 2017-06-03 13:52:48 +03:00
Genadi Samokovarov
a5b0c60714 Implement mattr_acessor :default option 2017-06-03 13:52:35 +03:00
Shota Iguchi
3fbe657e9b Add next occur and previous occurred day of week API (#26600) 2017-05-30 13:13:29 +02:00
Ryuta Kamizono
0a9522a8b3 Add missing "not" in the doc for assert_no_changes [ci skip] 2017-05-30 05:53:07 +09:00
David Heinemeier Hansson
1c275d812f Add option for class_attribute default (#29270)
* Allow a default value to be declared for class_attribute

* Convert to using class_attribute default rather than explicit setter

* Removed instance_accessor option by mistake

* False is a valid default value

* Documentation
2017-05-29 18:01:50 +02:00
Eileen M. Uchitelle
edc90c858d Merge pull request #26628 from mjhoy/fix-number-to-human-25742
round before calculating exponent in number_to_human_converter
2017-05-29 10:43:54 -04:00
Kasper Timm Hansen
87eae9ffa6 Merge pull request #29074 from kamipo/remove_returning_true_in_internal_callbacks
Remove returning true in internal callbacks
2017-05-28 17:10:21 +02:00
Matthew Draper
352865d0f8 Merge pull request #29097 from EilisHamilton/fix_uncountable_pluralization_locale
Fix pluralization of uncountables when given a locale
2017-05-28 22:54:54 +09:30
Kasper Timm Hansen
85211ea1ef Clear all current instances before a reload.
If users added an attribute or otherwise changed a CurrentAttributes subclass
they'd see exceptions on the next page load.

Because `ActiveSupport::CurrentAttributes.current_instances` would keep
references to the old instances from the previous request.

We can fix this by clearing out the `current_attributes` before we unload
constants. Then any change to the model can be autoloaded again since its
slot isn't taken by an old instance.

We'll still have to call reset before we clear so external collaborators,
like Time.zone, won't linger with their current value throughout other code.
2017-05-28 10:47:17 +02:00
Kasper Timm Hansen
3cacbc1ef0 Remove double Thread.current storage.
Since we're generating a key through the class name we can combine
the two Thread.current calls into a single hash version.
2017-05-28 10:17:36 +02:00
Kasper Timm Hansen
fcc47bcfcc Use non-raising finder.
`find` raises when it can't find a record, so we'll never reach the
else. Switch to `find_by` which returns nil when no record can be
found.
2017-05-27 14:36:18 +02:00
Kasper Timm Hansen
3a131b6c00 [ci skip] Fix spelling that's a bit of an overreach. 2017-05-27 14:34:53 +02:00
David Heinemeier Hansson
24a864437e ActiveSupport::CurrentAttributes provides a thread-isolated attributes singleton (#29180)
* Add ActiveSupport::CurrentAttributes to provide a thread-isolated attributes singleton

* Need to require first

* Move stubs into test namespace.

Thus they won't conflict with other Current and Person stubs.

* End of the line for you, whitespace!

* Support super in attribute methods.

Define instance level accessors in an included module such that
`super` in an overriden accessor works, akin to Active Model.

* Spare users the manual require.

Follow the example of concerns, autoload in the top level Active Support file.

* Add bidelegation support

* Rename #expose to #set. Simpler, clearer

* Automatically reset every instance.

Skips the need for users to actively embed something that resets
their CurrentAttributes instances.

* Fix test name; add tangible name value when blank.

* Try to ensure we run after a request as well.

* Delegate all missing methods to the instance

This allows regular `delegate` to serve, so we don't need bidelegate.

* Properly test resetting after execution cycle.

Also remove the stale puts debugging.

* Update documentation to match new autoreset
2017-05-26 20:00:27 +02:00
Matthew Draper
4cfcf1ee54 Merge pull request #29176 from bogdanvlviv/define-path-with__dir__
Define path with __dir__
2017-05-26 12:24:43 +09:30
Koichi ITO
bef95d372f Fix a RuboCop offences using rubocop -a 2017-05-24 15:21:17 +09:00
bogdanvlviv
40bdbce191
Define path with __dir__
".. with __dir__ we can restore order in the Universe." - by @fxn

Related to 5b8738c2df003a96f0e490c43559747618d10f5f
2017-05-23 00:53:51 +03:00
Andrew White
36d53f557d Merge pull request #29163 from rails/fix-scalar-duration-calculation
Fix implicit calculations with scalars and durations
2017-05-21 17:12:54 +01:00
Andrew White
28938dd64c Fix implicit calculations with scalars and durations
Previously calculations where the scalar is first would be converted
to a duration of seconds but this causes issues with dates being
converted to times, e.g:

    Time.zone = "Beijing"           # => Asia/Shanghai
    date = Date.civil(2017, 5, 20)  # => Mon, 20 May 2017
    2 * 1.day                       # => 172800 seconds
    date + 2 * 1.day                # => Mon, 22 May 2017 00:00:00 CST +08:00

Now the `ActiveSupport::Duration::Scalar` calculation methods will try
to maintain the part structure of the duration where possible, e.g:

    Time.zone = "Beijing"           # => Asia/Shanghai
    date = Date.civil(2017, 5, 20)  # => Mon, 20 May 2017
    2 * 1.day                       # => 2 days
    date + 2 * 1.day                # => Mon, 22 May 2017

Fixes #29160, #28970.
2017-05-20 16:36:25 +01:00
David Heinemeier Hansson
dccfcbfdaf Remove unused mismatch payload attribute 2017-05-20 16:43:31 +02:00
David Heinemeier Hansson
aa8749eb52 Add cache_key_with_version and use it in ActiveSupport::Cache.expand_cache_key
This retains the existing behavior of
ActiveSupport::Cache.expand_cache_key (as used by etaging) where the
cache key includes the version.
2017-05-19 14:09:09 +02:00
Eilis Hamilton
7c45146b15 Fix pluralization of uncountables when given a locale
Previously apply_inflections would only use the :en uncountables
rather then the ones for the locale that was passed to pluralize or
singularize.

This changes apply_inflections to take a locale which it will use to
find the uncountables.
2017-05-19 13:58:19 +12:00
David Heinemeier Hansson
75fa8dd309 Use recyclable cache keys (#29092) 2017-05-18 18:12:32 +02:00
Kasper Timm Hansen
7cc526beb5 Merge pull request #29086 from mikeycgto/message-encryptor-auth-tag-check
Message encryptor auth tag check

Fixes MessageEncryptor when used in AEAD mode. Specifically, we need to check if the `auth_tag` is nil. This may arise when an AEAD encryptor is used to decrypt a ciphertext generated from a different mode, such as CBC-HMAC. Basically, the number of double dashes will differ and `auth_tag` may be nil in this case.
2017-05-15 21:20:12 +02:00
Michael Coyne
71fb6def5f Fix for AEAD auth_tag check in MessageEncryptor
When MessageEncryptor tries to +decrypt_and_verify+ ciphertexts
generated in a different mode (such CBC-HMAC), the +auth_tag+ may be
+nil+ and must explicitly check for it.

See the discussion here:
https://github.com/rails/rails/pull/28132#discussion_r116388462
2017-05-15 08:54:39 +00:00
Eugene Kenny
db9ae5f1e1 Don't cache locally if unless_exist was passed
Some cache backends support the `unless_exist` option, which tells them
not to overwrite an existing entry. The local cache currently always
stores the new value, even though the backend may have rejected it.

Since we can't tell which value will end up in the backend cache, we
should delete the key from the local cache, so that the next read for
that key will go to the backend and pick up the correct value.
2017-05-14 23:24:37 +01:00
Ryuta Kamizono
0584e21ac0 Remove returning true in internal callbacks
`display_deprecation_warning_for_false_terminator` was removed since
3a25cdc.
2017-05-14 03:42:17 +09:00
Peter McCracken
4fc2ea1181 handle loops in the cause chain in Rescuable#rescue_with_handler 2017-05-10 13:33:46 -04:00
T.J. Schuck
aa91328058 Assorted delegate_missing_to doc fixes
* Fix rdoc code formatting — `tt`, not backticks

* Fix/simplify sentence grammar — should at least just be “and the like”, not “likes”, but this is just general tightening up.

* Add note that delegated methods must be public.  Tested here: 7ff5ccae94/activesupport/test/core_ext/module_test.rb (L359-L365)

* Simplify example code for delegate_missing_to. The example had complexity that wasn’t necessary for demonstrating `delegate_missing_to`.  This gets rid of a bunch of cruft so the example is more obvious about what’s going on regarding the feature itself.

[ci skip]
2017-05-04 16:29:00 -04:00
Ryuta Kamizono
4e6d84f555 Use flat_map rather than map(&:...).flatten 2017-04-29 23:30:38 +09:00
Eugene Kenny
001fc80d87 Add missing require for remove_possible_method
5055370828
added a call to `remove_possible_method`, but didn't require the file
that defines it.
2017-04-28 10:18:55 -07:00
Ryuta Kamizono
ca9ac31002 respond_to_missing? should be private
Follow up of 03d3f036.

Some of `respond_to?` were replaced to `respond_to_missing?` in 03d3f036.
But the visibility is still public. It should be private.
2017-04-22 23:11:15 +09:00
Jon Moss
32431b3770 Add comma
[ci skip]
2017-04-20 18:40:26 -04:00
Rafael França
e88636f733 Merge pull request #28582 from sbull/activejob-error-logging
Add error logging to Active Job
2017-04-20 18:01:57 -04:00
Josh Nussbaum
62b1b17221 Fixes Hash.from_xml with frozen strings for all backends 2017-04-20 16:31:50 -04:00
Marion Bouguet
ba2e9e0911 Remove outdated comment
Since 3aee9126aa6309538ee64064dcabcd34d7cc7d26, this class hasn't inherited from Array.
2017-04-20 16:20:00 +09:00
Willem van Bergen
0bdf6d757b Send deprecation horizon and gem name as arguments to deprecation heavier handler, and make sure they are used for the ActiveSupport::Notifications message. 2017-04-19 18:23:28 -04:00
Rafael França
48c5139ae3 Merge pull request #28790 from tjschuck/require_as_time_in_testing_time_helpers
Explicitly require AS::Time in AS::Testing::TimeHelpers
2017-04-18 17:25:19 -04:00
T.J. Schuck
1d82b7ce7c Explicitly require AS::Time in AS::Testing::TimeHelpers
If you just try to use `ActiveSupport::Testing::TimeHelpers` standalone by requiring `active_support/testing/time_helpers`, you currently get an error: `NoMethodError: undefined method `change' for 2017-12-14 01:04:44 -0500:Time`

9f6e82ee4783e491c20f5244a613fdeb4024beb5 added a dependency on `AS::Time` by using `AS::Time#change`.

Here's a script to reproduce the error:

```ruby
require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"
  gem "activesupport", github: "rails/rails"
end

require "active_support/testing/time_helpers"

require "minitest/autorun"

class BugTest < Minitest::Test
  include ActiveSupport::Testing::TimeHelpers

  def test_stuff
    travel_to Time.new(2017, 12, 14, 01, 04, 44) do
      assert true
    end
  end
end
```

It currently fails for all 5.x.x versions and master.  Ideally, this would be backported to `5-0-stable` and `5-1-stable` as well.
2017-04-18 15:43:01 -04:00
Fumiaki MATSUSHIMA
3adbf14d65 Fix Enumerable#sum redefined warning
If we require 'active_support/core_ext/enumerable' on Ruby 2.4,
we'll see following warning because `Enumerable#sum` and `Array#sum`
are added in Ruby 2.4.

```
rails/rails/activesupport/lib/active_support/core_ext/enumerable.rb:20: warning: method redefined; discarding old sum
```

The minimal way to fix the warning is `alias sum sum`.

```
$ ruby -v
ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-linux]

$ ruby -w -e "def a; end; def a; end"
-e:1: warning: method redefined; discarding old a
-e:1: warning: previous definition of a was here

$ ruby -w -e "def a; end; alias a a; def a; end"
```

But this behavior is not intended. (@amatsuda was told by @ko1)
So we should use `alias` as a meaningful way.

Ruby 2.4's `sum`  (`orig_sum`) assumes an `identity` is `0` when we omit `identity`
so we can delegate to `orig_sum` with explicit `identity` only.
In a strict sense, we can detect `identity` by check instance's class
but we don't care at this time about that because calling `Enumerable#sum` is rare.
In many cases, we will call `Array#sum`.
2017-04-18 21:37:47 +09:00
Jon Moss
0d208e02f6 Add missing periods
[ci skip]
2017-04-17 21:57:00 -04:00
Ken Mayer
881def43e3 Add (more) documentation to to_time
There's a difference in the results between `Date#to_time(:local)` and
`Date#in_time_zone` but it is subtle and can confuse users (like me :-).
2017-04-17 17:31:10 -07:00
Chris Dosé
5b32746cc0
There are actually only 134 unique timezones. 2017-04-16 15:03:31 -07:00
Chris Dosé
b6ae8cb990
Add Puerto Rico support to ActiveSupport::TimeZone 2017-04-16 15:01:41 -07:00
Kasper Timm Hansen
1e0d8425f6 Merge pull request #28638 from bogdanvlviv/prepend_and_append_in_ruby
Prevent aliases Array#append and Array#prepend
2017-04-15 16:13:05 +02:00
Andrew White
851b7f866e Add additional options to time change methods
Support `:offset` in `Time#change` and `:zone` or `:offset`
in `ActiveSupport::TimeWithZone#change`.

Fixes #28723.
2017-04-14 18:04:13 +01:00
Josh Pencheon
2144e70a86 Implement fetch_values for HashWithIndifferentAccess (#28316)
`fetch_values` was added to Hash in Ruby 2.3.0:
  https://bugs.ruby-lang.org/issues/10017

This patch adds an implemention for instances of HWAI, in line
with the existing definitions of `fetch` and `values_at`.
2017-04-10 10:20:13 +09:30
Matthew Draper
d928126725 delegate_missing_to should fall back to super 2017-04-09 20:07:12 +09:30