Skip to content

Fixes #38718 - Register feature scope in belongs_to_proxy#10677

Merged
ofedoren merged 1 commit intotheforeman:developfrom
adamruzicka:autocomplete-scope
Oct 3, 2025
Merged

Fixes #38718 - Register feature scope in belongs_to_proxy#10677
ofedoren merged 1 commit intotheforeman:developfrom
adamruzicka:autocomplete-scope

Conversation

@adamruzicka
Copy link
Contributor

@adamruzicka adamruzicka commented Sep 4, 2025

Copy link
Member

@ofedoren ofedoren left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @adamruzicka, makes sense.

Although, I'd like to see the issue filled with some information on why it's a useful feature or if it's intended to fix some issue.

Ideally, this should have some... example or showcase?..

Checked with scoped_search counterpart, seems to be working as intended.

@adamruzicka
Copy link
Contributor Author

Well, it is not exactly flashy, but this should allow proxy-feature-based search to offer you only proxies with relevant feature. Trying to search hosts by puppetmaster proxy should only offer you hosts with the puppetmaster feature, rather than offering you all the proxies you have, no matter if they have the right feature or not.

@adamruzicka
Copy link
Contributor Author

Additionally, the scoped search fix seems to address SAT-30581 as well

@adamruzicka adamruzicka marked this pull request as ready for review September 25, 2025 09:17
@adamruzicka adamruzicka requested a review from a team as a code owner September 25, 2025 09:17
@adamruzicka adamruzicka force-pushed the autocomplete-scope branch 3 times, most recently from eab67aa to ca04dc9 Compare September 25, 2025 10:07
evgeni
evgeni previously approved these changes Sep 25, 2025
Copy link
Member

@evgeni evgeni left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📦 ack, didn't look at the rest

@adamruzicka adamruzicka force-pushed the autocomplete-scope branch 5 times, most recently from 28bf8d2 to 914a2b3 Compare September 25, 2025 14:01
belongs_to :user, :class_name => 'User'
belongs_to :search_users, :class_name => 'User', :foreign_key => :user_id
belongs_to :search_hosts, -> { where(:audits => { :auditable_type => 'Host::Base' }) },
belongs_to :search_hosts, -> { joins('INNER JOIN audits ON audits.auditable_id = hosts.id AND audits.auditable_type LIKE \'Host::%\'') },
Copy link
Contributor Author

@adamruzicka adamruzicka Sep 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The scope (and the one for nics) was probably never evaluated before. The auditable type for hosts should be Host::Base, but Host::Base is not auditable so I had to hack around a bit. Having the join condition set to limit it to Host::Base should work too, but eh

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For anyone else: these were broken before changes to the scoped_search. It's just we noticed this due to the changes.

It seems we should've covered these with tests :/ Also, even with your fix (given that it at least works now) the results are might be weird (or that's me missing something). Essentially in some cases the results are the same as there was no scope defined (remove the whole lambda):

I've got a host with ID 1 and an audit record with auditable_id being also 1, but auditable_type being Setting. I'd assume that typing audit_record.search_hosts will return nil, but it finds that host instead and returns it, which was... unexpected. The same goes with search_nics:

Screenshot-1758811763970

Is that expected?

Copy link
Contributor Author

@adamruzicka adamruzicka Sep 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that expected?

Not really. Added a fix

Edit: god dammit rails, it takes an argument optionally

ArgumentError: The association scope 'search_nics' is instance dependent (the scope block takes an argument). Eager loading instance dependent scopes is not supported.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hrm, I've probably run out of tricks to make this somehow work, I'll sleep on it.

In case I come up short, would it be possible to accept it's current limitations? I assume the association is there primarily for scoped search to work, with primary audit->resource access point being Audit#auditable.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if we go with a workaround, which seems to make that association work and return the valid results? Something like:

    belongs_to :search_hosts, :class_name => 'Host::Base', :foreign_key => :auditable_id

    def search_hosts
      return nil unless auditable_type&.match?(/^(::)?Host::/)
      super
    end

@adamruzicka adamruzicka force-pushed the autocomplete-scope branch 5 times, most recently from 059ecb3 to 5e587f8 Compare September 25, 2025 15:56
@adamruzicka
Copy link
Contributor Author

This was getting out of hand, so I went ahead and forked off the "compatibility with newer scoped search" into #10703 . I'd prefer to first get that thing green and merged and then start leveraging the new features

@ofedoren
Copy link
Member

ofedoren commented Oct 1, 2025

@adamruzicka, this awaits for a rebase :)

Copy link
Member

@ofedoren ofedoren left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @adamruzicka, the feature seems to be working just fine, so ACK from that perspective.

The bug with Audits I've mentioned earlier is still present though :/ Not sure if it should be handled within this PR (probably not).

Test failures should not be related.

@adamruzicka
Copy link
Contributor Author

Not sure if it should be handled within this PR (probably not).

Could you please file an issue for that?

Copy link
Member

@ofedoren ofedoren left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please file an issue for that?

Done: https://projects.theforeman.org/issues/38798

As for this PR, let's get this in :)

@ofedoren ofedoren merged commit 8c2f07f into theforeman:develop Oct 3, 2025
127 of 148 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants