stefankroes / ancestry

Organise ActiveRecord model into a tree structure
MIT License
3.72k stars 459 forks source link

Multiple trees using the same model. #229

Closed hickscorp closed 4 years ago

hickscorp commented 9 years ago

Hello,

Before starting, i would like to point out that i'm aware of the various "multiple parents" threads as well as the limitations for this gem. My question is about a different thing.

Let's say you have a user model inside a genealogy application. Let's say that you also want to handle referrals between users without having to create a separate model. The ideal solution and the most DRY would be to be able to call has_ancestry two times, each time with a different column but which would also override the generated method names and logic behind Ancestry:

class User < ActiveRecord::Base
  has_ancestry  prefix: 'genealogic'
  has_ancestry  prefix: 'referral'
end

class CreateUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|
      # Ancestry fields.
      t.string       :genealogic_ancestry,        index:  true
      t.integer      :genealogic_ancestry_depth,  index:  true
      t.string       :referral_ancestry,          index:  true
      t.integer      :referral_ancestry_depth,    index:  true
      t.timestamps
    end
  end
end

This would then allow for instance:

u = User.first
u.genalogic_ancestry # Instead of just .ancestry
u.referral_parent # Instead of just .parent

Is there any way to maintain multiple ancestry columns inside the same model? My educated guess would be that the gem does not allow this kind of thing, as most of the generated methods on the instances (.parent, .ancestry etc) do not allow to give options regarding the column name. If you think that this request may be an interesting feature (Eg: Maintaining a list of ancestry-enabled methods and fields and columns), how hard do you think it would be to generate methods prefixed (Eg #{ column_name }_ancestry and #{ column_name }_parent)? What other kind of change would this require?

Having looked at the source code, i noticed that class and instance methods are right now added with module inclusions, and i guess it would require a bit of refactoring to meta-programming to use some HEREDOC-based eval, containing definitions like def #{ column_name }_parent etc if more than has_ancestry call was made, or if the has_ancestry call was made with a column specification. Would it be a feasible thing for this gem in the near future?

A pointer regarding class_eval would be the answer from Dr. Johnny Mohawk on this SO post.

Else, do you know any gem that might support what i'm trying to achieve? Thanks!

hickscorp commented 8 years ago

Bump... Ended up rewriting the plugin completely. See https://github.com/hickscorp/active_record-acts_as ... I know, it's old.

kbrock commented 8 years ago

@hickscorp sorry we weren't able to respond in a timely manner. Looking into making the ancestry column configurable so a node will be able to act as a node in multiple trees