Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Usefull methods? #2

Open
RichardK opened this issue Sep 19, 2010 · 1 comment
Open

Usefull methods? #2

RichardK opened this issue Sep 19, 2010 · 1 comment

Comments

@RichardK
Copy link

Hi. What about adding some usefull methods to your plug in:
leafs: returns only the last elements of a tree for a given knot
structure: returns the given knot and all elements under the knot

Hereby a suggestion of code - it works pretty nice:

module ActiveRecord
module Acts
module Tree
module ClassMethods
def structure(knot=find(0)) #Returns an array of knot and all elements under knot
y = Array.new
y[0] = knot
if y[0].children.first then
looping(y, knot)
end
return y
end
def leafs(knot=find(0)) #Returns only the last elements of your tree under the tree object knot
@@tmp = structure(knot)
@leafs = Array.new
@@tmp.each do |i|
if not (i.children.first) then
@leafs[@leafs.size] = i
end
end
return @leafs
end
private
def looping(y, parent)
t = Array.new
t = parent.children
t.each do |i|
y[y.size] = i
if i.children.first then
looping(y, i)
end
end
end
end
end
end
end

@RichardK
Copy link
Author

A variant could be passing an array instead of knot as object; so that the default could be knot.root ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant