ankane / chartkick

Create beautiful JavaScript charts with one line of Ruby
https://chartkick.com
MIT License
6.33k stars 565 forks source link

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

Closed blakegearin closed 3 years ago

blakegearin commented 3 years ago

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

ankane commented 3 years ago

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

blakegearin commented 3 years ago

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
ankane commented 3 years ago

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

blakegearin commented 3 years ago

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

ankane commented 3 years ago

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