Update

Had a play with Closure Tree Gem. Basically set it up outside of rails, worked out I could not call the generator to create a migration for the gem without rails. In the end I generated the migration in another rails repo and copied it across. I would have thought that something similar to the below code should have worked? given that I had Railties already installed due to my dependence on standalone_migrations and ActiveRecord?

bundle exec ruby -e 'require "bundler/setup"; \
  Bundler.require(:default); \
  Rails::Generators.invoke("closure_tree:migration", \
    destination_root: Pathname.new(`pwd`.chomp))' \
    company

This led to initially Thor::Parser::Options failing to call each on nil:NilClass.

/gems/thor-1.2.1/lib/thor/parser/options.rb:39:in `initialize':
  undefined method `each' for nil:NilClass (NoMethodError)

When I tried to get past that in a binding.irb session I got an invalid call to .underscore on Pathname as I presume I don’t have Rails which adds that method to String?

/gems/railties-7.0.4/lib/rails/generators/named_base.rb:177:in `map!':
  undefined method `underscore' for
  [:destination_root, #<Pathname:/Users/michael/Projects/me/100-days-of-code/004_closure_tree>]:Array (NoMethodError)

  @class_path.map!(&:underscore)
             ^^^^^

Next Up

Although I got a hierarchy of related objects I didn’t do much with them.

expect(map_object_method(tech.hash_tree, :name)).to eq([[
  "Tech",
  [
    ["Saas", [
      ["Facebook", []],
      ["Netflix", []],
      ["Google", []],
    ]],
    ["Hardware", [
      ["Amazon", []],
      ["Apple", []],
    ]],
  ],
]])
  • I had no use for them, like doing roll up calculations or something
  • I did not visualise them on a front end.

All of those may be ideas for next time.