Hi Stephan,
I have taken a look at the automatic scopes code and it's not easy to
"recover" it so you can do "Model.hobo_search" or something similar.
What you can do is monkey patch your app instead of Hobo, by copying the
method's logic:
class ActiveRecord::Base
def self.hobo_search(query, *fields)
match_keyword = %w(PostgreSQL
PostGIS).include?(::ActiveRecord::Base.connection.adapter_name) ?
"ILIKE" : "LIKE"
words = (query || "").split
args = []
word_queries = words.map do |word|
field_query = '(' + fields.map { |field|
field = "#{self.table_name}.#{field}" unless field =~ /\./
"(#{field} #{match_keyword} ?)"
}.join(" OR ") + ')'
args += ["%#{word}%"] * fields.length
field_query
end
self.where *([word_queries.join(" AND ")] + args)
end
end
That piece of code should add the hobo_search method to all your models.
Alternatively, I think that patching Hobo so the "search" scope is
equivalent to "hobo_search" sounds like a good solution, taking into
account the number of gems that overwrite "Model.search". I think this
can help future users when they get stuck in the same problem. What do
you think?
Warm regards,
Ignacio
El 08/05/14 11:32, Stefan Haslinger escribió:
> Hi folks!
>
> I have got a Webshop applacation with a structure of 1700 products in
> 400 product categories through categorizations,
> and 1600 product properties in 1100 property groups and their 105 000
> values.
>
> For filtering I wanted to have a facetted search on category level on
> the values, like in the screenshot
>
> <https://lh6.googleusercontent.com/-O2AkKA8OjcU/U2s5li2AyYI/AAAAAAAAAT8/TaWbbtJj4RQ/s1600/facets.png>
>
> So I came up with the idea of integrating Elastic search into hobo.
> The result can be seen in the screenshot as well. I documented it in the
> Gist <https://gist.github.com/haslinger/fe3b8f15432e08daf565> .
>
> The one thing I don't like is that Searchkick's search method/scope
> shadows Hobo's one, so I can't use live-search in an admin subsite in a
> different controller anymore.
>
> I tried to rescue it using alias method
>
> alias_method :hobo_search, :search
>
> which does not work out. As far as I understand Hobo's search is created
> using something like method missing.
>
> I found, I could rescue it for me by changing
>
> when name == "search"
>
> to
>
> when name == "search" || name == "hobo_search"
>
> in the hob gem in /hobo/lib/hobo/model/scopes/automatic_scopes.rb line 321
>
> I would be happy if anybody could come up with a better solution for
> this, because I don't want to monky patch hobo for elastic search.
>
> As far as I understand this overwriting is also the case for Sunspot
> because Kevin uses a search method in his controller in
> http://www.hobocentral.net/tutorials/57-how-to-get-full-text-search as well.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Hobo Users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to [email protected]
> <mailto:[email protected]>.
> To post to this group, send email to [email protected]
> <mailto:[email protected]>.
> Visit this group at http://groups.google.com/group/hobousers.
> For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Hobo
Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/hobousers.
For more options, visit https://groups.google.com/d/optout.