closure_tree icon indicating copy to clipboard operation
closure_tree copied to clipboard

Attributes From Ancestors?

Open peck opened this issue 11 years ago • 0 comments

I'm wondering if there is a good clean way of getting attributes from a nodes ancestors, and have it work efficiently and generically.

My project has a hierarchy of organizations and sub organizations, each with a few different fees/settings that can either be set or get the value from the parent or grandparent, etc.

Currently I'm solving it by doing:

module ClosureTreeLookup
  def ancestor_accessor *attrs
    attrs.each do |attr|
      define_method(attr) do
        ancestor_attribs = ancestors.map{|a| a.send(attr)}
        ([read_attribute(attr)] + ancestor_attribs).find{|x| x}
      end
    end
  end
end

and then doing

ancestor_accessor :rush_fee

In the class that acts_as_tree

Which is straightforward. Is there anything in the codebase that could help this and keep it efficient wrt database queries and object instantiation?

Essentially inheritance at the instantiated object level, which feels odd, but seems only solution that works for "flowing" changes down the tree

Thanks,

peck avatar Sep 03 '14 19:09 peck