From 81bc1034ac5dbb47ea0720b57517e99e99729ec2 Mon Sep 17 00:00:00 2001
From: Alexander Olofsson <alexander.olofsson@liu.se>
Date: Wed, 30 Nov 2022 13:46:48 +0100
Subject: [PATCH] Make rubocop happy

---
 .rubocop.yml                                  |  9 ++++
 .../notification_extensions.rb                | 48 +++++++++----------
 .../notification_target.rb                    |  2 +-
 .../sender_matrix.rb                          |  1 +
 ...20200414141644_add_notification_targets.rb |  3 +-
 lib/foreman_notification_send.rb              |  2 +
 lib/foreman_notification_send/version.rb      |  4 +-
 7 files changed, 41 insertions(+), 28 deletions(-)
 create mode 100644 .rubocop.yml

diff --git a/.rubocop.yml b/.rubocop.yml
new file mode 100644
index 0000000..9bec14f
--- /dev/null
+++ b/.rubocop.yml
@@ -0,0 +1,9 @@
+---
+AllCops:
+  NewCops: enable
+
+Metrics/AbcSize:
+  Enabled: false
+
+Style/Documentation:
+  Enabled: false
diff --git a/app/models/concerns/foreman_notification_send/notification_extensions.rb b/app/models/concerns/foreman_notification_send/notification_extensions.rb
index a573562..a6b6254 100644
--- a/app/models/concerns/foreman_notification_send/notification_extensions.rb
+++ b/app/models/concerns/foreman_notification_send/notification_extensions.rb
@@ -1,4 +1,5 @@
 # frozen_string_literal: true
+
 module ForemanNotificationSend
   module NotificationExtensions
     def self.prepended(base)
@@ -8,20 +9,12 @@ module ForemanNotificationSend
     end
 
     def send_notification
-      if Setting[:notification_send_enable]
-        sender = SenderBase.create_sender(
-          backend: :matrix,
-          hs_url: Setting[:notification_send_target_url],
-          access_token: Setting[:notification_send_token],
-          room: Setting[:notification_send_target_room]
-        )
-        sender.send_notification(self)
-      end
+      send_notification_fallback if Setting[:notification_send_enable]
 
-      #NotificationTarget.select { |target| target.should_send?(self) }
-      #                  .each { |target| target.send(self) }
-    rescue StandardError => ex
-      Foreman::Logging.exception "Failed to send notification #{self}", ex
+      # NotificationTarget.select { |target| target.should_send?(self) }
+      #                   .each { |target| target.send(self) }
+    rescue StandardError => e
+      Foreman::Logging.exception "Failed to send notification #{self}", e
     end
 
     def level_to_symbol
@@ -39,18 +32,15 @@ module ForemanNotificationSend
 
     def to_html
       unless actions.empty?
-        action_str = ''
-
-        (actions[:links] || []).each do |action|
+        munged = (actions[:links] || []).map do |action|
           next unless action.key?(:href) && action.key?(:title)
 
           url = action[:href]
           url = Setting[:foreman_url] + url unless action[:external]
 
-          action_str += ' | ' unless action_str.empty?
-          action_str += "<a href=\"#{url}\">#{action[:title]}</a>"
+          "<a href=\"#{url}\">#{action[:title]}</a>"
         end
-        action_str = "<br/>[ #{action_str} ]"
+        action_str = "<br/>[ #{munged.join ' | '} ]"
       end
 
       "<b>#{notification_blueprint.group}</b>:<br/>#{level_to_symbol} #{message}#{action_str}"
@@ -58,20 +48,28 @@ module ForemanNotificationSend
 
     def to_markdown
       unless actions.empty?
-        action_str = ''
-
-        (actions[:links] || []).each do |action|
+        munged = (actions[:links] || []).map do |action|
           next unless action.key?(:href) && action.key?(:title)
 
           url = action[:href]
           url = Setting[:foreman_url] + url unless action[:external]
 
-          action_str += ' | ' unless action_str.empty?
-          action_str += "[#{action[:title]}](#{url})"
+          "[#{action[:title]}](#{url})"
         end
-        action_str = "  \n\\[ #{action_str}\\ ]"
+        action_str = "  \n\\[ #{munged.join ' | '}\\ ]"
       end
       "**#{notification_blueprint.group}**:\n#{level_to_symbol} #{message}#{action_str}"
     end
+
+    private
+
+    def send_notification_fallback
+      SenderBase.create_sender(
+        backend: :matrix,
+        hs_url: Setting[:notification_send_target_url],
+        access_token: Setting[:notification_send_token],
+        room: Setting[:notification_send_target_room]
+      ).send_notification(self)
+    end
   end
 end
diff --git a/app/models/foreman_notification_send/notification_target.rb b/app/models/foreman_notification_send/notification_target.rb
index 01e3c59..375682b 100644
--- a/app/models/foreman_notification_send/notification_target.rb
+++ b/app/models/foreman_notification_send/notification_target.rb
@@ -3,7 +3,7 @@
 module ForemanNotificationSend
   class NotificationTarget < ApplicationRecord
     before_validation do
-      backend = slugify_backend if backend
+      # backend = slugify_backend if backend
     end
     validate :validate_backend
 
diff --git a/app/services/foreman_notification_send/sender_matrix.rb b/app/services/foreman_notification_send/sender_matrix.rb
index 06bb522..a5e6bf8 100644
--- a/app/services/foreman_notification_send/sender_matrix.rb
+++ b/app/services/foreman_notification_send/sender_matrix.rb
@@ -1,4 +1,5 @@
 # frozen_string_literal: true
+
 require 'matrix_sdk/api'
 
 module ForemanNotificationSend
diff --git a/db/migrate/20200414141644_add_notification_targets.rb b/db/migrate/20200414141644_add_notification_targets.rb
index 268bf01..081d331 100644
--- a/db/migrate/20200414141644_add_notification_targets.rb
+++ b/db/migrate/20200414141644_add_notification_targets.rb
@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
 class AddNotificationTargets < ActiveRecord::Migration[5.1]
   def change
     create_table :notification_targets do |t|
@@ -11,4 +13,3 @@ class AddNotificationTargets < ActiveRecord::Migration[5.1]
     end
   end
 end
-
diff --git a/lib/foreman_notification_send.rb b/lib/foreman_notification_send.rb
index d56ba1a..80cd19a 100644
--- a/lib/foreman_notification_send.rb
+++ b/lib/foreman_notification_send.rb
@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
 require 'foreman_notification_send/engine'
 
 module ForemanNotificationSend
diff --git a/lib/foreman_notification_send/version.rb b/lib/foreman_notification_send/version.rb
index dcf95fb..71d2953 100644
--- a/lib/foreman_notification_send/version.rb
+++ b/lib/foreman_notification_send/version.rb
@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
 module ForemanNotificationSend
-  VERSION = '0.1.0'.freeze
+  VERSION = '0.1.0'
 end
-- 
GitLab