Class: RubyLLM::MCP::OAuth::Generators::InstallGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
Rails::Generators::Migration
Defined in:
lib/generators/ruby_llm/mcp/oauth/install_generator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.next_migration_number(dirname) ⇒ Object



32
33
34
# File 'lib/generators/ruby_llm/mcp/oauth/install_generator.rb', line 32

def self.next_migration_number(dirname)
  ::ActiveRecord::Generators::Base.next_migration_number(dirname)
end

Instance Method Details

#add_routesObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/generators/ruby_llm/mcp/oauth/install_generator.rb', line 76

def add_routes
  return if options[:skip_routes]

  routes_content = if namespace_name
                     <<~ROUTES.strip
                       namespace :#{namespace_name.underscore} do
                         resources :mcp_connections, only: [ :index, :show, :create, :update, :destroy ] do
                           collection do
                             get :callback
                           end
                         end
                       end
                     ROUTES
                   else
                     <<~ROUTES.strip
                       resources :mcp_connections, only: [ :index, :show, :create, :update, :destroy ] do
                         collection do
                           get :callback
                         end
                       end
                     ROUTES
                   end

  route routes_content
end

#add_user_concernObject



110
111
112
# File 'lib/generators/ruby_llm/mcp/oauth/install_generator.rb', line 110

def add_user_concern
  template "concerns/user_mcp_oauth_concern.rb.tt", "app/models/concerns/user_mcp_oauth.rb"
end

#check_dependenciesObject

Validation methods



37
38
39
40
41
# File 'lib/generators/ruby_llm/mcp/oauth/install_generator.rb', line 37

def check_dependencies
  check_user_model_exists
  check_rails_version
  check_encryption_configured
end

#create_cleanup_jobObject



138
139
140
# File 'lib/generators/ruby_llm/mcp/oauth/install_generator.rb', line 138

def create_cleanup_job
  template "jobs/cleanup_expired_oauth_states_job.rb.tt", "app/jobs/cleanup_expired_oauth_states_job.rb"
end

#create_controllerObject



67
68
69
70
71
72
73
74
# File 'lib/generators/ruby_llm/mcp/oauth/install_generator.rb', line 67

def create_controller
  controller_path = if namespace_name
                      "#{namespace_name.underscore}/mcp_connections_controller.rb"
                    else
                      "mcp_connections_controller.rb"
                    end
  template "controllers/mcp_connections_controller.rb.tt", "app/controllers/#{controller_path}"
end

#create_example_jobObject



134
135
136
# File 'lib/generators/ruby_llm/mcp/oauth/install_generator.rb', line 134

def create_example_job
  template "jobs/example_job.rb.tt", "app/jobs/ai_research_job.rb"
end

#create_mcp_clientObject



63
64
65
# File 'lib/generators/ruby_llm/mcp/oauth/install_generator.rb', line 63

def create_mcp_client
  template "lib/mcp_client.rb.tt", "app/lib/mcp_client.rb"
end

#create_migrationsObject



43
44
45
46
47
48
49
50
51
52
# File 'lib/generators/ruby_llm/mcp/oauth/install_generator.rb', line 43

def create_migrations
  create_migration_if_needed(
    "create_mcp_oauth_credentials",
    "migrations/create_mcp_oauth_credentials.rb.tt"
  )
  create_migration_if_needed(
    "create_mcp_oauth_states",
    "migrations/create_mcp_oauth_states.rb.tt"
  )
end

#create_modelsObject



54
55
56
57
# File 'lib/generators/ruby_llm/mcp/oauth/install_generator.rb', line 54

def create_models
  template "models/mcp_oauth_credential.rb.tt", "app/models/mcp_oauth_credential.rb"
  template "models/mcp_oauth_state.rb.tt", "app/models/mcp_oauth_state.rb"
end

#create_token_storage_concernObject



59
60
61
# File 'lib/generators/ruby_llm/mcp/oauth/install_generator.rb', line 59

def create_token_storage_concern
  template "concerns/mcp_token_storage.rb.tt", "app/models/concerns/mcp_token_storage.rb"
end

#create_viewsObject



102
103
104
105
106
107
108
# File 'lib/generators/ruby_llm/mcp/oauth/install_generator.rb', line 102

def create_views
  return if options[:skip_views]

  view_path = namespace_name ? "#{namespace_name.underscore}/mcp_connections" : "mcp_connections"
  copy_file "views/index.html.erb", "app/views/#{view_path}/index.html.erb"
  copy_file "views/show.html.erb", "app/views/#{view_path}/show.html.erb"
end

#display_readmeObject



142
143
144
145
146
147
148
149
150
# File 'lib/generators/ruby_llm/mcp/oauth/install_generator.rb', line 142

def display_readme
  return unless behavior == :invoke

  display_header
  display_created_files
  display_next_steps
  display_documentation_links
  display_usage_example
end

#inject_concern_into_user_modelObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/generators/ruby_llm/mcp/oauth/install_generator.rb', line 114

def inject_concern_into_user_model
  user_model_path = "app/models/#{user_model_name.underscore}.rb"
  return unless File.exist?(user_model_path)

  # Check if concern is already included
  if File.read(user_model_path).include?("include UserMcpOauth")
    say "  ⏭️  UserMcpOauth concern already included in #{user_model_name}", :yellow
    return
  end

  inject_into_class user_model_path, user_model_name do
    "  include UserMcpOauth\n"
  end

  say "  ✅ Added UserMcpOauth concern to #{user_model_name}", :green
rescue StandardError => e
  say "  ⚠️  Could not automatically add concern to #{user_model_name}: #{e.message}", :yellow
  say "  Please manually add: include UserMcpOauth", :yellow
end