From 679c5160b2cfaa1fc770f5dc65a5ed56834f6e6d Mon Sep 17 00:00:00 2001
From: Taico Aerts <t.v.aerts@tudelft.nl>
Date: Thu, 10 Mar 2022 23:26:22 +0100
Subject: [PATCH] Update to new mechanics

---
 app/controllers/admin/companies_controller.rb |  2 +-
 .../admin/course_editions_controller.rb       | 76 ++++++++-----------
 app/controllers/companies_controller.rb       | 51 +++++--------
 3 files changed, 52 insertions(+), 77 deletions(-)

diff --git a/app/controllers/admin/companies_controller.rb b/app/controllers/admin/companies_controller.rb
index 0afdafe25..f2e06adf0 100644
--- a/app/controllers/admin/companies_controller.rb
+++ b/app/controllers/admin/companies_controller.rb
@@ -42,7 +42,7 @@ module Admin
     ##
     # Action to create a new company
     def create
-      @company = Company.create company_params
+      @company = Company.create(company_params)
       helpers.flash_crud_msg(@company,
                              on_success: proc { redirect_to admin_company_url(@company) },
                              on_failure: proc { render 'new' })
diff --git a/app/controllers/admin/course_editions_controller.rb b/app/controllers/admin/course_editions_controller.rb
index a1314ca67..ea0e413cc 100644
--- a/app/controllers/admin/course_editions_controller.rb
+++ b/app/controllers/admin/course_editions_controller.rb
@@ -39,35 +39,26 @@ module Admin
 
     def create
       # Create course edition. In case of failure, render new page again and return
-      @course_edition = CourseEdition.create(course_edition_params.merge(course: @course))
+      # On successful creation, also create algorithm param set, exclusivity group, copy files (in case of duplication) and create roles.
+      @course_edition = CourseEdition.create(course_edition_params)
       helpers.flash_crud_msg(@course_edition,
                              html: false,
-                             on_failure: proc { render 'new' }) or
-        return
-
-      # Create algorithm params. In case of failure, render new page again and return
-      @algorithm_params = AlgorithmParamSet.create(course_edition: @course_edition)
-      helpers.flash_crud_msg(@algorithm_params,
-                             model_name: CourseEdition.model_name.human,
-                             html: false,
-                             on_failure: proc { render 'new' }) or
-        return
-
-      # Update roles, exclusivity groups and copy files (in case of duplication)
-      helpers.flash_crud_msg(@course_edition, extra_checks:
-        {
-          Role.model_name.human(count: 2) =>
-            update_multiple_roles(course_edition_params_with_staff['staff'],
-                                  { '0': :coordinator, '1': :enrollment_manager, '2': :viewer }, @course_edition).any?,
-
-          ExclusivityGroup.model_name.human(count: 2) =>
-            update_exclusivity_groups.any?,
-
-          CourseFile.model_name.human(count: 2) =>
-            copy_files
-        }, html: false)
-
-      redirect_to admin_course_edition_url(@course, @course_edition)
+                             extra_checks:
+                               {
+                                 AlgorithmParamSet.model_name.human => lambda { AlgorithmParamSet.create(course_edition: @course_edition) },
+                                 ExclusivityGroup.model_name.human(count: 2) => lambda { update_exclusivity_groups.none? },
+                                 CourseFile.model_name.human(count: 2) => lambda { copy_files != true },
+                                 Role.model_name.human(count: 2) => lambda do
+                                   update_multiple_roles(
+                                     course_edition_params_with_staff['staff'],
+                                     { '0': :coordinator, '1': :enrollment_manager, '2': :viewer },
+                                     @course_edition
+                                   ).none?
+                                   end,
+                               },
+                             on_success: proc { redirect_to admin_course_edition_url(@course, @course_edition) },
+                             on_part_failure: proc { redirect_to admin_course_edition_url(@course, @course_edition) },
+                             on_failure: proc { render 'new' })
     end
 
     def edit
@@ -110,24 +101,21 @@ module Admin
     end
 
     def update
-      # Update course edition, or render the edit page again and return
+      # Update course edition, roles and exclusivity groups (all at the same time). If anything fails, show the edit page again.
       @course_edition.update(course_edition_params)
