-
Notifications
You must be signed in to change notification settings - Fork 87
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
Breaks shoulda's validate_uniqueness_of matcher #141
Comments
Yes, the uniqueness validation in Mobility does not handle case sensitivity, so this is an expected result. I should maybe document that somewhere. The problem is that Mobility has to do validation in a backend-independent way, and doing a |
Thanks for your response. So this means that Mobility replaces the default mechanism or what? And that replacement doesn't watch out for LOWER comparison? IMHO, I think there should be at least an option to force LOWER comparison somewhere? |
There is no mechanism for translated attributes, since they are not "real" attributes. Mobility adds a roughly-compatible uniqueness validation. I added it with some hesitation because I knew that it would not be complete. I won't add any backend-specific conditions like e.g. I believe you are using the column backend. In this case, you can validate uniqueness of the column, with the locale suffix. So you could I'm closing this since it's not fixable. |
p.s. If you want to ask more about what you could do to handle this validation, please add a question on SO with the "mobility" tag. |
Hmm, ok re-opening this, I misread the error message. It's saying validation is failing for case-sensitive uniqueness, which should work. |
So I checked this in a small application, and the matchers work for me. What you posted looks suspicious. You're testing that the model validates uniqueness scoped to Ah, ok now I see, this one will fail since it's testing with case insensitivy. But the one you posted above (scoped to customer) should work. I'm closing this again, but I will add a warning message if the case sensitive option is passed in so that people know that the Mobility validator does not support this. Thanks for posting. |
To anyone here: case-insensitive uniqueness validation now works as of 0.7.0. |
I'd like to bring this one up again, as I still can't seem to find a solution that's working for me. If I have this: validates :title, uniqueness: {scope: :parent_id} And this: it { should validate_uniqueness_of(:title).scoped_to :parent_id } I get this error:
I tried validating the languages separately: validates :title_de, uniqueness: {scope: :parent_id}
validates :title_en, uniqueness: {scope: :parent_id} With the original validation: it { should validate_uniqueness_of(:title).scoped_to :parent_id } I get this error:
With testing on each language separately... it { should validate_uniqueness_of(:title_de).scoped_to :parent_id }
it { should validate_uniqueness_of(:title_en).scoped_to :parent_id } It seems to work:
But this feels wrong to me: the model should be language agnostic. And apparently, in my feature specs, a lot of tests now fail with the following error:
When writing my own spec, everything seems to work as expected: # In User.rb
# validates uniqueness: {scope: :parent_id}
it 'validates language-specific uniqueness of title (scoped to parent_id)' do
page = create :page, title: 'English title',
title_de: 'Deutscher Titel',
creator: @user
new_page = build :page, creator: @user
# Try to set same title (should be rejected)
new_page.title = page.title
expect(new_page.valid?).to be_falsy
expect(new_page.errors[:title]).to include 'has already been taken'
# Try to set different title (should be accepted)
new_page.title = 'New english title'
expect(new_page.valid?).to be_truthy
# Try to set same title as in German (should be accepted)
new_page.title = page.title_de
expect(new_page.valid?).to be_truthy
# Try to set same title, but with different parent_id (should be accepted)
new_page.title = page.title
new_page.parent = page
expect(new_page.valid?).to be_truthy
end But it would be much easier to rely on the generic matchers. |
I'll repeat what I wrote before:
And of course, people expect too much from this. Translated attributes are not "attributes" in the AR sense, they do the dance as well as they can but when you take things too far they break. Uniqueness validation is a basic feature meant to help people a bit, but it is not close to 100% compatible with standard AR uniqueness validation (it simplifies some things to avoid huge complexity). I was happy to add case-insensitivity since it's something that makes the feature much more useful, but it's not meant to be entirely compatible with the original version. FWIW though, I suspect the reason your specs are passing and the shoulda matcher one is failing has to do with the scope condition ( If you have an actual test which fails (one that you wrote yourself), I would be happy to add it and try to fix it if it's reasonable. Maybe the scope condition doesn't handle |
Thank you for taking the time for another statement, @shioyama. I think it finally reached my brain that not everything can be expected as usual when adding complex functionality, and I will have to add a work-around every now and then. Thanks a lot for your great gem, it really serves me well. |
Thanks for being understanding. I don't mean to be blunt about these things, but I believe it's very important for open source projects to define and defend their boundaries in order to remain maintainable. So I try to be very clear about what the gem should and should not do (although I'm always more receptive if someone else is going to do the coding part 😉). |
I just came across a similar situation, but my issue was due to |
I'm testing uniqueness validations using shoulda like this:
Since I introduced mobility, this doesn't work for translated attributes anymore:
When trying manually, the validation works as always. Only the shoulda matcher seems to have a problem.
The text was updated successfully, but these errors were encountered: