ankane/chartkick — pull request #94 by dontfidget (2014-06-03 03:16:34+00:00)
Title: allow chartkick to fetch json from the current url if remote option given

After I had about 20 chartkick graphs on a page, my requests would start to time out and I didn't really want to change my code structure just to do separate remote fetches. So I came up with this:

```
<%= line_chart(remote: true) { Task.group_by_day(:completed_at).count } %>
```

In your controller, you specify:

```
include Chartkick::Remote
chartkick_remote only: [:show]
```

The controller changes allow you to server json for a single chart, although you do actually render the whole page on each request, but without the charts. It just sets up remote data sources for each chart with a number indicating which chart it wants the data for. You can end up repeating a lot of work if you do a lot of work outside the block to render the page, but if everything is nice and lazily evaluated, it can be really nice. I've been using this methodology for a few months now, but I had to patch chartkick internals to do it and I thought it would be cool to share it.

Note that it might also make sense to have this block syntax just work without the remote option so you can just turn it on and off as you please.

Finally, there's a nice gist at https://gist.github.com/maccman/5790509 providing any easy way to serialize your ajax requests if you don't want to swamp your server.

I hope you'll consider this. Unfortunately, I have not provided tests for the controller side of things because (a) it would require bringing all/muchof rails into your test suite, but more because (b) I couldn't figure out how to test anonymous controllers with minitest.