Scott Seago
2008-Jun-04 15:00 UTC
[Ovirt-devel] [Patch] fixed bug in tree widget, added Pool.full_nested_set
-------------- next part -------------- A non-text attachment was scrubbed... Name: expanded-subtree.patch Type: text/x-patch Size: 3806 bytes Desc: not available URL: <http://listman.redhat.com/archives/ovirt-devel/attachments/20080604/ed6354ec/attachment.bin>
Hugh O. Brock
2008-Jun-04 18:07 UTC
[Ovirt-devel] [Patch] fixed bug in tree widget, added Pool.full_nested_set
On Wed, Jun 04, 2008 at 11:00:45AM -0400, Scott Seago wrote:>> >From 1279c6f15ff10d2a2b4f907807f34a975322f465 Mon Sep 17 00:00:00 2001 > From: Scott Seago <sseago at redhat.com> > Date: Wed, 4 Jun 2008 10:52:35 -0400 > Subject: [PATCH] fixed a problem in the move tree widget: the '+' expand button was appearing even for pools that didn't have any child HW pools, and the move tree filters out VM pools. > > Also added full_set_nested api method to Pool which returns the subtree below (and including) a pool in a nested hash rather than as a flat list. > Just call pool.full_set_nested to get default hash structure, which includes :id, :obj, and :children. > pool.full_set_nested(:method => :minimal_hash_element) (or similar) to use a different hash-generating method on pool. Currently supported are :hash_element, :minimal_hash_element, and json_hash_element. > To set ":selected => true" for the current element, pass in :selected_id=><your id> > > Signed-off-by: Scott Seago <sseago at redhat.com> > --- > wui/src/app/models/pool.rb | 53 +++++++++++++++++++++++++++++++++++-------- > 1 files changed, 43 insertions(+), 10 deletions(-) > > diff --git a/wui/src/app/models/pool.rb b/wui/src/app/models/pool.rb > index 277b0ce..ca3b1f9 100644 > --- a/wui/src/app/models/pool.rb > +++ b/wui/src/app/models/pool.rb > @@ -154,15 +154,11 @@ class Pool < ActiveRecord::Base > end > def self.pool_hash(pools, open_list, filter_vm_pools=false) > pools.collect do |pool| > - hash = {} > - hash[:id] = pool.id > - hash[:type] = pool[:type] > - hash[:text] = pool.name > - hash[:name] = pool.name > - children = nil > + hash = pool.json_hash_element > + pool_children = nil > if filter_vm_pools > - children = select_hardware_pools(pool.children) > - hash[:hasChildren] = !children.empty? > + pool_children = pool.sub_hardware_pools > + hash[:hasChildren] = !pool_children.empty? > else > hash[:hasChildren] = pool.hasChildren > end > @@ -171,8 +167,8 @@ class Pool < ActiveRecord::Base > if pool.id == open_pool.id > new_open_list = open_list[(open_list.index(open_pool)+1)..-1] > unless new_open_list.empty? > - children = pool.children unless children > - hash[:children] = pool_hash(children, new_open_list) > + pool_children = pool.children unless pool_children > + hash[:children] = pool_hash(pool_children, new_open_list, filter_vm_pools) > hash[:expanded] = true > hash.delete(:hasChildren) > end > @@ -183,6 +179,43 @@ class Pool < ActiveRecord::Base > end > end > > + def json_hash_element > + { :id => id, :type => self[:type], :text => name, :name => name} > + end > + > + def hash_element > + { :id => id, :obj => self} > + end > + > + def minimal_hash_element > + { :id => id} > + end > + > + # if opts specifies order, this will be removed, since this impl > + # relies on full_set's ordering > + # in additon to standard find opts, add :method to the hash to specify > + # an alternative set of attributes (such as json_hash_element, etc.) > + # or :current_id to specify which pool gets ":selected => true" set > + def full_set_nested(opts={}) > + method = opts.delete(:method) {:hash_element} > + current_id = opts.delete(:current_id) > + opts.delete(:order) > + subtree_list = full_set(opts) > + return_tree = send(method) > + ref_hash = { id => return_tree} > + subtree_list.each do |pool| > + unless pool.id==return_tree[:id] > + new_element = pool.send(method) > + ref_hash[pool.id] = new_element > + parent = ref_hash[pool.parent_id] > + parent[:children] ||= [] > + parent[:children] << new_element > + end > + end > + ref_hash[current_id][:selected] = true if current_id > + return_tree > + end > + > def self.call_finder(*args) > obj = args.shift > method = args.shift > -- > 1.5.4.1 >ACK, Looks good, I have committed it. --Hugh