Class: SiteSettingMoveToGroupsMigrationGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
SiteSettingExtension
Defined in:
lib/generators/site_setting_move_to_groups_migration/site_setting_move_to_groups_migration_generator.rb

Constant Summary

Constants included from SiteSettingExtension

SiteSettingExtension::HOSTNAME_SETTINGS

Constants included from SiteSettings::DeprecatedSettings

SiteSettings::DeprecatedSettings::OVERRIDE_TL_GROUP_SETTINGS, SiteSettings::DeprecatedSettings::SETTINGS

Instance Method Summary collapse

Methods included from SiteSettingExtension

#add_override!, #after_fork, #all_settings, #areas, #categories, #client_settings, client_settings_cache_key, #client_settings_json, #client_settings_json_uncached, #current, #default_locale, #default_locale=, #default_locale?, #defaults, #deprecated_setting_alias, #deprecated_settings, #description, #ensure_listen_for_changes, extended, #filter_value, #get, #has_setting?, #hidden_settings, #hidden_settings_provider, #info, #keywords, #listen_for_changes=, #load_settings, #mandatory_values, #mutex, #notify_changed!, #notify_clients!, #placeholder, #plugins, #previews, #process_id, #process_message, #provider, #provider=, #refresh!, #refresh_settings, #remove_override!, #requires_confirmation_settings, #requires_refresh?, #secret_settings, #set, #set_and_log, #settings_hash, #shadowed_settings, #supported_types, #type_supervisor, #types, #valid_areas

Methods included from HasSanitizableFields

#sanitize_field

Methods included from SiteSettings::DeprecatedSettings

#group_to_tl, #is_tl_and_staff_setting?, #setup_deprecated_methods, #tl_to_group

Instance Method Details

#create_migration_fileObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/generators/site_setting_move_to_groups_migration/site_setting_move_to_groups_migration_generator.rb', line 9

def create_migration_file
  migration_version = ActiveRecord::Migration.next_migration_number(0)
  file_path = "db/migrate/#{migration_version}_fill_#{new_name}_based_on_deprecated_setting.rb"
  class_name = "Fill#{new_name.classify}BasedOnDeprecatedSetting"

  load_all_settings
  validate_setting_name!(old_name)
  validate_setting_name!(new_name)
  validate_setting_type!(old_name)

  create_file file_path, <<~MIGRATION_FILE if setting_type(old_name) == "TrustLevelSetting"
      # frozen_string_literal: true

      class #{class_name} < ActiveRecord::Migration[7.0]
        def up
          old_setting_trust_level =
            DB.query_single(
              "SELECT value FROM site_settings WHERE name = '#{old_name}' LIMIT 1",
            ).first

          if old_setting_trust_level.present?
            allowed_groups = "1\#\{old_setting_trust_level\}"

            DB.exec(
              "INSERT INTO site_settings(name, value, data_type, created_at, updated_at)
              VALUES('#{new_name}', :setting, '20', NOW(), NOW())",
              setting: allowed_groups,
            )
          end
        end

        def down
          raise ActiveRecord::IrreversibleMigration
        end
      end
    MIGRATION_FILE
  if setting_type(old_name) == "TrustLevelAndStaffSetting"
    create_file file_path, <<~MIGRATION_FILE
      # frozen_string_literal: true

      class #{class_name} < ActiveRecord::Migration[7.0]
        def up
          old_setting_trust_level =
            DB.query_single(
              "SELECT value FROM site_settings WHERE name = '#{old_name}' LIMIT 1",
            ).first

          if old_setting_trust_level.present?
            allowed_groups =
              case old_setting_trust_level
              when "admin"
                "1"
              when "staff"
                "3"
              when "0"
                "10"
              when "1"
                "11"
              when "2"
                "12"
              when "3"
                "13"
              when "4"
                "14"
              end

            DB.exec(
              "INSERT INTO site_settings(name, value, data_type, created_at, updated_at)
              VALUES('#{new_name}', :setting, '20', NOW(), NOW())",
              setting: allowed_groups,
            )
          end
        end

        def down
          raise ActiveRecord::IrreversibleMigration
        end
      end
    MIGRATION_FILE
  end
end