yui_tree.rb
Hipposoft 2009
A Rails interface to the Yahoo Interface Library tree component, for YUI version 2.7.0.
06-Apr-2009 (ADH): Created.
Simple helper method which builds an object suitable for rendering as JSON and sending to the YUI tree as node data. Pass an object ID, name to show in the tree and ‘true’ if this is a leaf, else ‘false’. Returns an appropriate Ruby object upon which “to_json” may be called. Call using code such as “::make_node_object”.
The text given in the second parameter is made HTML-safe with “h(…)”. The output must be made JS-safe somehow, e.g. by “render :js => <objects>” for one returned object or an array of them in controller handling XHR requests in a “format.js do …” block or by the necessity of turning the objects into a JS equivalent by calling “to_json”, which handles escaping for you.
An optional fourth parameter can be given - this is used as an extra class name on the node text. You can specialise individual node appearance with this and CSS, but only if you make custom nodes externally. If you use the default internal code for acts-as-tree-like data sets, no special class name will be applied to any nodes.
# File lib/yui_tree/yui_tree.rb, line 382 def self.make_node_object( id, name, isLeaf, className = nil ) { :id => id.to_s,#id.to_i, :label => ERB::Util.h( name.to_s ), :isLeaf => !! isLeaf, :className => className } end