-
Notifications
You must be signed in to change notification settings - Fork 470
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
Support for anonymous model classes #1172
Comments
This ensures that if IndexSet is customised, that customised class gets this method called as well, rather than the original implementation. Of course, if `reference_name` isn’t customised, then it’s the original implementation that does get invoked, and that’s fine. This just keeps things consistent - always use whatever configuration’s index_set_class is set to. This was prompted by thinking through how to support anonymous models as discussed in #1172 by @kalsan.
So, I've just pushed commit 16f2e66 that deals with the first monkey-patch to some extent… custom IndexSet subclasses have been allowed for some time, but # this can all go within an initialiser if you like…
class AnonymousModelsIndexSet < ThinkingSphinx::IndexSet
def self.reference_name(klass)
# implement this however you like
end
end
Rails.application.config.to_prepare do
ThinkingSphinx::Configuration.instance.index_set_class = AnonymousModelsIndexSet
end I've not yet figured out something for the second monkey-patch though… there's no existing customisation around that. I don't want to add support for rails_ops itself, because that ties TS to rails_ops internals. So, I'll think a bit further and see if I can find a neater way to allow further customisation to happen. |
Okay, I think I've got an approach for the second monkey-patch, as per f33642d: Instead of changing the Translator, you can instead replace the # in the index definition:
has custom_class_column, as: :sphinx_internal_class, type: :string, :facet => true
# in the model:
def custom_class_column
self.class.model_name.name
end Both of these changes are in the |
Hi Pat Thanks for your quick reaction! The adjustments sound great :-) I'll upgrade to 5.0.0 as soon as it's out. Looking forward to that! Cheers |
Hi Kalsan - I just wanted to let you know that the newly released v5.0.0 release of Thinking Sphinx includes the two changes discussed here: There's also a significant change with needing to specify callbacks - though this is more of a big deal for those not using real-time indices: https://github.com/pat/thinking-sphinx/releases/tag/v5.0.0 |
Hi Pat This is awesome, thanks a lot! The patch seems no longer required. I still have another question about callbacks which I will post separately. Thank you! |
Fore more fine-grained model control, I instantiate the model in an anonymous class that inherits from the original model class. However thinking-sphinx is unable to handle this situation since model.class is nil.
A concrete error scenario is using thinking-sphinx together with the gem RailsOps (https://github.com/sitrox/rails_ops).
To support this, two changes are necessary to thinking-sphinx. They can be monkey-patched in the the gem as follows:
It would be great if this or a better solution could be implemented in the upcoming release of thinking-sphinx. It should be no trouble to implement it and the change would allow support for patterns with anonymous classes.
Edit: Monkey patch 1 is flawed, use this instead:
The text was updated successfully, but these errors were encountered: