Commit Graph

127 Commits

Author SHA1 Message Date
Edouard CHIN
b677adede0 Move the ActiveModel:Errors#full_message method to the Error class:
- One regression introduced by the "AM errors as object" features is
  about the `full_messages` method.

  It's currently impossible to call that method if the `base` object
  passed in the constructor of `AM::Errors` doesn't respond to the
  `errors` method.
  That's because `full_messages` now makes a weird back and forth trip

  `AM::Errors#full_messages` -> `AM::Error#full_message` -> `AM::Errors#full_message`

  Since `full_message` (singular) isn't needed by AM::Errors, I moved
  it to the `AM::Error` (singular) class. This way we don't need to
  grab the `AM::Errors` object from the base.
2019-07-16 14:28:38 +02:00
lulalala
655036b09a Fix spec as generate_message is no longer called during validation 2019-03-31 22:59:12 +08:00
lulalala
db0256cad7 Fix misalignment caused by SHA eebb9ddf9ba559a510975c486fe59a4edc9da97d 2019-03-31 22:59:12 +08:00
Rafael França
f2cd46bd04
Merge pull request #35424 from Korri/validation-rules-locale-fallback
Fall back to parent locale before falling back to the :errors namespace
2019-03-11 21:47:38 -04:00
Samantha John
755112c7b1 Replace “can not” with “cannot”. 2019-03-06 16:35:52 -05:00
Hugo Vacher
2176f4b30c Fall back to parent locale before it falls back to the :errors namespace 2019-03-04 16:54:06 -05:00
Ryuta Kamizono
9def05385f Deprecate mismatched collation comparison for uniquness validator
In MySQL, the default collation is case insensitive. Since the
uniqueness validator enforces case sensitive comparison by default, it
frequently causes mismatched collation issues (performance, weird
behavior, etc) to MySQL users.

https://grosser.it/2009/12/11/validates_uniqness_of-mysql-slow/
https://github.com/rails/rails/issues/1399
https://github.com/rails/rails/pull/13465
c1dddf8c7d
https://github.com/huginn/huginn/pull/1330#discussion_r55152573

I'd like to deprecate the implicit default enforcing since I frequently
experienced the problems in code reviews.

Note that this change has no effect to sqlite3, postgresql, and
oracle-enhanced adapters which are implemented as case sensitive by
default, only affect to mysql2 adapter (I can take a work if sqlserver
adapter will support Rails 6.0).
2019-03-04 21:00:03 +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
1dc17e7b2e Fix CustomCops/AssertNot to allow it to have failure message
Follow up of #32605.
2018-05-13 11:32:47 +09:00
Daniel Colson
a1ac18671a Replace assert ! with assert_not
This autocorrects the violations after adding a custom cop in
3305c78dcd.
2018-04-19 08:11:33 -04:00
Eddie Lebow
5645149d3a
Deprecate update_attributes and update_attributes!
Closes #31998
2018-02-17 13:58:54 -08:00
Daniel Colson
82c39e1a0b Use assert_empty and assert_not_empty 2018-01-25 23:32:59 -05:00
Daniel Colson
94333a4c31 Use assert_predicate and assert_not_predicate 2018-01-25 23:32:59 -05:00
Daniel Colson
211adb47e7 Change refute to assert_not 2018-01-25 23:32:58 -05:00
Kir Shatrov
883b2a8cee Check :scope input in Uniqueness validator 2017-08-13 19:39:55 +03:00
Kir Shatrov
831be98f9a Use frozen-string-literal in ActiveRecord 2017-07-19 22:27:07 +03:00
Matthew Draper
87b3e226d6 Revert "Merge pull request #29540 from kirs/rubocop-frozen-string"
This reverts commit 3420a14590c0e6915d8b6c242887f74adb4120f9, reversing
changes made to afb66a5a598ce4ac74ad84b125a5abf046dcf5aa.
2017-07-02 02:15:17 +09:30
Kir Shatrov
cfade1ec7e Enforce frozen string in Rubocop 2017-07-01 02:11:03 +03:00
Vijay Dev
12038f6505 Merge branch 'master' of github.com:rails/docrails 2017-03-04 19:35:25 +00:00
Ryuta Kamizono
3a72b555cf Remove duplicated "does" [ci skip] 2017-02-14 23:20:25 +09:00
Koichi ITO
0f50538ab4 Fix a tests of AR::ValueTooLong when using OracleAdapter 2017-02-14 11:38:57 +09:00
Sergey Alekseev
8a0fda63aa fix the uniqueness validation scope with a polymorphic association
https://gist.github.com/sergey-alekseev/946657ebdb5e58d1bee115714056ec96
2016-11-26 18:49:00 +03:00
Rafael Mendonça França
fe1f4b2ad5
Add more rubocop rules about whitespaces 2016-10-29 01:17:49 -02:00
Ryuta Kamizono
3464cd5c28 Fix broken comments indentation caused by rubocop auto-correct [ci skip]
All indentation was normalized by rubocop auto-correct at 80e66cc4d90bf8c15d1a5f6e3152e90147f00772.
But comments was still kept absolute position. This commit aligns
comments with method definitions for consistency.
2016-09-14 18:26:32 +09:00
Rafael Mendonça França
55f9b8129a
Add three new rubocop rules
Style/SpaceBeforeBlockBraces
Style/SpaceInsideBlockBraces
Style/SpaceInsideHashLiteralBraces

