jsnX
4/19/2007 7:12:00 AM
On Apr 18, 9:01 am, Zouplaz <u...@domain.invalid> wrote:
> def affiche_arbre(rubriques)
> for rubrique in rubriques
> result = '<br/>'
> 0.step(rubrique.level) { result += ' '}
> result += rubrique.libelle + ' ' + link_to('+', :action => 'new',
> :parent_id => rubrique) + ' '
> unless rubrique.parent_id == nil
> result += link_to('E', :action => 'edit', :id => rubrique) + '
> ' + link_to('D', :action => 'delete', :id => rubrique )
> end
> if rubrique.children.size > 0
> result += affiche_arbre(rubrique.children)
> end
> end
> "#{result}"
> end
This code could also be:
============ code ============
links(rubrique)
[ link_to('+', :action => 'new', :parent_id => rubrique) ] +
( rubrique.parent_id ?
[ link_to('E', :action => 'edit', :id => rubrique),
link_to('D', :action => 'delete', :id => rubrique) ] : [ ] )
end
def affiche_arbre(rubriques)
rubriques.collect do |rubrique|
'<br/>' + (' ' * rubrique.level) +
[ rubrique.libelle ].concat(links(rubrique)).join(' ') +
( rubrique.children.size.zero? ? '' :
affiche_arbre(rubrique.children) )
end.join()
end
=========== /code ===============
Please excuse the Anglicized new function -- I know no French. Notice
that this code allows you to test correctness of link generation
separately from correctness of the generated HTML. By restricting
myself to expressions -- there are no variable assignments in this
code, let alone mutations -- I protect myself from the kind of bug
that marred the original.