From ed2dedd8051dd754148a471add16c2c8b36ba9d1 Mon Sep 17 00:00:00 2001 From: Alexander Olofsson <alexander.olofsson@liu.se> Date: Mon, 13 Mar 2023 10:57:48 +0100 Subject: [PATCH] Convert settings to DSL --- app/models/setting/notification_send.rb | 31 --------------- .../20230313105520_convert_settings_to_dsl.rb | 9 +++++ lib/foreman_notification_send/engine.rb | 39 ++++++++++++++----- 3 files changed, 38 insertions(+), 41 deletions(-) delete mode 100644 app/models/setting/notification_send.rb create mode 100644 db/migrate/20230313105520_convert_settings_to_dsl.rb diff --git a/app/models/setting/notification_send.rb b/app/models/setting/notification_send.rb deleted file mode 100644 index 110dcc9..0000000 --- a/app/models/setting/notification_send.rb +++ /dev/null @@ -1,31 +0,0 @@ -# frozen_string_literal: true - -class Setting - class NotificationSend < ::Setting - def self.default_settings - [ - set('notification_send_enable', _('Enable'), false, N_('Enable')), - set('notification_send_target_url', _('Target URI'), 'https://matrix.org', N_('Target URI')), - set('notification_send_target_room', _('Target Room'), '#test:matrix.org', N_('Target Room')), - set('notification_send_token', _('Token'), 'abcdefg', N_('Token')) - ] - end - - def self.load_defaults - # Check the table exists - return unless super - - transaction do - default_settings.each do |s| - create! s.update(category: 'Setting::NotificationSend') - end - end - - true - end - - def self.humanized_category - N_('Notification Send') - end - end -end diff --git a/db/migrate/20230313105520_convert_settings_to_dsl.rb b/db/migrate/20230313105520_convert_settings_to_dsl.rb new file mode 100644 index 0000000..0b8c9a0 --- /dev/null +++ b/db/migrate/20230313105520_convert_settings_to_dsl.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +class ConvertSettingsToDsl < ActiveRecord::Migration[6.0] + def up + # rubocop:disable Rails/SkipsModelValidations + Settings.where(category: 'Setting::NotificationSend').update_all(category: 'Setting') + # rubocop:enable Rails/SkipsModelValidations + end +end diff --git a/lib/foreman_notification_send/engine.rb b/lib/foreman_notification_send/engine.rb index 466e7d1..c5f9f59 100644 --- a/lib/foreman_notification_send/engine.rb +++ b/lib/foreman_notification_send/engine.rb @@ -13,18 +13,37 @@ module ForemanNotificationSend end end - initializer 'foreman_ipxe.load_default_settings', before: :load_config_initializers do - require_dependency File.expand_path('../../app/models/setting/notification_send.rb', __dir__) if \ - begin - Setting.table_exists? - rescue StandardError - (false) - end - end - initializer 'foreman_notification_send.register_plugin', before: :finisher_hook do |_app| Foreman::Plugin.register :foreman_notification_send do - requires_foreman '>= 1.16' + requires_foreman '>= 3.0' + + settings do + category :notification_send, N_('Notification Send') do + setting 'notification_send_enable', + type: :boolean, + description: N_('Enable'), + default: false, + full_name: N_('Enable') + + setting 'notification_send_target_url', + type: :string, + description: N_('Target URI'), + default: 'https://matrix.org', + full_name: N_('Target URI') + + setting 'notification_send_target_room', + type: :string, + description: N_('Target Room'), + default: '#test:matrix.org', + full_name: N_('Target Room') + + setting 'notification_send_token', + type: :string, + description: N_('Token'), + default: 'syt_abcdefg', + full_name: N_('Token') + end + end end end -- GitLab