New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make ValidateUniquenessOfMatcher method signature consistent #722
Make ValidateUniquenessOfMatcher method signature consistent #722
Conversation
…n optional boolean argument This makes it consistent with ValidateInclusionOfMatcher
We really need this optional boolean? If your answers is positive I think we need to add tests and document this. I add this to I think the regular use of this qualifier is like: class Post < ActiveRecord::Base
validates_uniqueness_of :author_id, allow_nil: true
end
# RSpec
describe Post do
it { should validate_uniqueness_of(:author_id).allow_nil }
end But if I do not want to allow nil I can use the negative form like: class Post < ActiveRecord::Base
validates_uniqueness_of :author_id
end
# RSpec
describe Post do
it { should_not validate_uniqueness_of(:author_id).allow_nil }
end I am missing something? Do you guys have a use case to this? Thanks |
Ask @mcmire In your example, without optional boolean support, it { should validate_uniqueness_of(:author_id) }
it { should_not validate_uniqueness_of(:author_id).allow_nil }
# Then I might also need
it { should_not validate_uniqueness_of(:author_id).allow_blank } If this PR is merged, it becomes: it { should validate_uniqueness_of(:author_id).allow_nil(false).allow_blank(false) } |
@PikachuEXE thanks for your feedback. |
@PikachuEXE @maurogeorge I think it's fine for all of the boolean qualifiers to take an optional value. It reads better in the true case ( The examples you two have mentioned would work regardless of whether this takes an optional argument. This change wouldn't affect existing tests. |
So...do I need to make more changes to this PR? |
@PikachuEXE Yes, we need to add tests for this. |
I see there is a pre-release version coming out (or came out) |
Actually I just re-read over this and realized that we probably don't need tests for this. Are people going to use The pre-release version already came out, yeah, so I probably won't get to merging this until after that is released. |
@mcmire I agree with you that use But if us assume that no one will use the IMO this optional param is a hidden feature, since this is not documented on RDoc. I think we can embrace this as a feature(adding docs and tests to this) or remove this hidden feature(removing the optional param). I am not sure about version if this will be a MAJOR or MINOR. I know that will break this API if we remove the optional param, but is this a real API or a hidden feature since this is not on docs. |
It's only optional because in the true case, people leave off the
If we removed the default value, then we'd be forcing people to change all of their |
@mcmire following your docs you are right, the example is showed with the default it { should validate_uniqueness_of(:author_id).allow_nil } I don't mean remove the default param, but remove the param at all. Change the def allow_nil
@options[:allow_nil] = true
self
end
Since no one will use the param, what you think about remove this? |
Ohh... sorry, I misunderstood. I see what you're saying now. So on one hand, accepting an explicit argument creates a symmetry between validation options and matcher qualifiers. When I first started working on shoulda-matchers, I had this idea that if a validation takes an option, the corresponding matcher should take a qualifier with the same name. In other words, you should be able to look at your validation and pretty easily guess what the corresponding qualifier on the matcher would be. So if you see That said, we don't completely follow this idea right now. There are a few cases where the qualifier is named differently than the validation option ( But actually... symmetry doesn't matter so much in this case. As a counterpoint, something else to consider is that boolean qualifiers are actually different from normal qualifiers. When you use them, they add an extra test that the matcher will perform -- but only if you're using them in a
Unless I'm misunderstanding, it sounds like you think that if you say
Perhaps it should do something meaningful. But the fact is, if your validation doesn't have an In light of this last point, I agree with @maurogeorge that we should take off optional parameters for boolean qualifiers across the board (considering it isn't documented anyway), so that people won't get misled about how they work. |
@mcmire
I guess I am imaging it while reading the code :P Since this PR does not actually solve anything, close it when appropriate. |
Yeah, we can't always test that the record fails validation if the attribute is nil, because this is only guaranteed to happen for |
After some discussion[1] was decided that these qualifiers do not accept any parameter. thoughtbot#722
After some discussion[1] was decided that these qualifiers do not accept any parameter. thoughtbot#722
After some discussion[1] was decided that these qualifiers do not accept any parameter. #722
After some discussion[1] was decided that these qualifiers do not accept any parameter. thoughtbot#722
After some discussion[1] was decided that these qualifiers do not accept any parameter. #722
This makes it consistent with ValidateInclusionOfMatcher
For #721