Class: Sage::ModelScopesContext

Inherits:
Object
  • Object
show all
Defined in:
lib/sage/model_scopes_context.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeModelScopesContext

Returns a new instance of ModelScopesContext.



3
4
5
# File 'lib/sage/model_scopes_context.rb', line 3

def initialize
  # Nothing to initialize for now
end

Class Method Details

.callObject



7
8
9
# File 'lib/sage/model_scopes_context.rb', line 7

def self.call
  new.build_context
end

Instance Method Details

#build_contextObject



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
# File 'lib/sage/model_scopes_context.rb', line 11

def build_context
  context_parts = []
  context_parts << "\n\n## AVAILABLE SCOPES → SQL MAPPINGS\n"
  context_parts << "CRITICAL: Use these scopes to understand how to query the data!"
  context_parts << "Each scope name shows the SQL conditions it generates."
  context_parts << "When a user's request matches a scope's intent, use that scope's SQL pattern.\n"

  # Get all ActiveRecord models from the host application
  # Safely attempt to eager load, but continue if there are issues
  begin
    Rails.application.eager_load! if Rails.env.development?
  rescue Zeitwerk::NameError => e
    Rails.logger.warn "Could not eager load all files: #{e.message}"
  end

  models_with_scopes = collect_models_with_scopes

  # Format the model scopes nicely
  if models_with_scopes.any?
    models_with_scopes.each do |model_info|
      context_parts << "\n### #{model_info[:name]} (table: `#{model_info[:table]}`)"
      context_parts << "Scopes and their SQL equivalents:"
      model_info[:scopes].each do |scope|
        context_parts << scope
      end
    end
  end

  context_parts.join("\n")
end