stefankroes / ancestry

Organise ActiveRecord model into a tree structure
MIT License
3.74k stars 462 forks source link

Add tree_view method #561

Closed Bizcho closed 2 years ago

Bizcho commented 2 years ago

This adds the funcionality for: #559. For Example:

arrange method:

message.subtree.arrange
{#<Message:0x000055f62357a930 id: 9, content: "This is a root message", created_at: Tue, 23 Nov 2021 05:04:04.053540000 UTC +00:00, updated_at: Tue, 23 Nov 2021 05:04:04.053540000 UTC +00:00, ancestry: nil>=>
  {#<Message:0x000055f62357a048 id: 10, content: "this is a reply", created_at: Tue, 23 Nov 2021 05:04:50.832312000 UTC +00:00, updated_at: Tue, 23 Nov 2021 05:04:50.832312000 UTC +00:00, ancestry: "9">=>
    {#<Message:0x000055f623578b58 id: 12, content: "this is a reply to a rply", created_at: Tue, 23 Nov 2021 05:05:48.845504000 UTC +00:00, updated_at: Tue, 23 Nov 2021 05:05:48.845504000 UTC +00:00, ancestry: "9/10">=>{}},
   #<Message:0x000055f6235796c0 id: 11, content: "this is another reply", created_at: Tue, 23 Nov 2021 05:05:00.767846000 UTC +00:00, updated_at: Tue, 23 Nov 2021 05:05:00.767846000 UTC +00:00, ancestry: "9">=>
    {}
  }
}

tree_view method:

message.subtree.tree_view(:content)
This is a root message
 |_ this is a reply
 |   |_ this is a reply to a rply
 |_ this is another reply
kbrock commented 2 years ago

So this is a pretty print kind of thing?

It looks nice

Bizcho commented 2 years ago

Yes a friend of mine is using this gem for nesting components and wanted a better way of debuggin the tree. Hope it gets added to the main repo

kbrock commented 2 years ago

thanks

kbrock commented 2 years ago

@Bizcho Hey,

Do you have any docs on using this? /cc @epugh

Bizcho commented 2 years ago

Hello @kbrock I dont have any Docs but the basic idea is to call the method tree_view and pass a symbold that is a property od the class you are using. like in the first coment.

Class Message has two atributes id and content, So you could call message.subtree.tree_view(:content) or message.subtree.tree_view(:id)

the result is built with the toString() of whaterever atribute you passed to the method

hope my awnser is sufficent,

epugh commented 2 years ago

I unfortunantly get:

ERROR TriesAncestryLifecycleTest#test_create_200_tries_behavior (1.25s)
Minitest::UnexpectedError:         NoMethodError: undefined method `tree_view' for #<ActiveRecord::Relation []>
            test/integration/tries_ancestry_lifecycle_test.rb:21:in `block in <class:TriesAncestryLifecycleTest>'

Here is the (commented out!) use of this command: https://github.com/o19s/quepid/blob/ancestry_column_too_short/test/integration/tries_ancestry_lifecycle_test.rb#L21

kbrock commented 2 years ago

@epugh I'm curious, is the ancestry column getting properly in your test? i.e. If you add a puts try.ancestry after the try.save! does it keep getting longer and longer?

also the use of the try variable is fine, but it does make me nervous for the try() method defined via ActiveSupport

Could you paste the code that is failing for you? That link no longer shows the line commented out