Commit Graph

91 Commits

Author SHA1 Message Date
Chris Salzberg
42a8340aa8
Add validation to subclass in tests to avoid polluting parent class
These two tests currently both define acceptance validators on the same
class, Topic. This means that in either one test or the other, there are
not one but *two* instances of the LazilyDefineAttributes module
builder in the class' ancestors, which can result in unpredictable
results.

Subclassing Topic in each test avoids conflicts.
2019-04-13 22:20:56 +09:00
Ryuta Kamizono
d99984d48c Add test case for ce48b5a366482d4b4c4c053e1e39e79d71987197 2018-12-18 11:18:18 +09: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
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
Daniel Colson
94333a4c31 Use assert_predicate and assert_not_predicate 2018-01-25 23:32:59 -05:00
Yasuo Honda
6c6c3fa166 Suppress warning: BigDecimal.new is deprecated in activerecord
`BigDecimal.new` has been deprecated in BigDecimal 1.3.3
 which will be a default for Ruby 2.5.

Refer 533737338d

```
$ cd rails/activerecord/
$ git grep -l BigDecimal.new | grep \.rb | xargs sed -i -e "s/BigDecimal.new/BigDecimal/g"
```

- Changes made only to Active Record. Will apply the same change to
other module once this commit is merged.

- The following deprecation has not been addressed because it has been
reported at `ActiveRecord::Result.new`. `ActiveRecord::Result.ancestors`
did not show `BigDecimal`.

* Not addressed

```ruby
/path/to/rails/activerecord/lib/active_record/connection_adapters/mysql/database_statements.rb:34:
warning: BigDecimal.new is deprecated
```

* database_statements.rb:34

```ruby
ActiveRecord::Result.new(result.fields, result.to_a) if result
```

* ActiveRecord::Result.ancestors

```ruby
[ActiveRecord::Result,
 Enumerable,
 ActiveSupport::ToJsonWithActiveSupportEncoder,
 Object,
 Metaclass::ObjectMethods,
 Mocha::ObjectMethods,
 PP::ObjectMixin,
 ActiveSupport::Dependencies::Loadable,
 ActiveSupport::Tryable,
 JSON::Ext::Generator::GeneratorMethods::Object,
 Kernel,
 BasicObject]
```

This commit has been tested with these Ruby and BigDecimal versions

- ruby 2.5 and bigdecimal 1.3.3

```
$ ruby -v
ruby 2.5.0dev (2017-12-14 trunk 61217) [x86_64-linux]
$ gem list |grep bigdecimal
bigdecimal (default: 1.3.3, default: 1.3.2)
```

- ruby 2.4 and bigdecimal 1.3.0

```
$ ruby -v
ruby 2.4.2p198 (2017-09-14 revision 59899) [x86_64-linux-gnu]
$ gem list |grep bigdecimal
bigdecimal (default: 1.3.0)
```

- ruby 2.3 and bigdecimal 1.2.8

```
$ ruby -v
ruby 2.3.5p376 (2017-09-14 revision 59905) [x86_64-linux]
$ gem list |grep -i bigdecimal
bigdecimal (1.2.8)
```

- ruby 2.2 and bigdecimal 1.2.6
```
$ ruby -v
ruby 2.2.8p477 (2017-09-14 revision 59906) [x86_64-linux]
$ gem list |grep bigdecimal
bigdecimal (1.2.6)
```
2017-12-13 21:29:06 +00: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
Bradley Priest
d83b8e6510 Fix regression in Numericality validator where extra decimal places on
a user input for a decimal column were ignored by numerically validations
2017-05-27 21:58:35 +08:00
Yasuo Honda
be9dc78fcc Creating a new Topic class instead of class_eval for the existing one
since it affects another test `ReflectionTest#test_read_attribute_names`

Address #26099
2016-08-11 15:38:40 +00: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
Tara Scherner de la Fuente
926a24a751 remove args from assert_nothing_raised in tests 2016-02-22 22:56:23 -08:00
Sean Griffin
37661bfc81 validates_acceptance_of shouldn't require a database connection
The implementation of `attribute_method?` on Active Record requires
establishing a database connection and querying the schema. As a general
rule, we don't want to require database connections for any class macro,
as the class should be able to be loaded without a database (e.g. for
things like compiling assets).

Instead of eagerly defining these methods, we do it lazily the first
time they are accessed via `method_missing`. This should not cause any
performance hits, as it will only hit `method_missing` once for the
entire class.
2015-09-25 07:54:38 -06:00
Dmitry Polushkin
e3d99e239d Validate multiple contexts on valid? and invalid? at once.
Example:

```ruby
class Person
  include ActiveModel::Validations

  attr_reader :name, :title
  validates_presence_of :name, on: :create
  validates_presence_of :title, on: :update
end

person = Person.new
person.valid?([:create, :update])    # => true
person.errors.messages               # => {:name=>["can't be blank"], :title=>["can't be blank"]}
```
2015-09-07 22:42:50 +01:00
Rafael Mendonça França
7b9b0b531f Revert "Merge pull request #21069 from dmitry/feature/validate-multiple-contexts-at-once"
This reverts commit 51dd2588433457960cca592d5b5dac6e0537feac, reversing
changes made to ecb4e4b21b3222b823fa24d4a0598b1f2f63ecfb.

This broke Active Record tests
2015-09-07 17:16:54 -03:00
Dmitry Polushkin
86e3b047ba Validate multiple contexts on valid? and invalid? at once.
Example:

```ruby
class Person
  include ActiveModel::Validations

  attr_reader :name, :title
  validates_presence_of :name, on: :create
  validates_presence_of :title, on: :update
end

person = Person.new
person.valid?([:create, :update])    # => true
person.errors.messages               # => {:name=>["can't be blank"], :title=>["can't be blank"]}
```
2015-07-30 10:05:29 +01:00
Sean Griffin
101c19f55f Allow a symbol to be passed to attribute, in place of a type object
The same is not true of `define_attribute`, which is meant to be the low
level no-magic API that sits underneath. The differences between the two
APIs are:

- `attribute`
  - Lazy (the attribute will be defined after the schema has loaded)
  - Allows either a type object or a symbol
- `define_attribute`
  - Runs immediately (might get trampled by schema loading)
  - Requires a type object

This was the last blocker in terms of public interface requirements
originally discussed for this feature back in May. All the
implementation blockers have been cleared, so this feature is probably
ready for release (pending one more look-over by me).
2015-02-06 11:51:13 -07:00
Vipul A M
6eced6a1fe Removed magic comments # encoding: utf-8 , since its default from ruby 2.0 onwards. 2015-02-03 20:51:40 +05:30
Sean Griffin
704c658531 Ensure numericality validations work with mutation
The detection of in-place changes caused a weird unexpected issue with
numericality validations. That validator (out of necessity) works on the
`_before_type_cast` version of the attribute, since on an `:integer`
type column, a non-numeric string would type cast to 0.

However, strings are mutable, and we changed strings to ensure that the
post type cast version of the attribute was a different instance than
the before type cast version (so the mutation detection can work
properly).

Even though strings are the only mutable type for which a numericality
validation makes sense, special casing strings would feel like a strange
change to make here. Instead, we can make the assumption that for all
mutable types, we should work on the post-type-cast version of the
attribute, since all cases which would return 0 for non-numeric strings
are immutable.

Fixes #17852
2014-12-01 05:31:44 -07:00
Arun Agrawal
9ae210ba09 Build fix when running in isolation
`Computer` class needs to be require

See #17217 for more details
2014-11-14 10:24:11 +01:00
Bogdan Gusiev
0fa4c95d91 Add AR::Base#validate! method
Acts same as valid? but raises AR::RecordInvalid exception
if validation fails
2014-06-23 16:11:06 +03:00
Carlos Antonio da Silva
d4fafeb003 Merge pull request #10662 from take/change-test-name-for-ActiveRecord--Validations#valid-
Refactor AR's validations_test.rb
2014-05-11 19:26:56 -03:00
Henrik Nyh
2e70f44123 ActiveRecord/ActiveModel '#validate' alias for 'valid?'
It's unintuitive to call '#valid?' when you want to run validations but
don't care about the return value.

The alias in ActiveRecord isn't strictly necessary (the ActiveModel
alias is still in effect), but it clarifies.
2014-03-27 17:56:14 +01:00
Carlos Antonio da Silva
c5b76b5362 Prefer assert_raise instead of flunk + rescue to test for exceptions
Change most tests to make use of assert_raise returning the raised
exception rather than relying on a combination of flunk + rescue to
check for exception types/messages.
2013-12-19 09:20:51 -02:00
Takehiro Adachi
05746fe14f Refactor AR's validations_test.rb
The `:context` switch feature is implemented in
ActiveRecord::Validations#valid? method, so we should rename the test
names, and execute `valid?` in the test.

