Commit Graph

2083 Commits

Author SHA1 Message Date
bogdanvlviv
d5c26c43c0
Add ActiveModel::Errors#of_kind?
Related to https://github.com/rails/rails/pull/34817#issuecomment-451508668
2019-01-04 20:47:31 +02:00
Arun Agrawal
50e3680768 Bump license years for 2019 2018-12-31 10:24:38 +07:00
Ryuta Kamizono
892e38c78e Enable Style/RedundantBegin cop to avoid newly adding redundant begin block
Currently we sometimes find a redundant begin block in code review
(e.g. https://github.com/rails/rails/pull/33604#discussion_r209784205).

I'd like to enable `Style/RedundantBegin` cop to avoid that, since
rescue/else/ensure are allowed inside do/end blocks in Ruby 2.5
(https://bugs.ruby-lang.org/issues/12906), so we'd probably meets with
that situation than before.
2018-12-21 06:12:42 +09:00
Ryuta Kamizono
d5699198a4 Module#{attr,attr_accessor,attr_reader,attr_writer} become public since Ruby 2.5
https://bugs.ruby-lang.org/issues/14132
2018-12-21 02:05:27 +09:00
Ryuta Kamizono
8034dde023 Module#{define_method,alias_method,undef_method,remove_method} become public since Ruby 2.5
https://bugs.ruby-lang.org/issues/14133
2018-12-21 01:39:18 +09:00
Ryuta Kamizono
d5197d59a1 Use native Array#append, Array#prepend, Hash#transform_keys, and Hash#transform_keys!
Since Rails 6 requires Ruby 2.5.

https://github.com/ruby/ruby/blob/ruby_2_5/NEWS

Follow up #34754.
2018-12-20 23:22:46 +09:00
Ryuta Kamizono
3e50a1bcd4
Merge pull request #30973 from k0kubun/prefer-block-parameter
Unify _read_attribute definition to use &block
2018-12-20 17:38:32 +09:00
Takashi Kokubun
f3c866a743 Unify _read_attribute definition to use &block
Thanks to ko1, passing block parameter to another method is
significantly optimized in Ruby 2.5.
https://bugs.ruby-lang.org/issues/14045

Thus we no longer need to keep this ugly hack.
2018-12-20 10:37:15 +09: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
Ryuta Kamizono
ce48b5a366 Prevent infinit method_missing loop on attribute methods
If a klass has acceptance validation and then
`klass.undefine_attribute_methods` is happened before an attribute
method is called, infinit loop is caused on the `method_missing` defined
by the `LazilyDefineAttributes`.

https://travis-ci.org/rails/rails/jobs/467053984#L1409

To prevent the infinit loop, the `method_missing` should ensure
`klass.define_attribute_methods`.
2018-12-15 18:07:13 +09:00
Ryuta Kamizono
144b57d925 Merge pull request #34693 from ahorek/match
[perf] use #match?
2018-12-13 13:20:14 +09:00
pavel
01b0ccce32 use match? 2018-12-12 23:29:53 +01:00
Gannon McGibbon
d126c0d6c0 Fix numericality equality validation on floats 2018-12-12 12:02:12 -05:00
Rafael Mendonça França
db080527a4 Do not use deprecated Object#!~ in Ruby 2.6
Closes #34530.
2018-11-26 15:10:25 -05:00
Daniel Lopez Prat
ee2b84f3cb
Add slice! method to ActiveModel::Errors 2018-11-21 08:56:19 +09:00
Gannon McGibbon
5fb432e9c2 Re-add changes_applied doc
[ci skip]
2018-11-16 18:02:41 -05:00
Rafael França
24ffc15d4e
Merge pull request #34417 from r3trofitted/fix/added_options_check
Fix ignored options in the `#added?` method
2018-11-13 11:11:21 -05:00
Ronan Limon Duparcmeur
13b77fa1cb Fix ignored options in the #added? method
Fixes #34416
2018-11-13 09:00:20 +01:00
Ryuta Kamizono
a741208f80 Ensure casting by decimal attribute when querying
Related 34cc301f03aea2e579d6687a9ea9782afc1089a0.

`QueryAttribute#value_for_database` calls only `type.serialize`, and
`Decimal#serialize` is a no-op unlike other attribute types.

Whether or not `serialize` will invoke `cast` is undefined in our test
cases, but it actually does not work properly unless it does so for now.
2018-11-12 03:39:28 +09:00
Gannon McGibbon
e74fdbe00c Amend CVE note and security guide section wordings
Reword first sentence of dep management and CVE section of
security guide. Also, reword and move gemspec notes above deps.

[ci skip]
2018-11-06 18:06:57 -05:00
Gannon McGibbon
1c11688b56 Add CVE note to security guide and gemspecs
[ci skip]
2018-11-06 14:25:36 -05:00
Sean Griffin
b63701e272 update_columns raises if the column is unknown
Previosly, `update_columns` would just take whatever keys you gave it
and tried to run the update query. Most likely this would result in an
error from the database. However, if the column actually did exist, but
was in `ignored_columns`, this would result in the method returning
successfully when it should have raised, and an attribute that should
not exist written to `@attributes`.
2018-10-30 12:52:23 -06:00
Alberto Almagro
8d54f3ebaa Fix grammar in changed? docs [ci skip]
See https://english.stackexchange.com/questions/23218/anyone-has-or-anyone-have-seen-them
2018-10-28 17:20:27 +01:00
wilddima
26cdd01eab Add new exception message to datetime from hash cast 2018-10-21 10:05:48 +02:00
Dylan Thacker-Smith
99c87ad247 Improve model attribute accessor method names for backtraces
Ruby uses the original method name, so will show the __temp__ method
name in the backtrace. However, in the common case the method name
is compatible with the `def` keyword, so we can avoid the __temp__
method name in that case to improve the name shown in backtraces
or TracePoint#method_id.
2018-10-12 09:50:10 -07:00
Gannon McGibbon
c401c43850 Fix call sites 2018-10-02 15:31:29 -04:00
Sharang Dashputre
3c4b729f48 Fix spellings for 'unmarshall(ing/ed)' & 'marshall(ing/ed)' 2018-10-02 13:55:39 +05:30
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
Rafael França
6556898884
Merge pull request #30676 from artofhuman/import-assert-attrs-error-message
Improve error message when assign wrong attributes to model
2018-09-26 14:10:24 -04:00
Rafael Mendonça França
f679933daa
Change the empty block style to have space inside of the block 2018-09-25 13:19:35 -04:00
Kasper Timm Hansen
22dc2b3db8
Merge pull request #33949 from sjain1107/no-private-def
Remove private def
2018-09-23 19:39:15 +02:00
Sakshi Jain
0fe2bb816f Remove private def 2018-09-23 21:27:44 +05:30
yuuji.yaginuma
1b86d90136 Enable Performance/UnfreezeString cop
In Ruby 2.3 or later, `String#+@` is available and `+@` is faster than `dup`.

```ruby
# frozen_string_literal: true

require "bundler/inline"

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

  gem "benchmark-ips"
end

Benchmark.ips do |x|
  x.report('+@') { +"" }
  x.report('dup') { "".dup }
  x.compare!
end
```

```
$ ruby -v benchmark.rb
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux]
Warming up --------------------------------------
                  +@   282.289k i/100ms
                 dup   187.638k i/100ms
Calculating -------------------------------------
                  +@      6.775M (± 3.6%) i/s -     33.875M in   5.006253s
                 dup      3.320M (± 2.2%) i/s -     16.700M in   5.032125s

Comparison:
                  +@:  6775299.3 i/s
                 dup:  3320400.7 i/s - 2.04x  slower

```
2018-09-23 08:56:55 +09:00
Ryuta Kamizono
c3e569550c
Merge pull request #33804 from yskkin/num_string
Fix non_numeric_string?
2018-09-08 05:56:22 +09:00
Rafael França
dd29fabebf
Merge pull request #33615 from Larochelle/i18n_full_message_with_nested_attributes
`ActiveModel.full_message` interaction with `index_errors`
2018-09-07 13:24:09 -04:00
Yoshiyuki Kinjo
ba406d9c22 Fix non_numeric_string?
For example, dirty checking was not right for the following case:

```
model.int_column = "+5"
model.float_column = "0.5E+1"
model.decimal_column = "0.5e-3"
```

It is enough to see whether leading character is a digit for avoiding
invalid numeric expression like 'wibble' to be type-casted to 0, as
this method's comment says.

Fixes #33801
2018-09-07 16:17:15 +09:00
Ryuta Kamizono
736edb9828 Formatting CHANGELOGs [ci skip]
Fixing code block rendering, indentation, backticks, etc.
2018-09-07 07:59:19 +09:00
schneems
e81b0ddd7a Faster time_value.rb
The multiplication of the value takes a long time when we can instead mutate and use the string value directly.

The `microsec` perf increases speed by 27% in the ideal case (which is the most common).

```
original_string = ".443959"

require 'benchmark/ips'

Benchmark.ips do |x|
  x.report("multiply") { 
    string = original_string.dup
    (string.to_r * 1_000_000).to_i 
  }
  x.report("new     ") { 
    string = original_string.dup
    if string && string.start_with?(".".freeze) && string.length == 7
      string[0] = ''.freeze
      string.to_i
    end
  }
  x.compare!
end

# Warming up --------------------------------------
#             multiply   125.783k i/100ms
#             new        146.543k i/100ms
# Calculating -------------------------------------
#             multiply      1.751M (± 3.3%) i/s -      8.805M in   5.033779s
#             new           2.225M (± 2.1%) i/s -     11.137M in   5.007110s

# Comparison:
#             new     :  2225289.7 i/s
#             multiply:  1751254.2 i/s - 1.27x  slower
```
2018-08-29 11:45:47 -05:00
Ryuta Kamizono
47a6d788dd Fix numericality validator to still use value before type cast except Active Record
The purpose of fe9547b is to work type casting to value from database.

But that was caused not to use the value before type cast even except
Active Record.

There we never guarantees that the value before type cast was going to
the used in this validation, but we should not change the behavior
unless there is some particular reason.

To restore original behavior, still use the value before type cast if
`came_from_user?` is undefined (i.e. except Active Record).

Fixes #33651.
Fixes #33686.
2018-08-24 00:44:02 +09:00
Martin Larochelle
8d2f3179e6 Call human_attribute_name with a string instead of a symbole 2018-08-16 11:50:13 -04:00
Martin Larochelle
0b54641878 ActiveModel.full_message interaction with index_errors 2018-08-14 12:01:23 -04:00
Ryuta Kamizono
2fece9036d Fix numericality validator not to be affected by custom getter
Since fe9547b6, numericality validator would parse raw value only when a
value came from user to work type casting to a value from database.

But that was caused a regression that the validator would work against
getter value instead of parsed raw value, a getter is sometimes
customized by people. #33550

There we never guarantees that the value before type cast was going to
the used in this validation (actually here is only place that getter
value might not be used), but we should not change the behavior unless
there is some particular reason.

The purpose of fe9547b6 is to work type casting to a value from
database. We could achieve the purpose by using `read_attribute`,
without using getter value.

Fixes #33550.
2018-08-13 23:28:46 +09:00
bogdanvlviv
159dc60bee
Add changelog entry for #31503 [ci skip]
Related to #31503
2018-08-12 15:38:47 +03:00
bogdanvlviv
3a0a8cf604
Fix test failure
```
...
(snip)
............F
Failure:
JsonSerializationTest#test_as_json_should_return_a_hash_if_include_root_
in_json_is_true [/home/travis/build/rails/rails/activemodel/test/cases/serializers/json_serialization_test.rb:145]:
Expected: 2006-08-01 00:00:00 UTC
  Actual: "2006-08-01T00:00:00.000Z"
rails test home/travis/build/rails/rails/activemodel/test/cases/serializers/json_serialization_test.rb:136
(snip)
...
```

Related to #31503
2018-08-11 19:19:04 +03:00
Eileen M. Uchitelle
09e1452eeb
Merge pull request #31503 from bogdan/timestamp-as-json
Fix AM::Serializers::JSON#as_json method for timestamps
2018-08-11 10:37:49 -04:00
Bart
eb4f7cad20 ActiveModel::Naming delegate match? in the same manner as =~ and != (#33466)
The purpose of the module seems to quack like a string.
2018-07-29 19:58:35 +02:00
Dillon Welch
d108288c2f
Turn on performance based cops
Use attr_reader/attr_writer instead of methods

method is 12% slower

Use flat_map over map.flatten(1)

flatten is 66% slower

Use hash[]= instead of hash.merge! with single arguments

merge! is 166% slower

See https://github.com/rails/rails/pull/32337 for more conversation
2018-07-23 15:37:06 -07:00
Ryuta Kamizono
a4398e412c Merge pull request #30919 from seanlinsley/17622-before_save_strict_arguments
Add strict argument checking to ActiveRecord callbacks
2018-07-23 04:19:37 +09:00
Sean Linsley
dfb0e4b3dc add strict argument checking to ActiveRecord callbacks
This ends up adding it to all save-related callbacks defined in `ActiveRecord::DefineCallbacks`, including e.g. `after_create`. Which should be fine: they didn't support `:on` in the first place.
2018-07-22 12:51:47 -05:00
Jeremy Baker
a19918124d Ensure attribute is a symbol in the added? method 2018-07-14 13:19:46 -05:00