Fix all violations in the repository.
2016-08-16 04:30:11 -03:00
Ryuta Kamizono
762e3f05f3 Add Style/EmptyLines in .rubocop.yml and remove extra empty lines 2016-08-07 17:50:59 +09:00
Xavier Noria
b326e82dc0 applies remaining conventions across the project 2016-08-06 20:20:22 +02:00
Xavier Noria
411ccbdab2 remove redundant curlies from hash arguments 2016-08-06 19:44:11 +02:00
Xavier Noria
d22e522179 modernizes hash syntax in activerecord 2016-08-06 19:37:57 +02:00
Xavier Noria
9617db2078 applies new string literal convention in activerecord/test
The current code base is not uniform. After some discussion,
we have chosen to go with double quotes by default.
2016-08-06 18:26:53 +02:00
sukesan1984
3bd30d9824 Add i18n_validation_test
add active record uniqueness validation test for { on: [:create, :update] } condition.
2016-06-02 04:17:15 +00:00
Jeremy Daer
f2f2d64429
Merge pull request #23523 from kamipo/avoid_truncation_in_uniqueness_validation
Avoid a string value truncation in uniqueness validation
2016-04-18 17:53:22 -07:00
yuuji.yaginuma
8b18feb077 remove outdated comment [ci skip]
Currently use minitest mock, mocha is not used.
2016-03-05 07:47:39 +09:00
eileencodes
ecf6dc3dfb Fix test name typo prescence -> presence 2016-02-27 11:20:03 -05:00
Joe Rafaniello
0379da6abc Fix uniqueness validation with an after_create hook.
record.id_was is nil in after_create/after_save, so we should use
id in these cases.

While this logic feels incomplete, the existing update_record uses the same
logic:
2fda4e0874/activerecord/lib/active_record/relation.rb (L83)

This logic was originally added for a similar problem:
updates not working with after_create hook.

See: 482f8c15b1d699c95bfbc3d836f674a09c0d9031

Followup to #23581
Fixes #23844
2016-02-25 10:13:12 -05:00
eileencodes
2c02bc0a47 Revert changes to validations from PR #18612
In order to fix issue #17621 we added a check to validations that
determined if a record should be validated. Based on the existing tests
and behavior we wanted we determined the best way to do that was by
checking if `!record.peristed? || record.changed? || record.marked_for_destruction?`

This change didn't make it into a release until now. When #23790 was
opened we realized that `valid?` and `invalid?` were broken and did not
work on persisted records because of the `!record.persisted?`.

While there is still a bug that #17621 brought up, this change was too
drastic and should not be a RC blocker. I will work on fixing this so
that we don't break `valid?` but also aren't validating parent records
through child records if that parent record is validate false. This
change removes the code changes to validate and the corresponding tests.
It adds tests for two of the bugs found since Rails 5 beta2 release.

Fixes #17621
2016-02-23 15:21:46 -05:00
Sean Griffin
96c1ada82d Merge pull request #23628 from maclover7/fix-23625
Fix issue #23625
2016-02-23 12:24:07 -07:00
Ian Fleeton
e3cfbca3b2 Match test name to test (typo) 2016-02-21 21:00:49 +00:00
eileencodes
6f15b276cb Always validate record if validating a virtual attribute
Fixes #23645

When you're using an `attr_accessor` for a record instead of an
attribute in the database there's no way for the record to know if it
has `changed?` unless you tell it `attribute_will_change!("attribute")`.

The change made in 27aa4dd updated validations to check if a record was
`changed?` or `marked_for_destruction?` or not `persisted?`. It did not
take into account virtual attributes that do not affect the model's
dirty status.

The only way to fix this is to always validate the record if the
attribute does not belong to the set of attributes the record expects
(in `record.attributes`) because virtual attributes will not be in that
hash.

I think we should consider deprecating this particular behavior in the
future and requiring that the user mark the record dirty by noting that
the virtual attribute will change. Unfortunately this isn't easy because
we have no way of knowing that you did the "right thing" in your
application by marking it dirty and will get the deprecation warning
even if you are doing the correct thing.

For now this restores expected behavior when using a virtual attribute
by always validating the record, as well as adds tests for this case.

I was going to add the `!record.attributes.include?(attribute)` to the
`should_validate?` method but `uniqueness` cannot validate a virtual
attribute with nothing to hold on to the attribute. Because of this
`should_validate?` was about to become a very messy method so I decided
to split them up so we can handle it specifically for each case.
2016-02-20 09:15:39 -05:00
Jon Moss
55385c8a39 Fix issue #23625
This resolves a bug where if the primary key used is not `id` (ex:
`uuid`), and has a `validates_uniqueness_of` in the model, a uniqueness error
would be raised. This is a partial revert of commit `119b9181ece399c67213543fb5227b82688b536f`, which introduced this behavior.
2016-02-18 00:32:38 -05:00
Ryuta Kamizono
811a4fa8eb Avoid a string value truncation in uniqueness validation
In MySQL, PostgreSQL, Oracle and SQLServer, a value over the limit
cannot be inserted or updated (See #23522).

In SQLite3, a value is inserted or updated regardless of the limit.

We should avoid a string value truncation in uniqueness validation.
2016-02-12 23:57:15 +09:00
Diego Silva
f1daa2be52 UniquenessValidator exclude itself when PK changed
When changing the PK for a record which has a uniqueness validation on
some other attribute, Active Record should exclude itself from the
validation based on the PK value stored on the DB (id_was) instead of
its new value (id).
2016-02-09 21:28:21 -02:00
Kassio Borges
c8bbe9aefa Improve support for non Active Record objects on validates_associated
Skipping `marked_for_destruction?` when the associated object does not responds
to it make easier to validate virtual associations built on top of Active Model
objects and/or serialized objects that implement a `valid?` instance method.
2015-11-08 10:58:39 -02:00
Akira Matsuda
dcecbb4234 File encoding is defaulted to utf-8 in Ruby >= 2.1 2015-09-18 17:05:05 +09:00
Ronak Jangir
796c83fca9 Removed mocha from Active Record Part 1 2015-08-25 22:07:25 +05:30
Yves Senn
50e4afff20 uniqueness validation raises error for persisted record without pk.
Closes #21304.

While we can validate uniqueness for record without primary key on
creation, there is no way to exclude the current record when
updating. (The update itself will need a primary key to work correctly).
2015-08-20 12:07:02 +02:00
Sean Griffin
119b9181ec Properly allow uniqueness validations on primary keys.
This is an alternate implementation of #20966.

[Sean Griffin & presskey]
2015-07-25 13:05:53 -06:00
Yves Senn
30c9aa7883 AR absence validator respects marked_for_destruction?. Closes #20449.
Associated objects that were marked for destruction are considered absent.
2015-06-22 15:45:08 +02:00
Sean Griffin
67c2deeb4b Merge branch 'fix_uniqueness_validation_when_value_is_out_of_range' 2015-04-08 11:07:39 -06:00
Andrey Voronkov
1a36be390e Fix uniqueness validation with out of range value 2015-04-08 19:28:45 +03:00