Class: RailsAiBridge::ViewFileAnalyzer

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_ai_bridge/view_file_analyzer.rb

Overview

Extracts edit-focused metadata from a single view file.

Class Method Summary collapse

Class Method Details

.call(root:, relative_path:) ⇒ Hash

Reads a single view file under app/views and extracts editing hints.

Parameters:

  • root (String, Pathname)

    Rails root path

  • relative_path (String)

    path relative to app/views

Returns:

  • (Hash)

    normalized metadata for the requested view file

Raises:

  • (SecurityError)

    when the path escapes app/views

  • (Errno::ENOENT)

    when the file does not exist



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rails_ai_bridge/view_file_analyzer.rb', line 14

def call(root:, relative_path:)
  views_root = File.expand_path('app/views', root.to_s)
  requested = File.expand_path(relative_path.to_s, views_root)

  raise SecurityError, "Path not allowed: #{relative_path}" unless requested.start_with?("#{views_root}/") || requested == views_root

  raise Errno::ENOENT, relative_path unless File.file?(requested)

  content = File.read(requested)
  relative = requested.delete_prefix("#{views_root}/")

  {
    path: relative,
    template_engine: File.extname(relative).delete('.'),
    partial: File.basename(relative).start_with?('_'),
    renders: extract_renders(content),
    turbo_frames: extract_turbo_frames(content),
    stimulus_controllers: extract_stimulus_controllers(content),
    stimulus_actions: extract_stimulus_actions(content),
    content: content
  }
end