2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-05 08:04:23 +08:00
|
|
|
class ThemeSettingsManager
|
|
|
|
attr_reader :name, :theme, :default
|
|
|
|
|
|
|
|
def self.types
|
|
|
|
ThemeSetting.types
|
|
|
|
end
|
|
|
|
|
FEATURE: Theme settings migrations (#24071)
This commit introduces a new feature that allows theme developers to manage the transformation of theme settings over time. Similar to Rails migrations, the theme settings migration system enables developers to write and execute migrations for theme settings, ensuring a smooth transition when changes are required in the format or structure of setting values.
Example use cases for the theme settings migration system:
1. Renaming a theme setting.
2. Changing the data type of a theme setting (e.g., transforming a string setting containing comma-separated values into a proper list setting).
3. Altering the format of data stored in a theme setting.
All of these use cases and more are now possible while preserving theme setting values for sites that have already modified their theme settings.
Usage:
1. Create a top-level directory called `migrations` in your theme/component, and then within the `migrations` directory create another directory called `settings`.
2. Inside the `migrations/settings` directory, create a JavaScript file using the format `XXXX-some-name.js`, where `XXXX` is a unique 4-digit number, and `some-name` is a descriptor of your choice that describes the migration.
3. Within the JavaScript file, define and export (as the default) a function called `migrate`. This function will receive a `Map` object and must also return a `Map` object (it's acceptable to return the same `Map` object that the function received).
4. The `Map` object received by the `migrate` function will include settings that have been overridden or changed by site administrators. Settings that have never been changed from the default will not be included.
5. The keys and values contained in the `Map` object that the `migrate` function returns will replace all the currently changed settings of the theme.
6. Migrations are executed in numerical order based on the XXXX segment in the migration filenames. For instance, `0001-some-migration.js` will be executed before `0002-another-migration.js`.
Here's a complete example migration script that renames a setting from `setting_with_old_name` to `setting_with_new_name`:
```js
// File name: 0001-rename-setting.js
export default function migrate(settings) {
if (settings.has("setting_with_old_name")) {
settings.set("setting_with_new_name", settings.get("setting_with_old_name"));
}
return settings;
}
```
Internal topic: t/109980
2023-11-02 13:10:15 +08:00
|
|
|
def self.cast_row_value(row)
|
|
|
|
type_name = self.types.invert[row.data_type].downcase.capitalize
|
|
|
|
klass = "ThemeSettingsManager::#{type_name}".constantize
|
2024-04-25 21:39:22 +08:00
|
|
|
klass.cast(klass.extract_value_from_row(row))
|
FEATURE: Theme settings migrations (#24071)
This commit introduces a new feature that allows theme developers to manage the transformation of theme settings over time. Similar to Rails migrations, the theme settings migration system enables developers to write and execute migrations for theme settings, ensuring a smooth transition when changes are required in the format or structure of setting values.
Example use cases for the theme settings migration system:
1. Renaming a theme setting.
2. Changing the data type of a theme setting (e.g., transforming a string setting containing comma-separated values into a proper list setting).
3. Altering the format of data stored in a theme setting.
All of these use cases and more are now possible while preserving theme setting values for sites that have already modified their theme settings.
Usage:
1. Create a top-level directory called `migrations` in your theme/component, and then within the `migrations` directory create another directory called `settings`.
2. Inside the `migrations/settings` directory, create a JavaScript file using the format `XXXX-some-name.js`, where `XXXX` is a unique 4-digit number, and `some-name` is a descriptor of your choice that describes the migration.
3. Within the JavaScript file, define and export (as the default) a function called `migrate`. This function will receive a `Map` object and must also return a `Map` object (it's acceptable to return the same `Map` object that the function received).
4. The `Map` object received by the `migrate` function will include settings that have been overridden or changed by site administrators. Settings that have never been changed from the default will not be included.
5. The keys and values contained in the `Map` object that the `migrate` function returns will replace all the currently changed settings of the theme.
6. Migrations are executed in numerical order based on the XXXX segment in the migration filenames. For instance, `0001-some-migration.js` will be executed before `0002-another-migration.js`.
Here's a complete example migration script that renames a setting from `setting_with_old_name` to `setting_with_new_name`:
```js
// File name: 0001-rename-setting.js
export default function migrate(settings) {
if (settings.has("setting_with_old_name")) {
settings.set("setting_with_new_name", settings.get("setting_with_old_name"));
}
return settings;
}
```
Internal topic: t/109980
2023-11-02 13:10:15 +08:00
|
|
|
end
|
|
|
|
|
2018-03-05 08:04:23 +08:00
|
|
|
def self.create(name, default, type, theme, opts = {})
|
|
|
|
type_name = self.types.invert[type].downcase.capitalize
|
|
|
|
klass = "ThemeSettingsManager::#{type_name}".constantize
|
|
|
|
klass.new(name, default, theme, opts)
|
|
|
|
end
|
|
|
|
|
FEATURE: Theme settings migrations (#24071)
This commit introduces a new feature that allows theme developers to manage the transformation of theme settings over time. Similar to Rails migrations, the theme settings migration system enables developers to write and execute migrations for theme settings, ensuring a smooth transition when changes are required in the format or structure of setting values.
Example use cases for the theme settings migration system:
1. Renaming a theme setting.
2. Changing the data type of a theme setting (e.g., transforming a string setting containing comma-separated values into a proper list setting).
3. Altering the format of data stored in a theme setting.
All of these use cases and more are now possible while preserving theme setting values for sites that have already modified their theme settings.
Usage:
1. Create a top-level directory called `migrations` in your theme/component, and then within the `migrations` directory create another directory called `settings`.
2. Inside the `migrations/settings` directory, create a JavaScript file using the format `XXXX-some-name.js`, where `XXXX` is a unique 4-digit number, and `some-name` is a descriptor of your choice that describes the migration.
3. Within the JavaScript file, define and export (as the default) a function called `migrate`. This function will receive a `Map` object and must also return a `Map` object (it's acceptable to return the same `Map` object that the function received).
4. The `Map` object received by the `migrate` function will include settings that have been overridden or changed by site administrators. Settings that have never been changed from the default will not be included.
5. The keys and values contained in the `Map` object that the `migrate` function returns will replace all the currently changed settings of the theme.
6. Migrations are executed in numerical order based on the XXXX segment in the migration filenames. For instance, `0001-some-migration.js` will be executed before `0002-another-migration.js`.
Here's a complete example migration script that renames a setting from `setting_with_old_name` to `setting_with_new_name`:
```js
// File name: 0001-rename-setting.js
export default function migrate(settings) {
if (settings.has("setting_with_old_name")) {
settings.set("setting_with_new_name", settings.get("setting_with_old_name"));
}
return settings;
}
```
Internal topic: t/109980
2023-11-02 13:10:15 +08:00
|
|
|
def self.cast(value)
|
|
|
|
value
|
|
|
|
end
|
|
|
|
|
2024-04-25 21:39:22 +08:00
|
|
|
def self.extract_value_from_row(row)
|
|
|
|
row.value
|
|
|
|
end
|
|
|
|
|
2018-03-05 08:04:23 +08:00
|
|
|
def initialize(name, default, theme, opts = {})
|
|
|
|
@name = name.to_sym
|
|
|
|
@default = default
|
|
|
|
@theme = theme
|
|
|
|
@opts = opts
|
|
|
|
@types = self.class.types
|
|
|
|
end
|
|
|
|
|
|
|
|
def value
|
2021-09-16 10:28:53 +08:00
|
|
|
has_record? ? db_record.value : default
|
2018-03-05 08:04:23 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def type_name
|
|
|
|
self.class.name.demodulize.downcase.to_sym
|
|
|
|
end
|
|
|
|
|
|
|
|
def type
|
|
|
|
@types[type_name]
|
|
|
|
end
|
|
|
|
|
|
|
|
def description
|
2019-05-31 21:49:59 +08:00
|
|
|
@opts[:description] # Old method of specifying description. Is now overridden by locale file
|
2018-03-05 08:04:23 +08:00
|
|
|
end
|
|
|
|
|
2021-11-22 20:16:56 +08:00
|
|
|
def requests_refresh?
|
|
|
|
@opts[:refresh]
|
|
|
|
end
|
|
|
|
|
2018-03-05 08:04:23 +08:00
|
|
|
def value=(new_value)
|
|
|
|
ensure_is_valid_value!(new_value)
|
2024-03-07 16:38:11 +08:00
|
|
|
value = new_value.to_s
|
|
|
|
|
|
|
|
record = has_record? ? update_record!(value:) : create_record!(value:)
|
2018-03-05 08:04:23 +08:00
|
|
|
|
|
|
|
record.value
|
|
|
|
end
|
|
|
|
|
|
|
|
def db_record
|
2018-12-21 01:13:05 +08:00
|
|
|
# theme.theme_settings will already be preloaded, so it is better to use
|
|
|
|
# `find` on an array, rather than make a round trip to the database
|
|
|
|
theme.theme_settings.to_a.find do |i|
|
|
|
|
i.name.to_s == @name.to_s && i.data_type.to_s == type.to_s
|
2023-01-09 20:10:19 +08:00
|
|
|
end
|
2018-03-05 08:04:23 +08:00
|
|
|
end
|
|
|
|
|
2024-03-07 16:38:11 +08:00
|
|
|
def update_record!(args)
|
|
|
|
db_record.tap { |instance| instance.update!(args) }
|
2018-03-05 08:04:23 +08:00
|
|
|
end
|
|
|
|
|
2024-03-07 16:38:11 +08:00
|
|
|
def create_record!(args)
|
|
|
|
record = ThemeSetting.new(name: @name, data_type: type, theme: @theme, **args)
|
2018-03-05 08:04:23 +08:00
|
|
|
record.save!
|
|
|
|
record
|
|
|
|
end
|
|
|
|
|
2024-03-07 16:38:11 +08:00
|
|
|
def has_record?
|
|
|
|
db_record.present?
|
|
|
|
end
|
|
|
|
|
2018-03-05 08:04:23 +08:00
|
|
|
def ensure_is_valid_value!(new_value)
|
2024-02-21 08:08:26 +08:00
|
|
|
return if new_value.nil?
|
|
|
|
|
|
|
|
error_messages = ThemeSettingsValidator.validate_value(new_value, type, @opts)
|
|
|
|
raise Discourse::InvalidParameters.new error_messages.join(" ") if error_messages.present?
|
2018-03-05 08:04:23 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def has_min?
|
|
|
|
min = @opts[:min]
|
|
|
|
(min.is_a?(::Integer) || min.is_a?(::Float)) && min != -::Float::INFINITY
|
|
|
|
end
|
|
|
|
|
|
|
|
def has_max?
|
|
|
|
max = @opts[:max]
|
|
|
|
(max.is_a?(::Integer) || max.is_a?(::Float)) && max != ::Float::INFINITY
|
|
|
|
end
|
|
|
|
end
|