Commit Graph

613 Commits

Author SHA1 Message Date
Christian Blais
17141481d9 Change RangeError to a more specific ActiveModel::RangeError
The should make it easier for apps to rescue ActiveModel specific
errors without the need to wrap all method calls with a generic
rescue RangeError.
2016-05-03 14:46:49 -04:00
Vipul A M
1f99bdb620 Add test case for interpolation with passing of data along with record attribute in error message, where a proc is passed. 2016-04-05 14:18:44 +05:30
Prathamesh Sonpatki
7bdb4b5598 Allow passing record being validated to error message generator
- Pass object to I18n helper so that when calling message proc, it will
  pass that object as argument to the proc and we can generate custom
  error messages based on current record being validated.
- Based on https://github.com/rails/rails/issues/856.

[Łukasz Bandzarewicz, Prathamesh Sonpatki]
2016-04-05 12:55:02 +05:30
Kasper Timm Hansen
044baef101 Merge pull request #24299 from kitop/activemodel-errors-include-fix
Do not create a hash key when calling ActiveModel::Errors#include?
2016-03-25 13:24:41 +01:00
Sean Griffin
c7d3bd48df Apply scale before precision when coercing floats to decimal
Since precision is always larger than scale, it can actually change
rounding behavior. Given a precision of 5 and a scale of 3, when you
apply the precision of 5 to `1.25047`, the result is `1.2505`, which
when the scale is applied would be `1.251` instead of the expected
`1.250`.

This issue appears to only occur with floats, as scale doesn't apply to
other numeric types, and the bigdecimal constructor actually ignores
precision entirely when working with strings. There's no way we could
handle this for the "unknown object which responds to `to_d`" case, as
we can't assume an interface for applying the scale.

Fixes #24235
2016-03-24 16:09:19 -06:00
Esteban Pastorino
9848c4632f Do not create a hash key when calling ActiveModel::Errors#include?
From: https://github.com/rails/rails/issues/24279

Problem:
By doing `record.errors.include? :foo`, it adds a new key to the
@messages hash that defaults to an empty array.

This happens because of a combination of these 2 commits:
b97035df64
(Added in Rails 4.1)
and
6ec8ba16d8 (diff-fdcf8b65b5fb954372c6fe1ddf284c78R76)
(Rails 5.0)

By adding the default proc that returns an array for non-existing keys,
ruby adds that key to the hash.

Solution:
Change `#include?` to check with `has_key?` and then check if that value is
`present?`.

Add test case for ActiveModels::Errors#include?
2016-03-24 16:29:22 -04:00
ojab
01b8a23a7c Add edge cases to Time/Date/DateTime inclusion validation tests 2016-03-24 11:36:02 +00:00
Arthur Neves
2abcdfd978 Remove load_paths file 2016-02-27 13:03:57 -05:00
Tara Scherner de la Fuente
926a24a751 remove args from assert_nothing_raised in tests 2016-02-22 22:56:23 -08:00
Andrey Novikov
0a0ffb64d8 Take UTC offset into account when assigning string value to time attribute. 2016-01-05 18:35:04 +03:00
Robert Eshleman
b96fdd234d Failing Tests for Validating String Numbericality
Covers Regressions:

* <=
* <
* ==
* >
* >=
* other than
2015-12-22 14:27:25 -05:00
Rafael França
b7a7e82207 Merge pull request #22598 from yui-knk/deprecate_string_callback
Deprecate passing string to define callback.
2015-12-16 13:54:02 -02:00
yui-knk
21f4017fd9 Deprecate passing string to define callback. 2015-12-16 19:56:20 +09:00
Vokhmin Alexey V
61e5081404 ActiveRecord::Base#becomes should copy the errors 2015-12-14 19:02:20 +03:00
keepcosmos
7a8031b578 add test for nested model translation 2015-12-03 14:37:05 +09:00
Sean Griffin
c78c43533e Fix test failures caused by #21000 2015-11-23 15:19:39 -07:00
Thomas Walpole
85f7d955f3 Update and fix forbidden attributes tests
Add AC::Parameters tests for WhereChain#not
2015-11-03 11:34:07 -08:00
Sean Griffin
0d216d1add Really fix test failures caused by #19851
Ok, this explains why the branch showed as green. We don't run files in
isolation for PRs, only for master. Active Support monkeypatches
`BigDecimal#to_s`, so the generated error message was different
depending on if the file was run in isolation
2015-10-20 18:08:50 -06:00
Sean Griffin
12df3391e3 Fix test failures caused by #19851
The error message when asserting `greater_than: BigDecimal.new` will
give an error message based on how BigDecimal displays itself. Big
decimal appears to always use scientific notation. This might not be the
best error message for the general case, but the general case wouldn't
use big decimal for the validation. And if they do, they likely need
this level of precision.
2015-10-20 17:49:47 -06:00
Sean Griffin
328ec26cbe Merge pull request #19851 from repinel/numericality-validation2
Use the post-type-cast version of the attribute to validate numericality
2015-10-20 17:05:44 -06:00
Sean Griffin
34321e4a43 Add an immutable string type to opt out of string duping
This type adds an escape hatch to apps for which string duping causes
unacceptable memory growth. The reason we are duping them is in order to
detect mutation, which was a feature added to 4.2 in #15674. The string
type was modified to support this behavior in #15788.

