ankane/chartkick

Sinatra: chart commands only work in a class that inherits from Sinatra::Base

blakegearin opened this issue · 5 comments

Describe the bug
When trying to move chart generation (e.g. line_chart data) deeper into my application's stack (specifically from HAML file to model), I noticed that line_chart could only be used/called in a class that inherits from Sinatra::Base. Perhaps this is a limitation of chartkick and not a bug? If it is a limitation, it should likely be mentioned in the documentation

To reproduce
Working demo here: https://github.com/blakegearin/chartkick-sinatra-base-bug

I've only tested this on one computer, so if runs differently on your machine please let me know.

HTML generated by Chartkick

N/A

Hey @blakegearin, line_chart won't be available in every Ruby class - it's only designed for views.

I see. It's interesting then that it's working in any class with Sinatra::Base no matter the file's position in the stack. Have any insight on this front?

Some relevant code I found:

# chartkick.js
require "chartkick/sinatra" if defined?(Sinatra)

# sinatra.rb
require "sinatra/base"

class Sinatra::Base
  helpers Chartkick::Helper
end

Sinatra helpers are also available in Sinatra::Base (and therefore any subclass): http://sinatrarb.com/extensions.html

Ok, seems like this behavior is expected then. Might be good to document somewhere that methods like line_charts are designed for views and potentially what steps can be taken for them to work other places

I appreciate the suggestion. Will add more documentation if this becomes a common point of confusion.