diff --git a/app/controllers/collections/workflows_controller.rb b/app/controllers/collections/workflows_controller.rb index ae50b90f9..28b691f11 100644 --- a/app/controllers/collections/workflows_controller.rb +++ b/app/controllers/collections/workflows_controller.rb @@ -10,6 +10,6 @@ class Collections::WorkflowsController < ApplicationController private def set_workflow - @workflow = Workflow.find(params.expect(collection: [ :workflow_id ]).require(:workflow_id)) + @workflow = Workflow.find(params[:collection][:workflow_id]) end end diff --git a/app/controllers/workflows_controller.rb b/app/controllers/workflows_controller.rb index 49abcd26f..f8b54254d 100644 --- a/app/controllers/workflows_controller.rb +++ b/app/controllers/workflows_controller.rb @@ -11,8 +11,6 @@ class WorkflowsController < ApplicationController def create @workflow = Workflow.create! workflow_params - # FIXME: this should definitely change. - [ "Triage", "In progress", "On Hold", "Review" ].each { |name| @workflow.stages.create! name: name } redirect_to workflows_path end diff --git a/app/models/workflow.rb b/app/models/workflow.rb index df896cf09..b1e1fdb22 100644 --- a/app/models/workflow.rb +++ b/app/models/workflow.rb @@ -1,3 +1,13 @@ class Workflow < ApplicationRecord + DEFAULT_STAGES = [ "Triage", "In progress", "On Hold", "Review" ] + has_many :stages, dependent: :delete_all + + after_create_commit :create_default_stages + + private + def create_default_stages + Workflow::Stage.insert_all \ + DEFAULT_STAGES.collect { |default_stage_name| { workflow_id: id, name: default_stage_name } } + end end diff --git a/test/controllers/users_controller_test.rb b/test/controllers/users_controller_test.rb index ff7ec0f2c..fad78128f 100644 --- a/test/controllers/users_controller_test.rb +++ b/test/controllers/users_controller_test.rb @@ -5,8 +5,8 @@ class UsersControllerTest < ActionDispatch::IntegrationTest sign_in_as :kevin get users_path - assert_match /#{users(:david).name}/, @response.body - assert_match /#{users(:kevin).name}/, @response.body + assert_in_body users(:david).name + assert_in_body users(:kevin).name end test "new" do @@ -32,7 +32,7 @@ class UsersControllerTest < ActionDispatch::IntegrationTest sign_in_as :kevin get user_path(users(:david)) - assert_match /#{users(:david).name}/, @response.body + assert_in_body users(:david).name end test "update oneself" do diff --git a/test/controllers/workflows_controller_test.rb b/test/controllers/workflows_controller_test.rb new file mode 100644 index 000000000..8d3d64c69 --- /dev/null +++ b/test/controllers/workflows_controller_test.rb @@ -0,0 +1,43 @@ +require "test_helper" + +class WorkflowsControllerTest < ActionDispatch::IntegrationTest + setup do + sign_in_as :kevin + end + + test "index" do + get workflows_path + assert_in_body workflows(:on_call).name + end + + test "create" do + get new_workflow_path + assert_response :success + + assert_difference -> { Workflow.count }, +1 do + post workflows_path, params: { workflow: { name: "My new workflow!" } } + assert_redirected_to workflows_path + end + end + + test "show" do + get workflow_path(workflows(:on_call)) + assert_in_body workflows(:on_call).name + end + + test "update" do + get edit_workflow_path(workflows(:on_call)) + assert_response :success + + put workflow_path(workflows(:on_call)), params: { workflow: { name: "Monkeyflow!" } } + follow_redirect! + assert_in_body "Monkeyflow!" + end + + test "destroy" do + assert_difference -> { Workflow.count }, -1 do + delete workflow_path(workflows(:on_call)) + assert_redirected_to workflows_path + end + end +end diff --git a/test/models/workflow_test.rb b/test/models/workflow_test.rb new file mode 100644 index 000000000..23239b3b4 --- /dev/null +++ b/test/models/workflow_test.rb @@ -0,0 +1,8 @@ +require "test_helper" + +class WorkflowTest < ActiveSupport::TestCase + test "create with default stages" do + workflow = Workflow.create name: "My New Workflow" + assert_equal Workflow::DEFAULT_STAGES.sort, workflow.stages.collect(&:name).sort + end +end diff --git a/test/test_helper.rb b/test/test_helper.rb index 1447430b1..dd27bd4ca 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -11,5 +11,15 @@ module ActiveSupport fixtures :all include CardTestHelper, ChangeTestHelper, SessionTestHelper + + # FIXME: Remove when upstream in Rails has landed (https://github.com/rails/rails/pull/54938) + def assert_in_body(text) + assert_match /#{text}/, @response.body + end + + # FIXME: Remove when upstream in Rails has landed (https://github.com/rails/rails/pull/54938) + def assert_not_in_body(text) + assert_no_match /#{text}/, @response.body + end end end