algolia / scout-extended

Scout Extended: The Full Power of Algolia in Laravel
https://www.algolia.com/doc/framework-integration/laravel/getting-started/introduction-to-scout-extended/
MIT License
397 stars 84 forks source link

[Bugfix] Allow toSearchableArray on Traits #283

Closed juampi92 closed 3 years ago

juampi92 commented 3 years ago
Q A
Bugfix? yes
New feature? no
BC breaks? no
Need Doc update no

What problem is this fixing?

The UpdateJob class has a little trick that checks if the model has a toSearchableArray implementation, and if it doesn't, it will apply a generic transformation.

Given that every searchable model has to implement Laravel Scout's Searchable trait, the check logic had to be wrapped around that. A simple method_exists wouldn't cut it.

To solve this, this package was making sure that the definition of the method toSearchableArray was the actual model's class.

. . .

The problem with this implementation comes when you want to group behavior inside a trait (especially for big models). At Studocu we usually do this, and it took us a while to notice that the method toSearchableArray inside our DocumentSearchable trait for the Document model was being wrapped with an extra transformer.

Describe your change

To add support for custom Traits (or even class inheritance), I changed the way we check if the toSearchableArray method is defined.

Instead of checking that the method's definition is in the Model class, we're now checking that the method's definition doesn't come from the Scout/Searchable trait.

juampi92 commented 3 years ago

It appears that the tests are failing with the message Invalid Application-ID or API key. Is there anything else I should do?

DevinCodes commented 3 years ago

Thank you for the PR @juampi92 ! Don't worry about the failing tests for now, there's currently an issue with the credentials for forked PRs. I'll dig into and review your PR later today or tomorrow morning!

DevinCodes commented 3 years ago

This is released in v1.19.0. Thank you again for contributing!