Managing namespace conflicts with Jbuilder/rspec when creating boolean queries/filters with "should"


(Loren Siebert) #1

I'm posting this in case someone else runs across this problem.

If you are using Jbuilder https://github.com/rails/jbuilder to compose
your JSON for an Elasticsearch boolean queryhttp://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html you
might end up using the "should" typed occurrence.

$ irb
2.0.0p195 :001 > require 'jbuilder'
=> true
2.0.0p195 :002 > Jbuilder.encode do |json|
2.0.0p195 :003 > json.should do
2.0.0p195 :004 > json.foo "this works"
2.0.0p195 :005?> end
2.0.0p195 :006?> end
=> "{"should":{"foo":"this works"}}"

But if you also use RSpec http://rspec.info in your project, you will run
into this namespace conflict:

2.0.0p195 :007 > require 'rspec'
=> true
2.0.0p195 :008 > Jbuilder.encode do |json|
2.0.0p195 :009 > json.should do
2.0.0p195 :010 > json.foo "this does not work"
2.0.0p195 :011?> end
2.0.0p195 :012?> end
=> "{}”

Here is the workaround:
2.0.0p195 :013 > Jbuilder.encode do |json|
2.0.0p195 :014 > json.set! :should do
2.0.0p195 :015 > json.foo "this works again"
2.0.0p195 :016?> end
2.0.0p195 :017?> end
=> "{"should":{"foo":"this works again"}}"

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/9749e705-9447-4af6-839c-546c69021a0a%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


(system) #2