Class: RailsAiContext::Hydrators::ViewHydrator

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_ai_context/hydrators/view_hydrator.rb

Overview

Resolves instance variable references in views to model schema hints. Maps @post → Post, @posts → Post (singularized), etc.

Constant Summary collapse

SKIP_IVARS =
%w[
  page per_page total_count total_pages
  query search filter sort order
  flash notice alert errors
  breadcrumbs tabs menu
  title description meta
  current_page pagy
  output_buffer virtual_path _request
].to_set.freeze

Class Method Summary collapse

Class Method Details

.call(ivar_names, context:) ⇒ Object

Hydrate view instance variables with schema hints. ivar_names: array of instance variable names (without @) used in a view. Returns a HydrationResult with hints for resolved models.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rails_ai_context/hydrators/view_hydrator.rb', line 11

def self.call(ivar_names, context:)
  return HydrationResult.new if ivar_names.nil? || ivar_names.empty?

  model_names = ivar_names.filter_map { |ivar| ivar_to_model_name(ivar) }.uniq
  return HydrationResult.new if model_names.empty?

  hints = SchemaHintBuilder.build_many(model_names, context: context, max: RailsAiContext.configuration.hydration_max_hints)

  warnings = []
  unresolved = model_names - hints.map(&:model_name)
  unresolved.each do |name|
    warnings << "@#{name.underscore} used in view but '#{name}' model not found"
  end

  HydrationResult.new(hints: hints, warnings: warnings)
rescue => e
  $stderr.puts "[rails-ai-context] ViewHydrator failed: #{e.message}" if ENV["DEBUG"]
  HydrationResult.new
end