Skip to content

Attributes From Ancestors? #120

Open
@peck

Description

@peck

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,

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions