Skip to content

Commit

Permalink
move around logic; get rendering of prompts working
Browse files Browse the repository at this point in the history
  • Loading branch information
evdevdev committed Sep 13, 2024
1 parent 1fda608 commit 7b91b42
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 25 deletions.
15 changes: 13 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
{
"rubyLsp.formatter": "rubocop",
}
"[erb]": {
"editor.defaultFormatter": "Shopify.ruby-lsp"
},
"[ruby]": {
"editor.defaultFormatter": "Shopify.ruby-lsp",
"editor.formatOnSave": true,
"editor.tabSize": 2,
"editor.insertSpaces": true,
"editor.semanticHighlighting.enabled": true,
"editor.formatOnType": true,
"editor.wordSeparators": "`~@#$%^&*()-=+[{]}\\|;:'\",.<>/"
}
}
4 changes: 3 additions & 1 deletion app/controllers/action_prompt/previews_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ def index
end

def show
@preview = ActionPrompt::Preview.find(params[:preview])
preview_class_name = params[:preview_name].camelize + "Preview"
@preview_class = ActionPrompt::Preview.find(preview_class_name)
@prompt_output = @preview_class.new.send(params[:prompt_name].to_sym)
end
end
2 changes: 1 addition & 1 deletion app/views/action_prompt/previews/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<% @previews.each do |preview| %>
<h3><%= preview.preview_name.titleize %></h3>
<% preview.prompts.each do |prompt| %>
<p><%= link_to prompt, "/action_prompt/preview/#{preview.preview_name}/#{prompt}" %></p>
<p><%= link_to prompt.name, "/action_prompt/previews/#{prompt.slug}" %></p>
<% end %>
<% end %>
<% else %>
Expand Down
9 changes: 8 additions & 1 deletion app/views/action_prompt/previews/show.html.erb
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
<h1>Action Prompt Preview</h1>
<h1>Action Prompt Preview</h1>
<div>
<%= @preview_class.name %>
</div>
<div>
<%= @prompt_output %>
</div>

5 changes: 0 additions & 5 deletions lib/action_prompt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,4 @@
require "action_prompt/railtie"
require "action_prompt/preview"
module ActionPrompt
def self.render(template_name, locals: {})
controller = ApplicationController.new
controller.prepend_view_path(Rails.root.join("app", "prompts"))
controller.render_to_string(template: template_name, locals: locals)
end
end
31 changes: 28 additions & 3 deletions lib/action_prompt/preview.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,37 @@ def preview_name
end

def prompts
public_instance_methods(false).map(&:to_s).sort
# TODO: this might benefit from some memoization
prompt_methods = public_instance_methods(false).map(&:to_s).sort
prompt_methods.map do |method_name|
Prompt.new(name: method_name, slug: "#{preview_name}/#{method_name}")
end
end

def find(name)
all.find { |preview| preview.name == name }
end

def find_prompt(name)
prompts.find { |p| p.name == name }
end
end


class Prompt
attr_reader :name, :slug

def find(preview_name)
all.find { |p| p.preview_name == preview_name }
# NOTE: this could probably be a Struct
def initialize(name:, slug:)
@name = name
@slug = slug
end
end

def render(template_name, locals: {})
controller = ApplicationController.new
controller.prepend_view_path(Rails.root.join("app", "prompts"))
controller.render_to_string(template: template_name, locals: locals)
end
end
end
2 changes: 1 addition & 1 deletion lib/action_prompt/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Railtie < Rails::Railtie
if Rails.env.development? || Rails.env.test?
app.routes.prepend do
get "/action_prompt/previews", to: "action_prompt/previews#index" # , internal: true
get "/action_prompt/preview/:prompt", to: "action_prompt/previews#show" # , internal: true
get "/action_prompt/previews/:preview_name/:prompt_name", to: "action_prompt/previews#show" # , internal: true
end
end
end
Expand Down
9 changes: 0 additions & 9 deletions test/action_prompt/base_test.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
require "test_helper"

class ActionPrompt::BaseTest < ActiveSupport::TestCase
test "hello world" do
result = ActionPrompt.render("hello_world")
assert_equal "You are a helpful assistant who replies with, \"Hello, world!\"", result
end

test "a template with a variable" do
result = ActionPrompt.render("hello_somebody", locals: { name: "John" })
assert_equal "You are a helpful assistant who replies with, \"Hello, John!\"", result
end
end
12 changes: 12 additions & 0 deletions test/action_prompt/preview_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,16 @@ class ActionPrompt::PreviewTest < ActiveSupport::TestCase

assert_equal "hello_world", preview.preview_name
end

test "render without locals" do
skip
# result = ActionPrompt.render("hello_world")
# assert_equal "You are a helpful assistant who replies with, \"Hello, world!\"", result
end

test "render with locals" do
skip
# result = ActionPrompt.render("hello_somebody", locals: { name: "John" })
# assert_equal "You are a helpful assistant who replies with, \"Hello, John!\"", result
end
end
4 changes: 2 additions & 2 deletions test/dummy/test/prompts/hello_world_preview.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
class HelloWorldPreview < ActionPrompt::Preview
def hello_world
ActivePrompt.render("hello_world")
render("hello_world")
end

def hello_somebody
ActivePrompt.render("hello_somebody", locals: { name: "Lenny" })
render("hello_somebody", locals: { name: "Lenny" })
end
end

0 comments on commit 7b91b42

Please sign in to comment.