Commit Graph

6 Commits

Author SHA1 Message Date
Teo Ljungberg fbd4e42c19 Remove duplicated method `model`
Which quells the following warning:

```
warning: method redefined; discarding old model
```
2018-01-02 14:35:41 -02:00
Elliot Winkler 1189934806 Add ignoring_interference_by_writer to all matchers
`allow_value` matcher is, of course, concerned with setting values on a
particular attribute on a particular record, and then checking that the
record is valid after doing so. That comes with a caveat: if the
attribute is overridden in such a way so that the same value going into
the attribute isn't the same value coming out of it, then `allow_value`
will balk -- it'll say, "I can't do that because that changes how I
work."

That's all well and good, but what the attribute intentionally changes
incoming values? ActiveRecord's typecasting behavior, for instance,
would trigger such an exception. What if the developer needs a way to
get around this? This is where `ignoring_interference_by_writer` comes
into play. You can tack it on to the end of the matcher, and you're free
to go on your way.

So, prior to this commit you could already apply it to `allow_value`,
but now in this commit it also works on any other matcher.

But, one little thing: sometimes using this qualifier isn't going to
work. Perhaps you or something else actually *is* overriding the
attribute to change incoming values in a specific way, and perhaps the
value that comes out makes the record fail validation, and there's
nothing you can do about it. So in this case, even if you're using
`ignoring_interference_by_writer`, we want to inform you about what the
attribute is doing -- what the input and output was. And so we do.
2016-01-05 00:58:16 -07:00
Elliot Winkler 9e8603eb74 allow_value: Fix compatibility with enum columns
When used against an attribute that's an enum, `allow_value` will raise
an AttributeValueChangedError. This commit prevents that from happening.
2016-01-05 00:58:16 -07:00
Elliot Winkler a4a8a5afce Adjust message for AttributeDoesNotExistError
Make it a little more clear that the attribute mentioned is on the
model.
2015-12-30 22:16:32 -05:00
Elliot Winkler 6b3253147a allow_value: Inspect values more clearly
Modify descriptions and failure messages for all matchers by way of
allow_value and put small angle brackets around inspected values. This
is to visually distinguish an inspected value from the rest of the text,
and is especially noticeable for complex values such as an array that
contains an object, particularly if the inspected version of the value
wraps onto another line. It's a little easier to see:

    When attempting to set :attr on Example to ‹[#<Child id:
    nil>]›...

rather than:

    When attempting to set :attr on Example to [#<Child id:
    nil>]...
2015-12-30 21:51:54 -05:00
Elliot Winkler 2962112114 allow_value: pre-set attributes before validation
While attempting to add support for `ignoring_interference_by_writer` to
the confirmation matcher, I was noticing that there are two attributes
we are concerned with: the attribute under test, and the confirmation
attribute -- for instance, `password` and `password_confirmation`. The
way that the matcher works, `password_confirmation` is set first on the
record before `password` is set, and then the whole record is validated.
This is fine, but I also noticed that `allow_value` has a specific way
of setting attributes -- not only does it check whether the attribute
being set exists and fail properly if it is does not, but it also
raises a CouldNotSetAttribute error if the attribute changes incoming
values. This logic needs to be performed on both `password_confirmation`
as well as `password`.

With that in mind, `allow_value` now supports a `values_to_preset=`
writer method which allows one to assign additional attributes unrelated
to the one being tested prior to validation. This will be used by the
confirmation matcher in a future commit.

This means that `allow_value` now operates in two steps:

1. Set attributes unrelated to the test, raising an error if any of the
   attributes do not exist on the model.
2. Set the attribute under test to one or more values, raising an error
   if the attribute does not exist, then running validations on the
   record, failing with an appropriate error message if the validations
   fail.

Note that the second step is similar to the first, although there are
more things involved. To that end, `allow_value` has been completely
refactored so that the logic for setting and validating attributes
happens in other places. Specifically, the core logic to set an
attribute (and capture the results) is located in a new AttributeSetter
class.

Also, the CouldNotSetAttributeError class has been moved to a namespace
and renamed to AttributeChangedValueError.

Finally, this commit fixes DisallowValueMatcher so that it is the true
opposite of AllowValueMatcher: DVM#matches? calls AVM#does_not_match?
and DVM#does_not_match? calls AVM#matches?.
2015-12-30 21:34:02 -05:00