Class: ActionPrompt::Renderer

Inherits:
Object
  • Object
show all
Defined in:
lib/action_prompt/renderer.rb

Overview

Handles ERB template rendering for prompt actions via ActionView.

Templates are resolved from the view paths configured on the prompt class (defaults to app/views/ in a Rails app). The lookup path convention is:

app/views/action_prompts/<prompt_class_name>/<action_name>.text.erb

Examples

ArticleSummarizerPrompt  ->  action_prompts/article_summarizer_prompt/summarize.text.erb
Admin::ReportPrompt      ->  action_prompts/admin/report_prompt/generate.text.erb

Instance Method Summary collapse

Constructor Details

#initialize(prompt_instance, action_name) ⇒ Renderer

Returns a new instance of Renderer.

Parameters:

  • prompt_instance (ActionPrompt::Base)

    owns the instance variables for the template

  • action_name (String)

    the prompt action method name



18
19
20
21
# File 'lib/action_prompt/renderer.rb', line 18

def initialize(prompt_instance, action_name)
  @prompt_instance = prompt_instance
  @action_name = action_name
end

Instance Method Details

#renderString

Renders the ERB template and returns the resulting string.

Returns:

  • (String)

    the rendered prompt body

Raises:

  • (ActionView::MissingTemplate)

    if the template file cannot be found



27
28
29
30
31
# File 'lib/action_prompt/renderer.rb', line 27

def render
  lookup_context = ActionView::LookupContext.new(resolved_view_paths)
  view = ActionView::Base.with_empty_template_cache.new(lookup_context, assigns, nil)
  view.render(template: template_path, formats: [:text], handlers: [:erb])
end