Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

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

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

nithinbekal opened this issue Jun 25, 2012 · 5 comments

Comments

@nithinbekal
Copy link
Contributor

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
Copy link
Owner

Actually, I've added to_gmaps4rails to the Array class.

It should be added it to the Criteria class I guess. Could you try this?

Sent from my iPhone

On 25 juin 2012, at 14:22, Nithin Bekalreply@reply.github.com wrote:

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?


Reply to this email directly or view it on GitHub:
#223

@nithinbekal
Copy link
Contributor Author

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

@nithinbekal
Copy link
Contributor Author

@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
Copy link
Owner

this is a very good idea!
100% agreed

On Jun 26, 2012, at 9:19 AM, Nithin Bekal wrote:

@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.


Reply to this email directly or view it on GitHub:
#223 (comment)

@nithinbekal
Copy link
Contributor Author

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

apneadiving added a commit that referenced this issue Jul 1, 2012
Fix issue #223: to_gmaps4rails(&blk) doesn't work with Mongoid
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants