Rails: CSS Graph and “wrong number of arguments”
Posted on February 9, 2007
Filed Under /dev/ruby | 959 views |
There's a great plugin for Rails called CSS Graphs Helper that does, well, just that: graphs things. However the instructions on that page say to implement a graph like so:
-
<%= bar_graph ['Gouda', 24], ['Havarti', 9], ['Provolone', 81], ['GjeitÃst', 57], ['Stilton', 42] %>
This threw a "wrong number of arguments (5 for 1)" error when I tested it out.
Turns out that code really needs to be:
-
<%= bar_graph [['Gouda', 24], ['Havarti', 9], ['Provolone', 81], ['GjeitÃst', 57], ['Stilton', 42]] %>
Note the extra set of square brackets surrounding the included items.
Update: Upon further inspection it seems this plugin was designed only to fill a niche that the plugin author needed and isn't really suitable for anyone else to use. Far, far too many hard-coded values and CSS values embedded inside the Ruby code of the plugin? That's just gross. Unless your graph is always going to contain five multi-coloured bars that should never be more than 100px high, I can't recommend this plugin as-is.
The core concept is impressive, don't get me wrong, but it really does just scratch one itch for one guy, apparently one time.
I've spent the last eight hours or so refactoring it to remove the hard-coding. A few more hours of work and my version should be reuseable and flexible.
Update, the second: Turns out doing development on a live plugin is not a very good idea since bugs introduced seems to destabilize the entire application, often to the point of preventing lighttpd from running (hey, I never said my code always worked right the first time...or the second...). So things have gone from a little refactoring of the existing plugin to rewriting it entirely as a class that returns instances of bar graphs that can then be passed to partials for rendering.
This new approach should offer two additional benefits (beyond allowing me to bash about without having to restart the http server): the complete abstraction of logic from presentation and multiple graphs displayed multiple ways on the same page (the current plugin states that its implementation only allows one graph per page).
Just to be clear: none of this is a knock against the original developers or their code. It obviously does exactly what they needed exactly as they need it done and it works quite nicely. And without it, I wouldn't be anywhere near where I am with my implementation (...with is, quite honestly, standing here with a bunch of code fragments, half-baked ideas and broken implementations littering my project, but it's a start!). I am indebted to them for providing their plugin as they did.
Comments
Leave a Reply