Coder Social home page Coder Social logo

Comments (4)

matsales28 avatar matsales28 commented on June 2, 2024

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!

from shoulda-matchers.

thedean7 avatar thedean7 commented on June 2, 2024

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') }

from shoulda-matchers.

matsales28 avatar matsales28 commented on June 2, 2024

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.

def extract_relation_clause_from(relation, name)
case name
when :conditions
relation.where_values_hash

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.

https://github.com/rails/rails/blob/254f1d8ded07dfd73462b40d6aaa8c19032259f6/activerecord/lib/active_record/relation.rb#L772-L774

@mcmire I'd love to hear your thoughts on this.

from shoulda-matchers.

thedean7 avatar thedean7 commented on June 2, 2024

Thank you, @matsales28. I appreciate the detailed explanation.

from shoulda-matchers.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.