Commit Graph

69031 Commits

Author SHA1 Message Date
Corey Farwell
0077462be0
Indicate true in a doc comment is code-like. 2018-05-22 17:24:33 -04:00
Guillermo Iguaran
e10fd57e98 Bump minimum version of Thor to 0.19.0
Thor 0.18 is causing failures in all the generator tests.
2018-05-22 15:58:35 -05:00
Rafael França
e6519f95da
Merge pull request #32825 from utilum/fewer_tt.eclosure_calls
FIX: tt.eclosure is not needed for the GTG
2018-05-22 16:53:40 -04:00
Guillaume Malette
976ef40aef Rollback parent transaction when children fails to update (#32796)
* Rollback parent transaction when children fails to update

Rails supports autosave associations on the owner of a `has_many`
relationship. In certain situation, if the children of the association
fail to save, the parent is not rolled back.

```ruby
class Employee < ActiveRecord::Base
end

class Company < ActiveRecord::Base
  has_many(:employees)
end

company = Company.new
employee = company.employees.new
company.save
```

In the previous example, if the Employee failed to save, the Company
will not be rolled back. It will remain in the database with no
associated Employee.

I expect the `company.save` call to be atomic, and either create all or
none of the records.

The persistance of the Company already starts a transaction that nests
it's children. However, it didn't track the success or failure of it's
children in this very situation, and the outermost transaction is not
rolled back.

This PR makes the change to track the success of the child insertion and
rollback the parent if any of the children fail.

* Change the test to reflect what we expect

Once #32862 is merged, rolling back a record will rollback it's state to match
the state before the database changes were applied

* Use only the public API to express the tests

* Refactor to avoid reassigning saved for nested reflections

[Guillaume Malette + Rafael Mendonça França]
2018-05-22 15:58:22 -04:00
utilum
0428ed8067 Remove 7 years old note-to-self by tenderlove
Introduced in rails/journey@a806beb

[ci skip]
2018-05-22 21:55:41 +02:00
Rafael França
9385a5b585
Merge pull request #32934 from aki77/fix-locale-selector
Fix locale selector to email preview
2018-05-22 15:13:29 -04:00
Rafael França
09f26ed9da
Merge pull request #32865 from yahonda/another_31988
Disable foreign keys during `alter_table` for sqlite3 adapter
2018-05-22 14:23:38 -04:00
Rafael França
fa9d01d7dd
Merge pull request #32938 from utilum/range_case_equality
Allow Range#=== and Range#cover? on Range
2018-05-22 14:14:56 -04:00
Rafael França
7ee898ca18
Merge pull request #32931 from jeremyevans/fast-xor
Speed up xor_byte_strings by 70%
2018-05-22 13:14:27 -04:00
utilum
e04a79e772 add CHANGELOG/Doc 2018-05-22 15:56:36 +02:00
utilum
0fcb921a65 Allow Range#=== and Range#cover? on Range
ruby/ruby@989e07c features switching `Range#===` to use internal `r_cover_p`
instead of rubyland `include?`. This breaks expected behavior of
`ActiveSupport::CoreExt::Range` documented since at least 8b67a02.

This patch adds overrides on `Range#cover?` and `Range#===` and places all
three in a single module, `CompareWithRange`.

*Requiring core_ext/range/include_range now causes a deprecation warnning*
2018-05-22 14:38:19 +02:00
Yasuo Honda
45881b0a64 Disable foreign keys during alter_table for sqlite3 adapter
Unlike other databases, changing SQLite3 table definitions need to create a temporary table.
While changing table operations, the original table needs dropped which caused
`SQLite3::ConstraintException: FOREIGN KEY constraint failed` if the table is referenced by foreign keys.
This pull request disables foreign keys by `disable_referential_integrity`.

Also `disable_referential_integrity` method needs to execute `defer_foreign_keys = ON`
to defer re-enabling foreign keys until the transaction is committed.

https://www.sqlite.org/pragma.html#pragma_defer_foreign_keys

Fixes #31988

- This `defer_foreign_keys = ON` has been supported since SQLite 3.8.0
https://www.sqlite.org/releaselog/3_8_0.html and Rails 6 requires SQLite 3.8 #32923 now

- <Models>.reset_column_information added to address `ActiveModel::UnknownAttributeError`

```
Error:
ActiveRecord::Migration::ForeignKeyChangeColumnTest#test_change_column_of_parent_table:
ActiveModel::UnknownAttributeError: unknown attribute 'name' for ActiveRecord::Migration::ForeignKeyChangeColumnTest::Post.
```
2018-05-22 02:08:08 +00:00
George Claghorn
6c574ac589
Merge pull request #32936 from jacobsmith/image-variant-allow-disabling-options
[ActiveStorage] Disable variant options when false or nil present
2018-05-21 11:49:22 -04:00
Kasper Timm Hansen
41147e3ef8
Merge pull request #32523 from kaspth/enumerable-index-with-extension
Add Enumerable#index_with.
2018-05-21 17:46:48 +02:00
Kasper Timm Hansen
429f15ff7f
[ci skip] Add documentation/changelog entry. 2018-05-21 17:46:05 +02:00
Kasper Timm Hansen
39c22303a6
Add Enumerable#index_with.
In the app I'm working on I've wished that index_by had a buddy that would
assign the hash value instead of the key multiple times.

Enter index_with. Useful when building a hash from a static list of
symbols. Before you'd do:

```ruby
POST_ATTRIBUTES.map { |attr_name| [ attr_name, public_send(attr_name) ] }.to_h
```

But now that's a little clearer and faster with:

````ruby
POST_ATTRIBUTES.index_with { |attr_name| public_send(attr_name) }
```

It's also useful when you have an enumerable that should be converted to a hash,
but you don't want to muddle the code up with the overhead that it takes to create
that hash. So before, that's:

```ruby
WEEKDAYS.each_with_object(Hash.new) do |day, intervals|
  intervals[day] = [ Interval.all_day ]
end
```

And now it's just:

```ruby
WEEKDAYS.index_with([ Interval.all_day ])
```

It's also nice to quickly get a hash with either nil, [], or {} as the value.
2018-05-21 17:44:54 +02:00
Kasper Timm Hansen
db485a8d99
Merge pull request #32946 from coryjb/patch-1
Exception wording change
2018-05-21 17:24:11 +02:00
Jacob Smith
0210ac0b43 Disable variant options when false or nil present
In response to https://github.com/rails/rails/issues/32917

In the current implementation, ActiveStorage passes all options to the underlying processor,
including when a key has a value of false.

For example, passing:

```
avatar.variant(resize: "100x100", monochrome: false, flip: "-90")
```

will return a monochrome image (or an error, pending on ImageMagick configuration) because
it passes `-monochrome false` to the command (but the command line does not allow disabling
flags this way, as usually a user would omit the flag entirely to disable that feature).

This fix only passes those keys forward to the underlying processor if the value responds to
`present?`. In practice, this means that `false` or `nil` will be filtered out before going
to the processor.

One possible use case would be for a user to be able to apply different filters to an avatar.
The code might look something like:

```
  variant_options = {
    monochrome: params[:monochrome],
    resize:     params[:resize]
  }

  avatar.variant(*variant_options)
```

Obviously some sanitization may be beneficial in a real-world scenario, but this type of
configuration object could be used in many other places as well.

- Add removing falsy values from varaints to changelog

- The entirety of #image_processing_transformation inject block was wrapped in `list.tap`
 to guard against the default `nil` being returned if no conditional was called.

- add test for explicitly true variant options
2018-05-21 10:38:15 -04:00
Kasper Timm Hansen
ba07b5fc12
Rename exception variable to error.
Follows the change from 6fac9bd, so the naming is consistent.
2018-05-21 15:43:16 +02:00
Cory Becker
61ca92661e
Exception wording change 2018-05-21 08:37:49 -05:00
Ryuta Kamizono
054893d574
Merge pull request #32923 from yahonda/bump_sqlite3_version_to_38
Bump minimum SQLite version to 3.8
2018-05-21 21:22:06 +09:00
Ryuta Kamizono
a77447f4da Enable Lint/StringConversionInInterpolation rubocop rule
To prevent redundant `to_s` like https://github.com/rails/rails/pull/32923#discussion_r189460008
automatically in the future.
2018-05-21 21:10:14 +09:00
Yasuo Honda
d1a74c1e01 Bump minimum SQLite version to 3.8
These OS versions have SQLite 3.8 or higher by default.

- macOS 10.10 (Yosemite) or higher
- Ubuntu 14.04 LTS or higher

Raising the minimum version of SQLite 3.8 introduces these changes:

- All of bundled adapters support `supports_multi_insert?`
- SQLite 3.8 always satisifies `supports_foreign_keys_in_create?` and `supports_partial_index?`
- sqlite adapter can support `alter_table` method for foreign key referenced tables by #32865
- Deprecated `supports_multi_insert?` method
2018-05-21 04:56:26 +00:00
Ryuta Kamizono
15d00f04f4 SqlTypeMetadata is :nodoc: class [ci skip]
So do not expose `PostgreSQLTypeMetadata` in the doc too.
2018-05-21 07:28:05 +09:00
yuuji.yaginuma
ce4d467f7c Add test case that configure config.action_view.finalize_compiled_template_methods
Follow up of #32418.
2018-05-20 10:19:12 +09:00
aki
d7f01a28a9 Fix locale selector 2018-05-19 17:23:03 +09:00
Ryuta Kamizono
5f8115a185 Rollback correctly restore initial record id after double save 2018-05-19 10:40:27 +09:00
Ryuta Kamizono
870766017a
Merge pull request #32911 from eugeneius/finalize_transaction_record_state
Finalize transaction record state after real transaction
2018-05-19 10:10:18 +09:00
Eugene Kenny
5359428a14 Finalize transaction record state after real transaction
After a real (non-savepoint) transaction has committed or rolled back,
the original persistence-related state for all records modified in that
transaction is discarded or restored, respectively.

When the model has transactional callbacks, this happens synchronously
in the `committed!` or `rolled_back!` methods; otherwise, it happens
lazily the next time the record's persistence-related state is accessed.

The synchronous code path always finalizes the state of the record, but
the lazy code path only pops one "level" from the transaction counter,
assuming it will always reach zero immediately after a real transaction.
As the test cases included here demonstrate, that isn't always the case.

By using the same logic as the synchronous code path, we ensure that the
record's state is always updated after a real transaction has finished.
2018-05-19 01:34:16 +01:00
Jeremy Evans
8b10a9414d Speed up xor_byte_strings by 70%
Benchmark:

```ruby
require 'benchmark'
require 'benchmark/ips'
require 'securerandom'

def xor_byte_strings(s1, s2) # :doc:
  s2_bytes = s2.bytes
  s1.each_byte.with_index { |c1, i| s2_bytes[i] ^= c1 }
  s2_bytes.pack("C*")
end

def xor_byte_strings_new(s1, s2) # :doc:
  s2 = s2.dup
  size = s1.bytesize
  i = 0
  while i < size
    s2.setbyte(i, s1.getbyte(i) ^ s2.getbyte(i))
    i += 1
  end
  s2
end

s1 = SecureRandom.random_bytes(32)
s2 = SecureRandom.random_bytes(32)

Benchmark.ips do |x|
  x.report("current"){xor_byte_strings(s1, s2)}
  x.report("new"){xor_byte_strings_new(s1, s2)}
  x.compare!
end

100000.times do |i|
  s3 = SecureRandom.random_bytes(32)
  s4 = SecureRandom.random_bytes(32)
  raise unless xor_byte_strings(s3, s4) == xor_byte_strings_new(s3, s4)
end
```

Results on ruby 2.5.1:

```
Warming up --------------------------------------
             current     6.519k i/100ms
                 new    10.508k i/100ms
Calculating -------------------------------------
             current     84.723k (_ 0.4%) i/s -    423.735k in   5.001456s
                 new    145.871k (_ 0.3%) i/s -    735.560k in   5.042606s

Comparison:
                 new:   145870.6 i/s
             current:    84723.4 i/s - 1.72x  slower
```
2018-05-18 15:08:38 -07:00
Rafael França
1efbc634b5
Merge pull request #32900 from ttanimichi/app-update-skip-yarn
Don't generate yarn's contents in `app:update` task if it's skipped
2018-05-18 13:53:28 -04:00
Rafael França
fc5a69def4
Merge pull request #32925 from Linuus/fix/actioncontroller-params-fetch-docs
Fix documentation for ActionController::Params#fetch
2018-05-18 13:49:46 -04:00
Rafael Mendonça França
a213ac360f
Raise a better exception when a invalid depreation behavior is set
Fixes #32928.
2018-05-18 13:35:09 -04:00
Linus Marton
8d5e0d3f5b
Fix documentation for ActionController::Params#fetch
Make it clear that the return value is converted to an
instance of ActionController::Parameters if possible
2018-05-18 10:03:42 +02:00
George Claghorn
9f95767979 Permit opening a blob in a custom tempdir 2018-05-17 19:14:11 -04:00
Rafael França
f018d4e118
Merge pull request #32921 from joshsusser/master
Generate ActiveStorage attachment getter and setter methods in mixin
2018-05-17 18:53:22 -04:00
Rafael Mendonça França
d7edb8ecd0
Fix markdown [ci skip] 2018-05-17 18:19:18 -04:00
Josh Susser
fd0bd1bf68 Generate getter and setter methods in mixin
Generated attachment getter and setter methods are created within
the model's `GeneratedAssociationMethods` module to allow overriding
and composition using `super`.

Includes tests for new functionality.

Co-authored-by: Josh Susser <josh@hasmanythrough.com>
Co-authored-by: Jamon Douglas <terrildouglas@gmail.com>
2018-05-17 14:51:15 -07:00
Rafael França
6c05728a50
Merge pull request #32908 from anniecodes/fix-time-random-string
Fix user_input_in_time_zone to coerce non valid string into nil
2018-05-17 14:24:44 -04:00
Ryuta Kamizono
16c0cd1870 Fix formatting of author credit [ci skip] 2018-05-17 21:01:11 +09:00
Ryuta Kamizono
e77c516a13
Merge pull request #32916 from lucfranken/patch-1
Active storage: Image variant options not correct in docs

[ci skip]
2018-05-17 20:34:06 +09:00
lucfranken
8cc79f502e
Active storage: Image variant options not correct
### Steps to reproduce

Using Rails 5.2.0

When following this example:

http://api.rubyonrails.org/classes/ActiveStorage/Variant.html

`avatar.variant(resize: "100x100", monochrome: true, flip: "-90")`

### Expected behavior

Image should be rendered as flipped.

### Actual behavior

I get an error:

> failed with error: gm mogrify: Unrecognized option (-90).

### Fix:

According to: https://github.com/minimagick/minimagick the option should be called rotate:

`avatar.variant(resize: "100x100", monochrome: true, rotate: "-90")`

So **flip** changed to **rotate**.

### System configuration
**Rails version**: 5.2.0

**Ruby version**: ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin17]
2018-05-17 12:49:11 +02:00
Ryuta Kamizono
a1f9fe8bdd Remove unused require "active_support/core_ext/string/filters" 2018-05-17 15:30:49 +09:00
George Claghorn
e092cae49c Add ActiveStorage::Previewer#tempdir 2018-05-16 22:55:09 -04:00
George Claghorn
ff3210556b Add missing block parameters 2018-05-16 22:50:24 -04:00
George Claghorn
373720bf23 Demonstrate ActiveStorage::Blob#open in the Active Storage guide 2018-05-16 22:26:39 -04:00
George Claghorn
ee21b7c2eb Add ActiveStorage::Blob#open
[David Robertson & George Claghorn]
2018-05-16 22:12:31 -04:00
Annie-Claude Côté
6ff593ef87 Fix user_input_in_time_zone to coerce non valid string into nil
Before it was coercing an invalid string into "2000-01-01 00:00:00".
2018-05-16 17:01:07 -04:00
Annie-Claude Côté
35bf8e90b1 Add missing require for string to timezone conversion
Inside user_input_in_time_zone we call in_time_zone on the value and value can be a String.
2018-05-16 15:48:12 -04:00
Tsukuru Tanimichi
7b0a316f2d Don't generate yarn's contents in app:update task if it's skipped 2018-05-16 20:24:57 +09:00