Open thedean7 opened 1 year ago
Hello! I hope you're doing well. Thank you for raising this issue. After quickly reviewing the source code, I believe you can achieve the desired outcome by combining both where clauses within the conditions method, as shown in the example below.
it { should have_many(:active_website_resources)
.conditions(resource: { status: :active }, is_active: true)
.class_name('WebsiteResource') }
Please try using this code and see if it works as expected. Let me know if you need any further assistance!
Hi!
I tried many combinations, inline and chained but the result is the same. The culprit is nested condition. Order of conditions matters but attribute names and values of nested condition do not.
Only is_active
is tested, nested attribute is ignored. Notice attribute changes and order:
# passes
it { should have_many(:active_website_resources)
.conditions(whatever: { whatever: :whatever }) # <==
.conditions(is_active: true)
.class_name('WebsiteResource') }
# fails
it { should have_many(:active_website_resources)
.conditions(is_active: true)
.conditions(whatever: { whatever: :whatever })
.class_name('WebsiteResource') }
Inline conditions:
# passes
it { should have_many(:active_website_resources)
.conditions(is_active: true, whatever: { whatever: :whatever })
.class_name('WebsiteResource') }
# fails
it { should have_many(:active_website_resources)
.conditions(is_active: false, whatever: { whatever: :whatever })
.class_name('WebsiteResource') }
# passes
it { should have_many(:active_website_resources)
.conditions(whatever: { whatever: :whatever }, is_active: true)
.class_name('WebsiteResource') }
# fails
it { should have_many(:active_website_resources)
.conditions(whatever: { whatever: :whatever }, is_active: false)
.class_name('WebsiteResource') }
Yes, I dived into the code now, and we are using the where_values_hash
to build a hash of conditions and compare it with the given conditions for the matcher modifier.
Unfortunately, this method only returns conditions for the queried primary table, in this case, "website_resources", so any nested condition will be discarded. So, making this spec using shoulda is impossible for now 😠.
We could implement a new solution to build the hash using some of the methods Rails uses on the where_values_hash
, but I'm not sure if that's a nice approach because they're internal and not even documented methods.
@mcmire I'd love to hear your thoughts on this.
Thank you, @matsales28. I appreciate the detailed explanation.
Not sure if I'm missing something...
Scope in model:
This test passes:
But when conditions are reversed then it fails:
with message: