to_gmaps4rails(&blk) doesn't work with Mongoid #223

Closed
nithinbekal opened this Issue Jun 25, 2012 · 5 comments

2 participants

@nithinbekal

I'm not exactly sure if this is a bug or not, but it did confuse me for a while. I'm using the gmaps4rails gem version 1.5.2 with Mongoid. In my controller, I've been doing this:

    def index
      @places = Place.all.desc(:longitude)
      @json = @places.to_gmaps4rails do |place, marker|
        marker.json({id: place.slug})
      end
    end

The JSON generated by this doesn't contain the id field. It turns out you need to use @places.to_a.to_gmaps4rails.... for the id to appear in JSON. This is the code that worked for me:

      @json = @places.to_a.to_gmaps4rails do |place, marker|
        marker.json({id: place.slug})
      end

I haven't tried this with ActiveRecord, so I'm not sure if this is the expected behavior there, but most of the examples I see don't seem to have to_a called on it. Could you confirm if this is the expected behavior?

@apneadiving
@nithinbekal

Sure, I'll try adding this tomorrow and send in a pull request.

@nithinbekal

@apneadiving I got it to work by adding the method to Mongoid::Criteria. However, couldn't we avoid the duplication by adding the method to Enumerable module rather than the Array class? After all, Enumerable is included in Array as well as Mongoid::Criteria.

I ran the tests after moving to_gmaps4rails to Enumerable, and all the tests are running fine. This also works fine with Mongoid.

@apneadiving
@nithinbekal

Ok, I'll make the changes and send in a pull request later today.

@nithinbekal nithinbekal added a commit that referenced this issue Jun 26, 2012
@nithinbekal nithinbekal Add to_gmaps4rails method to Enumerable rather than Array. Fixes issue
…#223.

- Rename lib/gmaps4rails/extensions/array.rb to lib/gmaps4rails/extensions/enumerable.rb.
- Add redcar temp files to .gitignore.
99c2dd4
@apneadiving apneadiving closed this Jul 1, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment