Class: RailsConsolePro::AssociationNavigator

Inherits:
Object
  • Object
show all
Includes:
ColorHelper
Defined in:
lib/rails_console_pro/association_navigator.rb

Overview

Interactive association navigator

Constant Summary collapse

ASSOCIATION_ICONS =
{
  belongs_to: "↖️",
  has_one: "→",
  has_many: "⇒",
  has_and_belongs_to_many: "⇔"
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ColorHelper

#bold_color, #color, #method_missing, #pastel, #respond_to_missing?

Constructor Details

#initialize(model) ⇒ AssociationNavigator

Returns a new instance of AssociationNavigator.



17
18
19
20
21
# File 'lib/rails_console_pro/association_navigator.rb', line 17

def initialize(model)
  @model = resolve_model(model)
  @history = []
  validate_model!
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class RailsConsolePro::ColorHelper

Instance Attribute Details

#historyObject (readonly)

Returns the value of attribute history.



15
16
17
# File 'lib/rails_console_pro/association_navigator.rb', line 15

def history
  @history
end

#modelObject (readonly)

Returns the value of attribute model.



15
16
17
# File 'lib/rails_console_pro/association_navigator.rb', line 15

def model
  @model
end

Instance Method Details

#startObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rails_console_pro/association_navigator.rb', line 23

def start
  current_model = @model
  loop do
    display_menu(current_model)
    choice = get_user_choice(current_model)
    
    break if choice == :exit
    next handle_back(current_model) if choice == :back
    next unless choice
    
    @history.push(current_model)
    current_model = navigate_to(current_model, choice)
  end
end