ankane/searchkick — pull request #107 by nragaz (2014-01-24 17:10:56+00:00)
Title: Extract search into a class

I ran into a bug where options passed to where/facets were being mutated (so using the same options hash multiple times didn't work). When I went in to fix the bug, I discovered that `search` was one giant function.

I presume this was to avoid introducing new methods on the model class. But it also makes understanding, debugging and testing the `search` function difficult. In addition, the procs for `where_filters` and `term_filters` were being recreated for every search which is a minor inefficiency.

I extracted the `search` function's logic into its own class (Searchkick::Query), which is responsible for creating the payload and then fetching the results (it would be nice to separate these responsibilities, but the need to do facet limiting on the client-side makes this difficult).

I also removed the `delete` calls that were mutating the where/facet options.

Apart from correcting that bug, this refactor passes all existing tests and does not introduce any new functionality.

More refactoring would obviously be possible - there are still some very long methods in the `Query` class. But I wanted to get your feedback before taking this any further.