Change test name in AR's validations_test.rb

This test is testing save method's code
2013-12-03 20:44:11 +09:00
Akshay Vishnoi
e0326f56b4 Typo fixes [ci skip] 2013-11-30 20:27:01 +05:30
Guillermo Iguaran
8020f71df1 Remove mass assignment security from ActiveRecord 2012-09-16 23:58:19 -05:00
Jon Leighton
40a711ce29 remove tests for #with_scope (it's now deprecated) 2012-04-25 17:52:28 +01:00
Jon Leighton
0b72a04d0c Deprecate set_table_name in favour of self.table_name= or defining your own method. 2011-11-29 20:13:36 +00:00
Aaron Patterson
61774e0d49 please use ruby -I lib:test path/to/test.rb, or export RUBY_OPT 2011-06-06 15:47:13 -07:00
Jon Leighton
253bb6b926 Refactor Active Record test connection setup. Please see the RUNNING_UNIT_TESTS file for details, but essentially you can now configure things in test/config.yml. You can also run tests directly via the command line, e.g. ruby path/to/test.rb (no rake needed, uses default db connection from test/config.yml). This will help us fix the CI by enabling us to isolate the different Rails versions to different databases. 2011-06-04 23:47:03 +01:00
Carlos Antonio da Silva
9b610049bb Cleanup deprecation warnings in active record
Signed-off-by: José Valim <jose.valim@gmail.com>
2010-09-06 13:39:55 +02:00
Subba Rao Pasupuleti
b0c7dee4f2 removing unused models from tests
[#5153 state:resolved]

Signed-off-by: José Valim <jose.valim@gmail.com>
2010-07-21 22:08:07 +02:00
Neeraj Singh
e11bb95d56 Validators should at model level and not at AR:Base level [Closes #4804]
[#4804 state:resolved]

Signed-off-by: David Heinemeier Hansson <david@loudthinking.com>
2010-06-10 11:50:42 -04:00
José Valim
ad4be3d75d Fix failing test. 2010-05-21 16:20:56 +02:00
Neeraj Singh
1bc6b43f53 Replace assert with assert_equal in some test cases
[#4654 state:resolved]

Signed-off-by: José Valim <jose.valim@gmail.com>
2010-05-21 16:00:49 +02:00
Pratik Naik
88b4a8fcaf Remove undocumented save_without_validation! 2010-05-11 15:39:29 +01:00
José Valim
5c245b91d2 Make sure valid? preceives the context as in ActiveModel API (ht: Carlos Antonio) 2010-05-10 12:28:38 +03:00
Josh Kalderimis
e17ff6d617 updated AR to work with the AMo model validation changes 2010-05-08 23:51:36 +03:00
Santiago Pastorino
44c46558a4 test_validates_acceptance_of_as_database_column fixed
[#3826 state:committed]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
2010-02-05 16:08:08 -08:00
José Valim
40c4a0036a Ensure deprecated validate methods are invoked when they are private [#3214 status:resolved] 2010-01-18 09:56:36 +01:00
José Valim
c0d31ca41b save(false) is gone, use save(:validate => false) instead. 2010-01-17 14:22:34 +01:00
José Valim
8c5fe60ec8 Simplify repair_validations on AR and make it work with new callbacks. 2010-01-01 03:20:38 +01:00
Jeremy Kemper
949c8c0d0e Don't publicize with_scope for tests since it may shadow public misuse 2009-12-28 14:07:23 -08:00
José Valim
e714b499cc Move validator, human_name and human_attribute_name to ActiveModel, remove deprecated error messages and add i18n_scope and lookup_ancestors.
Signed-off-by: Carl Lerche <carllerche@mac.com>
2009-10-20 17:52:32 -07:00
Akira Matsuda
68b2b730e4 Fix default_error_messages back to the original message
Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
2009-09-11 18:34:12 -07:00
José Valim
4f37b97033 Changed ActiveRecord to use new callbacks and speed up observers by only notifying events that are actually being consumed.
Signed-off-by: Joshua Peek <josh@joshpeek.com>
2009-09-08 10:26:39 -05:00