-      helpers.flash_crud_msg(@course_edition, on_failure: proc { render 'edit' }) or
-        return
-
-      # Update roles and exclusivity groups
-      helpers.flash_crud_msg(@course_edition, extra_checks:
-        {
-          Role.model_name.human(count: 2) =>
-            update_multiple_roles(course_edition_params_with_staff['staff'],
-                                  { '0': :coordinator, '1': :enrollment_manager, '2': :viewer }, @course_edition).any?,
-
-          ExclusivityGroup.model_name.human(count: 2) =>
-            update_exclusivity_groups.any?
-        }, on_failure: proc { render 'edit' }) or
-        return
-
-      redirect_to admin_course_edition_url(@course, @course_edition)
+      helpers.flash_crud_msg(@course_edition,
+                             extra_checks:
+                               {
+                                 ExclusivityGroup.model_name.human(count: 2) => update_exclusivity_groups.none?,
+                                 Role.model_name.human(count: 2) =>
+                                   update_multiple_roles(
+                                     course_edition_params_with_staff['staff'],
+                                     { '0': :coordinator, '1': :enrollment_manager, '2': :viewer },
+                                     @course_edition
+                                   ).none?
+                               },
+                             on_success: proc { redirect_to admin_course_edition_url(@course, @course_edition) },
+                             on_failure: proc { render 'edit' })
     end
 
     def import_projects
diff --git a/app/controllers/companies_controller.rb b/app/controllers/companies_controller.rb
index 0739f3833..6d4edf540 100644
--- a/app/controllers/companies_controller.rb
+++ b/app/controllers/companies_controller.rb
@@ -25,8 +25,6 @@ class CompaniesController < ApplicationController
     @has_company_invitations = @company_invitations.any?
   end
 
-  ##
-  # Action to show a company (to both external and internal users).
   def show
     authorize! :show, @company
     add_breadcrumb @company.name, company_path(@company)
@@ -41,48 +39,36 @@ class CompaniesController < ApplicationController
               .group_by(&:course_edition)
   end
 
-  ##
-  # Action to view the edit form for a company.
   def edit
     add_breadcrumb @company.name, company_path(@company)
     add_breadcrumb 'Edit', edit_company_path(@company)
   end
 
-  ##
-  # Action to view the form for submitting a new company.
   def new
     add_breadcrumb 'New', new_company_path(@company)
   end
 
-  ##
-  # Action to create a new company.
   def create
-    @company = Company.create company_params
-    if current_user.internal?
-      @company.affiliation = 1
-    end
+    @company = Company.create(
+      company_params.merge(
+        affiliation: current_user.internal? ? :internal : :external)
+    )
 
-    if @company.id
-      save_image
-      current_user.add_role :registrar, @company
-      flash[:success] = "Successfully created company #{@company.name}"
-      redirect_to company_path(@company)
-    else
-      flash[:danger] = 'Failed to create company'
-      render 'new'
-    end
+    helpers.flash_crud_msg(@company, html: false,
+                           extra_checks: {
+                             Image.model_name.human => lambda { save_image },
+                             Role.model_name.human => lambda { current_user.add_role(:registrar, @company).persisted? rescue false }
+                           },
+                           on_success: proc { redirect_to company_path(@company) },
+                           on_part_failure: proc { redirect_to company_path(@company) },
+                           on_failure: proc { render 'edit' })
   end
 
-  ##
-  # Action to update an existing company using +company_params+.
   def update
-    if @company.update(company_update_params)
-      flash[:success] = 'Successfully updated the company'
-      redirect_to company_path(@company)
-    else
-      flash[:danger] = 'Failed to update the company'
-      render 'edit'
-    end
+    @company.update(company_update_params)
+    helpers.flash_crud_msg(@company,
+                           on_success: proc { redirect_to company_path(@company) },
+                           on_failure: proc { render 'edit' })
   end
 
   protected
@@ -116,10 +102,11 @@ class CompaniesController < ApplicationController
     )
   end
 
+  # @return [Boolean] false in case there was an image and saving it was unsuccessful
   def save_image
-    return unless params[:company][:image_attributes]
+    return true unless params[:company][:image_attributes]
 
-    image = Image.new image_params
+    image = Image.new(image_params)
     image.imageable_type = 'Company'
     image.imageable_id = @company.id
     image.save
-- 
GitLab