Skip to content

Commit

Permalink
Add ability to execute a RakeTask through a controller action
Browse files Browse the repository at this point in the history
  • Loading branch information
Austio committed Feb 4, 2021
1 parent 054b77e commit 80432cb
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 3 deletions.
Empty file.
9 changes: 9 additions & 0 deletions app/controllers/rake_ui/rake_tasks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,14 @@ def show
format.html
end
end


def execute
@rake_task = RakeUi::RakeTask.find_by_id(params[:id])

@rake_task.call(args: params[:args], environment: params[:environment])

redirect_to rake_task_path @rake_task.id
end
end
end
10 changes: 10 additions & 0 deletions app/views/rake_ui/rake_tasks/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<%= @rake_task.name %>
<%= form_with url: rake_task_execute_path(@rake_task), method: :post do |f| %>
<%= f.label :args, "Arguments, as if you were raking" %>
<%= f.text_field :arguments %>
<%= f.label :env, "Environment overrides" %>
<%= f.text_field :environment %>
<%= f.submit "Submit" %>
<% end %>
6 changes: 3 additions & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
RakeUi::Engine.routes.draw do
resources :rake_tasks, only: [:index, :show] do
resources :execute, only: [:create]
end
resources :rake_tasks, only: [:index, :show]

post "/rake_tasks/:id/execute", to: "rake_tasks#execute", as: "rake_task_execute"
end
17 changes: 17 additions & 0 deletions test/integration/rake_tasks_request_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,21 @@ class RakeTasksRequestTest < ActionDispatch::IntegrationTest
assert_equal task.id, json_response[:rake_task][:id]
assert_equal task.name_with_args, json_response[:rake_task][:name_with_args]
end

test "post executes the task" do
mock_task = Minitest::Mock.new
def mock_task.id; "some_identifier"; end
mock_task.expect :call, true, [{ args: "1,2,3", environment: "FOO=bar" }]

mock_find_by_id = lambda do |args|
assert args, "some_identifier"

mock_task
end

RakeUi::RakeTask.stub :find_by_id, mock_find_by_id do
post "/rake-ui/rake_tasks/#{mock_task.id}/execute", params: { environment: "FOO=bar",
args: "1,2,3" }
end
end
end
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
ActiveRecord::Migrator.migrations_paths = [File.expand_path("../test/dummy/db/migrate", __dir__)]
ActiveRecord::Migrator.migrations_paths << File.expand_path('../db/migrate', __dir__)
require "rails/test_help"
require "minitest/mock"


# Load fixtures from the engine
Expand Down

0 comments on commit 80432cb

Please sign in to comment.