Skip to content

Commit

Permalink
introduce preview; wire up list of previews and prompts
Browse files Browse the repository at this point in the history
  • Loading branch information
evdevdev committed Aug 28, 2024
1 parent 5928b13 commit b5a98f5
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ gem "sprockets-rails"
gem "rubocop-rails-omakase", require: false

# Start debugger with binding.b [https://github.com/ruby/debug]
# gem "debug", ">= 1.0.0"
gem "debug"
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ GEM
connection_pool (2.4.1)
crass (1.0.6)
date (3.3.4)
debug (1.9.2)
irb (~> 1.10)
reline (>= 0.3.8)
drb (2.2.1)
erubi (1.13.0)
globalid (1.2.1)
Expand Down Expand Up @@ -266,6 +269,7 @@ PLATFORMS

DEPENDENCIES
action_prompt!
debug
puma
rubocop-rails-omakase
sprockets-rails
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/action_prompt/previews_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class ActionPrompt::PreviewsController < Rails::ApplicationController

def index
@page_title = "Action Prompt Previews"
@previews = []
@previews = ActionPrompt::Preview.all
end

def show
Expand Down
12 changes: 5 additions & 7 deletions app/views/action_prompt/previews/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
<h1><%= @page_title %></h1>
<h1>Action Prompt Previews</h1>

<% if @previews.any? %>
<% @previews.each do |preview| %>
<h3><%= link_to preview.preview_name.titleize, url_for(controller: "rails/mailers", action: "preview", path: preview.preview_name) %></h3>
<ul>
<% preview.emails.each do |email| %>
<li><%= link_to email, url_for(controller: "rails/mailers", action: "preview", path: "#{preview.preview_name}/#{email}") %></li>
<% end %>
</ul>
<h3><%= preview.preview_name %></h3>
<% preview.prompts.each do |prompt| %>
<p><%= link_to prompt, "/action_prompt/preview/#{prompt}" %></p>
<% end %>
<% end %>
<% else %>
<p>You have not defined any Action Prompt Previews.</p>
Expand Down
2 changes: 1 addition & 1 deletion lib/action_prompt.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "action_prompt/version"
require "action_prompt/engine"
require "action_prompt/railtie"

require "action_prompt/preview"
module ActionPrompt
def self.render(template_name, locals: {})
controller = ApplicationController.new
Expand Down
33 changes: 33 additions & 0 deletions lib/action_prompt/preview.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require "active_support/descendants_tracker"

module ActionPrompt
class Preview
# We need to be able to do the following:
# - find all the preview objects
# - find all the preview methods on those objects
# - render a preview
#
class << self
def all
load_previews if descendants.empty?
descendants.sort_by(&:name)
end

def load_previews
# TODO: this preview path could be made configurable in order to have equivalent
# functionality to Rails mailer previews
Dir[Rails.root.join("test", "prompts", "**", "*_preview.rb")].each do |path|
require path
end
end

def preview_name
name.delete_suffix("Preview").underscore
end

def prompts
public_instance_methods(false).map(&:to_s).sort
end
end
end
end
4 changes: 2 additions & 2 deletions lib/action_prompt/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ class Railtie < Rails::Railtie
config.after_initialize do |app|
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_id", to: "action_prompt/previews#show", internal: true
get "/action_prompt/previews", to: "action_prompt/previews#index" # , internal: true
get "/action_prompt/preview/:prompt", to: "action_prompt/previews#show" # , internal: true
end
end
end
Expand Down
14 changes: 14 additions & 0 deletions test/action_prompt/preview_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require "test_helper"

class ActionPrompt::PreviewTest < ActiveSupport::TestCase
test "all" do
previews = ActionPrompt::Preview.all
assert_equal 1, previews.count
end

test "preview_name" do
preview = ActionPrompt::Preview.all.first

assert_equal "hello_world", preview.preview_name
end
end
9 changes: 9 additions & 0 deletions test/dummy/test/prompts/hello_world_preview.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class HelloWorldPreview < ActionPrompt::Preview
def hello_world
ActivePrompt.render("hello_world")
end

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

0 comments on commit b5a98f5

Please sign in to comment.