8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/autocontext.rb', line 8
def self.generate
raise "Rails is not defined. Please ensure you are running this within a Rails application." unless defined?(Rails)
puts "Generating autocontext file"
Rails.application.eager_load!
gemfile_content = File.read(Rails.root.join('Gemfile'))
ruby_version = gemfile_content.match(/ruby ['"](.+?)['"]/i)&.captures&.first
rails_version = gemfile_content.match(/gem ['"]rails['"],\s*['"](.+?)['"]/i)&.captures&.first
File.open('.autocontext', 'w') do |file|
file.puts "/* AutoContext generation completed at #{Time.now} */"
file.puts "Ruby version: #{ruby_version}"
file.puts "Rails version: #{rails_version}"
file.puts ""
self.generate_controllers(file)
file.puts ""
file.puts "Models:"
ApplicationRecord.descendants.each do |model|
file.puts " #{model.name}:"
file.puts " superclass: #{model.superclass.name}"
file.puts " database: #{model.connection.current_database}"
file.puts " table_name: #{model.table_name}"
file.puts " relative path: app/models/#{model.name.underscore}.rb"
self.generate_associations(model, file)
file.puts ""
rescue => e
puts "Error generating autocontext for #{model.name}: #{e.message}"
end
end
end
|