25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/console_agent/tools/model_tools.rb', line 25
def describe_model(model_name)
return "ActiveRecord is not available." unless defined?(ActiveRecord::Base)
return "Error: model_name is required." if model_name.nil? || model_name.strip.empty?
eager_load_app!
model_name = model_name.strip
model = find_models.detect { |m| m.name == model_name || m.name.underscore == model_name.underscore }
return "Model '#{model_name}' not found. Use list_models to see available models." unless model
result = "Model: #{model.name}\n"
result += "Table: #{model.table_name}\n"
assocs = model.reflect_on_all_associations.map { |a| "#{a.macro} :#{a.name}" }
unless assocs.empty?
result += "Associations:\n"
assocs.each { |a| result += " #{a}\n" }
end
begin
validators = model.validators.map { |v|
attrs = v.attributes.join(', ')
kind = v.class.name.split('::').last.sub('Validator', '').downcase
"#{kind} on #{attrs}"
}.uniq
unless validators.empty?
result += "Validations:\n"
validators.each { |v| result += " #{v}\n" }
end
rescue => e
end
begin
base = defined?(ApplicationRecord) ? ApplicationRecord : ActiveRecord::Base
scope_candidates = (model.singleton_methods - base.singleton_methods)
.reject { |m| m.to_s.start_with?('_') || m.to_s.start_with?('find') }
.sort
.first(20)
unless scope_candidates.empty?
result += "Possible scopes/class methods:\n"
scope_candidates.each { |s| result += " #{s}\n" }
end
rescue => e
end
result
rescue => e
"Error describing model '#{model_name}': #{e.message}"
end
|