Memory growth is really only a concern for string types, as it's the
only mutable type where the act of coersion does not create a new object
regardless (as we're usually returning an object of a different class).

I do feel strongly that if we are going to support detecting mutation,
we should do it universally for any type which is mutable. While it is
less common and ideomatic to mutate strings than arrays or hashes, there
shouldn't be rules or gotchas to understanding our behavior.

However, I also appreciate that for apps which are using a lot of string
columns, this would increase the number of allocations by a large
factor. To ensure that we keep our contract, if you'd like to opt out of
mutation detection on strings, you'll also be option out of mutation of
those strings.

I'm not completely married to the thought that strings coming out of
this actually need to be frozen -- and I think the name is correct
either way, as the purpose of this is to provide a string type which
does not detect mutation.

In the new implementation, I'm only overriding `cast_value`. I did not
port over the duping in `serialize`. I cannot think of a reason we'd
need to dup the string there, and the tests pass without it.
Unfortunately that line was introduced at a time where I was not nearly
as good about writing my commit messages, so I have no context as to
why I added it. Thanks past Sean. You are a jerk.
2015-10-15 09:50:37 -07:00
Akira Matsuda
d30f934af0 AMo typos 2015-09-22 23:36:40 +09:00
Sean Griffin
dac7d0d046 Fix another implicit dependency of the AM test suite
Hopefully this is the last one
2015-09-21 10:50:36 -06:00
Sean Griffin
858a7b0429 Move the appropriate type tests to the Active Model suite
Any tests for a type which is not overridden by Active Record, and does
not test the specifics of the attributes API interacting in more complex
ways have no reason to be in the Active Record suite. Doing this
revealed that the implementation of the date and time types in AM was
actually completely broken, and incapable of returning any value other
than `nil`.
2015-09-21 10:12:31 -06:00
Sean Griffin
4590d7729e Simplify the implementation of Active Model's type registry
Things like decorations, overrides, and priorities only matter for
Active Record, so the Active Model registry can be implemented much more
simply. At this point, I wonder if having Active Record's registry
inherit from Active Model's is even worth the trouble?

The Active Model class was also missing test cases, which have been
backfilled.

This removes the error when two types are registered with the same name,
but given that Active Model is meant to be significantly more generic, I
do not think this is an issue for now. If we want, we can raise an error
at the point that someone tries to register it.
2015-09-21 10:12:31 -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
Rafael Mendonça França
51dd258843 Merge pull request #21069 from dmitry/feature/validate-multiple-contexts-at-once
Validate multiple contexts on `valid?` and `invalid?` at once
2015-09-07 16:46:18 -03:00
Prakash Laxkar
3ee1f7b40b Removed unused config file 2015-09-03 08:40:03 +05:30
Aditya Kapoor
69f3f81e6c Add missing test for #17351 2015-09-01 17:35:50 +05:30
Ronak Jangir
8d7bf97798 Removed duplicate requiring minitest/mock as it is already required in method_call_assertions 2015-08-26 19:43:54 +05:30
Zachary Scott
f7ebdb1ac5 Remove XML Serialization from core.
This includes the following classes:

- ActiveModel::Serializers::Xml
- ActiveRecord::Serialization::XmlSerializer
2015-08-07 11:01:48 -04: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
yuuji.yaginuma
0a20e48d52 pass the correct argument to mock on a test of validates_length_of 2015-07-28 22:59:42 +09:00
Roque Pinel
7500daec69 Conditionally convert the raw_value received by the numeric validator.
This fixes the issue where you may be comparing (using a numeric
validator such as `greater_than`) numbers of a specific Numeric type
such as `BigDecimal`.

Previous behavior took the numeric value to be validated and
unconditionally converted to Float. For example, due to floating point
precision, this can cause issues when comparing a Float to a BigDecimal.

Consider the following:

```
    validates :sub_total, numericality: {
      greater_than: BigDecimal('97.18')
    }
```

If the `:sub_total` value BigDecimal.new('97.18') was validated against
the above, the following would be valid since `:sub_total` is converted
to a Float regardless of its original type. The result therefore becomes
Kernel.Float(97.18) > BigDecimal.new('97.18')

The above illustrated behavior is corrected with this patch by
conditionally converting the value to validate to float.

Use the post-type-cast version of the attribute to validate numericality

[Roque Pinel & Trevor Wistaff]
2015-07-11 14:40:14 -04:00
Zamith
a9ab7e85fd Removes unnecessary comments from i18n validations tests [ci skip]
These comments do not add a lot to the readability, grepability or
overall understanding of the tests, therefore I believe they can be
safely removed.
2015-07-11 12:29:46 +01:00
Zamith
ec7771e7f6 Remove the reference to mocha in activemodel
Activemodel is no longer dependent on mocha, so we can make the comments
more generic.
2015-07-11 02:08:45 +01:00
Kasper Timm Hansen
8a389ffc10 Use private method call assertions in Active Model tests.
Also fix Minitest constant reference.
2015-07-10 23:45:42 +02:00
Radan Skoric
e5a78aaaf8 Improve Validation Helpers' documentation comments and tests 2015-06-27 16:34:16 +02:00
Anton Davydov
3f92a8247c Fix typo in AM I18n validation test name [skip ci] 2015-06-09 01:23:20 +03:00
Rafael Mendonça França
109e71d2bb Require yaml for isolation test
It was removed when we removed mocha at
5a6ae7f7539216931f2b3f4aa53394ac4136c74e
2015-05-29 18:22:39 -03:00
Roque Pinel
5a6ae7f753 Remove use of mocha from Active Model 2015-05-28 18:35:17 -05:00
claudiob
58fc63fd96 Stop skipping a test that now works on Rubinius
The test was skipped because of an issue that, in the meantime,
has been fixed: https://github.com/rubinius/rubinius/issues/3328.

Using the latest Rubinius (the one currently on Travis CI), this
is the result:

```sh
$ ruby --version
rubinius 2.5.3 (2.1.0 2482b093 2015-05-10 3.5.1 JI) [x86_64-darwin14.3.0]
```

**Before this PR**

```sh
$ ruby -Itest test/cases/attribute_assignment_test.rb
Run options: --seed 58569

.....S...

Finished in 0.048278s, 186.4203 runs/s, 269.2738 assertions/s.

9 runs, 13 assertions, 0 failures, 0 errors, 1 skips

You have skipped tests. Run with --verbose for details.
```

**After this PR**
$ ruby -Itest test/cases/attribute_assignment_test.rb
Run options: --seed 35720

.........

Finished in 0.029441s, 305.6961 runs/s, 475.5273 assertions/s.

9 runs, 14 assertions, 0 failures, 0 errors, 0 skips
```
2015-05-10 16:24:46 -07:00
Zamith
6f418a09d8
Adds/Corrects use case for adding an error message
I believe this is a use case that was supposed to be supported, and it's
a small fix.
2015-05-04 13:36:26 +01:00
Jay Elaraj
b2967999ae ensure method_missing called for non-existing methods passed to
`ActiveModel::Serialization#serializable_hash`
2015-04-28 21:06:30 -04:00
Fernando Tapia Rico
f072db8e4f Add ActiveModel::Dirty#[attr_name]_previously_changed? and
`ActiveModel::Dirty#[attr_name]_previous_change` to improve access
to recorded changes after the model has been saved.

It makes the dirty-attributes query methods consistent before and after
saving.
2015-04-21 19:30:46 +02:00
Rafael Mendonça França
7131f6ba22 Merge pull request #19021 from morgoth/activemodel-errors-refactoring
Simplify and alias ActiveModel::Errors methods where possible
2015-03-30 13:47:30 -03:00
Sean Griffin
1c341eb7cb Deprecate the :tokenizer option to validates_length_of
As demonstrated by #19570, this option is severely limited, and
satisfies an extremely specific use case. Realistically, there's not
much reason for this option to exist. Its functionality can be trivially
replicated with a normal Ruby method. Let's deprecate this option, in
favor of the simpler solution.
2015-03-29 16:34:01 -06:00
Carlos Antonio da Silva
a3f030686b Fix arguments order on assertion
The expected value comes first. Related to #19465.
2015-03-22 20:25:27 -03:00
Radan Skoric
26b35a4096 Fix ActiveModel::Errors#delete return value to stay backward compatible
Rails 5.0 changes to ActiveModel::Errors include addition of `details`
that also accidentally changed the return value of `delete`. Since
there was no test for that behavior it went unnoticed. This commit
adds a test and fixes the regression.

Small improvements to comments have also been made. Since `get` is
getting deprecated it is better to use `[]` in other methods' code
examples. Also, in the module usage example, `def Person.method`
was replaced with a more commonly used `def self.method` code style.
2015-03-22 23:18:22 +01:00