Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/generators/erb/agent_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def copy_view_files
private

def formats
[ :text, :html ]
[ :text, :html, :json ]
end

def file_name
Expand Down
1 change: 1 addition & 0 deletions lib/generators/erb/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class InstallGenerator < ::Rails::Generators::Base # :nodoc:
def create_agent_layouts
template "layout.html.erb.tt", "app/views/layouts/agent.html.erb"
template "layout.text.erb.tt", "app/views/layouts/agent.text.erb"
template "layout.json.erb.tt", "app/views/layouts/agent.json.erb"
end
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/generators/erb/templates/layout.json.erb.tt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%%= yield %>
16 changes: 16 additions & 0 deletions lib/generators/erb/templates/view.json.erb.tt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<%%= {
type: :function,
function: {
name: action_name,
description: "This action takes no params and gets a random cat image and returns it as a base64 string.",
parameters: {
type: :object,
properties: {
param_name: {
type: :string,
description: "The param_description"
}
}
}
}
}.to_json.html_safe %>
27 changes: 27 additions & 0 deletions test/generators/erb/agent_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ class Erb::Generators::AgentGeneratorTest < Rails::Generators::TestCase

assert_file "app/views/user_agent/create.html.erb"
assert_file "app/views/user_agent/create.text.erb"
assert_file "app/views/user_agent/create.json.erb"
assert_file "app/views/user_agent/update.html.erb"
assert_file "app/views/user_agent/update.text.erb"
assert_file "app/views/user_agent/update.json.erb"
end

test "generates view files with correct content" do
Expand All @@ -26,13 +28,20 @@ class Erb::Generators::AgentGeneratorTest < Rails::Generators::TestCase
assert_file "app/views/user_agent/create.text.erb" do |content|
assert_match(/User#create/, content)
end

assert_file "app/views/user_agent/create.json.erb" do |content|
assert_match(/action_name/, content)
assert_match(/function/, content)
assert_match(/\.to_json\.html_safe/, content)
end
end

test "generates nested view files" do
run_generator [ "admin/user", "create" ]

assert_file "app/views/admin/user_agent/create.html.erb"
assert_file "app/views/admin/user_agent/create.text.erb"
assert_file "app/views/admin/user_agent/create.json.erb"
end

test "does not generate view files without actions" do
Expand All @@ -42,5 +51,23 @@ class Erb::Generators::AgentGeneratorTest < Rails::Generators::TestCase
assert_file "app/views/user_agent"
assert_no_file "app/views/user_agent/index.html.erb"
assert_no_file "app/views/user_agent/show.html.erb"
assert_no_file "app/views/user_agent/index.text.erb"
assert_no_file "app/views/user_agent/show.text.erb"
assert_no_file "app/views/user_agent/index.json.erb"
assert_no_file "app/views/user_agent/show.json.erb"
end

test "generates json view files with function schema structure" do
run_generator [ "user", "create" ]

assert_file "app/views/user_agent/create.json.erb" do |content|
assert_match(/type: :function/, content)
assert_match(/function:/, content)
assert_match(/name: action_name/, content)
assert_match(/description:/, content)
assert_match(/parameters:/, content)
assert_match(/type: :object/, content)
assert_match(/properties:/, content)
end
end
end
4 changes: 4 additions & 0 deletions test/generators/erb/install_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ class Erb::Generators::InstallGeneratorTest < Rails::Generators::TestCase
assert_file "app/views/layouts/agent.text.erb" do |content|
assert_match(/<%= yield %>/, content)
end

assert_file "app/views/layouts/agent.json.erb" do |content|
assert_match(/<%= yield %>/, content)
end
end

test "creates layout files with correct ERB syntax" do
Expand Down