neo4jrb/neo4j — pull request #638 by subvertallchris (2014-12-14 04:51:12+00:00)
Title: From Query back to Query Proxy, OPTIONAL MATCH in QP

Had a long plane ride back from SF today and had some time to work on this. It adds two things that I've been dying for: a way to turn Core::Query objects back into QP objects and a way to use OPTIONAL MATCH in QueryProxy.

`proxy_as` creates a QueryProxy from a Core::Query instance. You do `my_query_object.proxy_as(Model, :identifier)`. An optional third argument, a boolean, controls whether the new QP object will start with `MATCH` (default, `false`) or `OPTIONAL MATCH`. Because I think that a contextless `true` as an argument is ugly, there's another method, `proxy_as_optional` that calls `proxy_as` with the third argument true, to help keep code readable.

By itself, `proxy_as` is enough to do optional matches in QueryProxy but it's a little wordy for such a common pattern. To make it easier, an `optional` QueryProxy method was also added to the QueryProxyMethods module. It's a shortcut for `proxy_as` that accepts an association name and an identifier, then calls the required methods to create a new `OPTIONAL MATCH`.

As part of this, I had to change the behavior of the `result` identifier because it had a chance of being duplicated if the user was going from QP to Query and then back to QP. It now tries to do `result_#{some information about the current QP target}`. I'm not totally in love with how it works but it does work, so it's a start.

Specs give examples of this in action. A test is failing in Travis and I'm not sure why, I'm not having any issues locally. I'll need to work on it more tomorrow.