mongoid / origin

A Ruby DSL for building MongoDB queries
http://mongoid.org/en/origin/index.html
MIT License
62 stars 29 forks source link

Coerce non-array objects to Array in Origin::Extensions::Array::ClassMethods::evolve #66

Closed wyattisimo closed 11 years ago

wyattisimo commented 11 years ago

Other field values seem to coerce to their specified field type, but Array does not.

If person.name is specified as type String, then person.name = 75 results in "75".

If person.fake_names is specified as type Array, then person.fake_names = "Jim" results in "Jim", but the expected result should be ["Jim"].

NOTE: On line 160, you could alternatively do something like evolve(object.split(/\s*,\s*/)) rescue evolve([object]) to make it assume that strings containing commas should be split into separate array elements. I did not include it in this pull request because it may be an unnecessary assumption.

durran commented 11 years ago

Thank you sir.

durran commented 11 years ago

Pulled into master.

wyattisimo commented 11 years ago

I discovered that this commit is revealing a problem in Origin::Selector. If a field is defined as an Array, the evolve method recurses down to individual array elements and causes them to be coerced to arrays themselves.

For example, the following query (where the location field is defined as an array):

Places.find_by(location: [73, 40])

produces the following selector:

{"$query"=>{"location"=>[[73], [40]]}}

Would it be a good idea to stop recursion when value and serializer.type are both arrays?

see: https://github.com/mongoid/origin/blob/master/lib/origin/selector.rb#L104-L113

wyattisimo commented 10 years ago

@durran Not sure if you saw my comment from 2 months ago. Should I open a new pull request?

durran commented 10 years ago

Yeah sorry, been all over the place. Please a new pull would be great or I'll get this fixed.