From: Eric Davis Date: Fri, 22 Oct 2010 22:38:45 +0000 (+0000) Subject: Refactor: move method to model with compatibility wrapper X-Git-Tag: 1.1.0~253 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=0ca74df6040371e448bcb3c718b84cdb635f07ee;p=redmine.git Refactor: move method to model with compatibility wrapper git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@4282 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 39c2ebda1..38d43e5b8 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -238,15 +238,10 @@ module ApplicationHelper end # Yields the given block for each project with its level in the tree + # + # Wrapper for Project#project_tree def project_tree(projects, &block) - ancestors = [] - projects.sort_by(&:lft).each do |project| - while (ancestors.any? && !project.is_descendant_of?(ancestors.last)) - ancestors.pop - end - yield project, ancestors.size - ancestors << project - end + Project.project_tree(projects, &block) end def project_nested_ul(projects, &block) diff --git a/app/models/project.rb b/app/models/project.rb index 000efa7c2..16c97441e 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -565,6 +565,18 @@ class Project < ActiveRecord::Base return nil end end + + # Yields the given block for each project with its level in the tree + def self.project_tree(projects, &block) + ancestors = [] + projects.sort_by(&:lft).each do |project| + while (ancestors.any? && !project.is_descendant_of?(ancestors.last)) + ancestors.pop + end + yield project, ancestors.size + ancestors << project + end + end private