From 9cf45b058627f039040165519de9c2074dda141f Mon Sep 17 00:00:00 2001
From: Lin Jen-Shin <godfat@godfat.org>
Date: Tue, 14 Jun 2016 23:11:43 +0800
Subject: [PATCH] Return the association and check it in controller instead:

Feedback:
https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/4641#note_12444891
---
 app/controllers/admin/runner_projects_controller.rb    | 4 +++-
 app/controllers/projects/runner_projects_controller.rb | 3 ++-
 app/models/ci/runner.rb                                | 2 +-
 lib/api/runners.rb                                     | 4 +++-
 4 files changed, 9 insertions(+), 4 deletions(-)

diff --git a/app/controllers/admin/runner_projects_controller.rb b/app/controllers/admin/runner_projects_controller.rb
index 29307aeab6d..5383afdbd20 100644
--- a/app/controllers/admin/runner_projects_controller.rb
+++ b/app/controllers/admin/runner_projects_controller.rb
@@ -11,7 +11,9 @@ class Admin::RunnerProjectsController < Admin::ApplicationController
 
     return head(403) if runner.is_shared? || runner.is_locked?
 
-    if @runner.assign_to(@project, current_user)
+    runner_project = @runner.assign_to(@project, current_user)
+
+    if runner_project.persisted?
       redirect_to admin_runner_path(@runner)
     else
       redirect_to admin_runner_path(@runner), alert: 'Failed adding runner to project'
diff --git a/app/controllers/projects/runner_projects_controller.rb b/app/controllers/projects/runner_projects_controller.rb
index 4c013303269..2413e583d7b 100644
--- a/app/controllers/projects/runner_projects_controller.rb
+++ b/app/controllers/projects/runner_projects_controller.rb
@@ -10,8 +10,9 @@ class Projects::RunnerProjectsController < Projects::ApplicationController
     return head(403) unless current_user.ci_authorized_runners.include?(@runner)
 
     path = runners_path(project)
+    runner_project = @runner.assign_to(project, current_user)
 
-    if @runner.assign_to(project, current_user)
+    if runner_project.persisted?
       redirect_to path
     else
       redirect_to path, alert: 'Failed adding runner to project'
diff --git a/app/models/ci/runner.rb b/app/models/ci/runner.rb
index df343fc957a..8149929f492 100644
--- a/app/models/ci/runner.rb
+++ b/app/models/ci/runner.rb
@@ -63,7 +63,7 @@ module Ci
     def assign_to(project, current_user = nil)
       self.is_shared = false if shared?
       self.save
-      project.runner_projects.create(runner_id: self.id).persisted?
+      project.runner_projects.create(runner_id: self.id)
     end
 
     def display_name
diff --git a/lib/api/runners.rb b/lib/api/runners.rb
index 2c2610fc2e7..ecc8f2fc5a2 100644
--- a/lib/api/runners.rb
+++ b/lib/api/runners.rb
@@ -97,7 +97,9 @@ module API
         runner = get_runner(params[:runner_id])
         authenticate_enable_runner!(runner)
 
-        if runner.assign_to(user_project)
+        runner_project = runner.assign_to(user_project)
+
+        if runner_project.persisted?
           present runner, with: Entities::Runner
         else
           conflict!("Runner was already enabled for this project")
-- 
GitLab