recursive finds

I am trying to supplement and ultimately provide a patch for 'foreman' which is an adjunct to puppet.

Essentially, there is a Hosts class which belongs_to Hostgroup and Hostgroup class has a column called 'ancestry' which is actually a Hostgroup (probably what is referred to as STI but I am not sure) and thus within Foreman, nesting Hostgroups is not uncommon.

Thus I could have a Host that is called "ServerA", that belongs to "WebServer" Hostgroup which has an ancestry (Hostgroup) called "Base" and for me to find out all of the puppetclasses that host "ServerA" belongs to, I would have to get a listing of all of the puppetclasses within the chain of Hostgroups to which ServerA belongs.

So this is my code...

@hosts.each do |host|
  @ancestors = Hostgroup.find(host.hostgroup.ancestry) unless host.hostgroup.ancestry == nil
   @ancestors.puppetclasses.each do |anc|
      @my_classes[anc.name] += 1
  end
end

And this works for me because I am only nesting 1 level but it's conceivable that other users would nest many more levels and if my patch is to be accepted, I undoubtedly have to account for an infinite level of nesting.

How would I accomplish that?

I am trying to supplement and ultimately provide a patch for 'foreman' which is an adjunct to puppet.

Essentially, there is a Hosts class which belongs_to Hostgroup and Hostgroup class has a column called 'ancestry' which is actually a Hostgroup (probably what is referred to as STI but I am not sure) and thus within Foreman, nesting Hostgroups is not uncommon.

This is actually the ancestry gem [1] which does most of the heavy lifting.

Thus I could have a Host that is called "ServerA", that belongs to "WebServer" Hostgroup which has an ancestry (Hostgroup) called "Base" and for me to find out all of the puppetclasses that host "ServerA" belongs to, I would have to get a listing of all of the puppetclasses within the chain of Hostgroups to which ServerA belongs.

So this is my code...

@hosts.each do |host|
@ancestors = Hostgroup.find(host.hostgroup.ancestry) unless host.hostgroup.ancestry == nil
@ancestors.puppetclasses.each do |anc|
@my_classes[anc.name] += 1
end
end

And this works for me because I am only nesting 1 level but it's conceivable that other users would nest many more levels and if my patch is to be accepted, I undoubtedly have to account for an infinite level of nesting.

How would I accomplish that?

look for the sort_by_ancestry method

see https://github.com/ohadlevy/foreman/blob/develop/app/models/hostgroup.rb#L73

--
Craig White ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ craig.white@ttiltd.com
1.800.869.6908 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ www.ttiassessments.com

Need help communicating between generations at work to achieve your desired success? Let us help!

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

[1] - https://github.com/stefankroes/ancestry