A platform for community discussion. Free, open, simple.
DEV: Implement depends_behavior for depends_on in site settings (#37370)
Martin Brennan60259dd45395
diff --git a/config/site_settings.yml b/config/site_settings.ymlindex 0e44bbde89f..13894a6e40f 100644--- a/config/site_settings.yml+++ b/config/site_settings.yml@@ -25,6 +25,10 @@# - status: experimental | alpha | beta | stable# - impact: two parts separated by comma - feature|other , staff|admins|moderators|all_members|developers# - learn_more_url: a url for more information+# depends_on - One or more boolean settings that must be true for this setting to take effect.+# depends_behavior - The behavior of the dependent setting. Only currently supported behaviour is "hidden"+# to hide the setting if the dependent setting is false. If left blank, no special behavior+# is applied and the setting is shown as normal.###diff --git a/lib/site_setting_extension.rb b/lib/site_setting_extension.rbindex 2ba117734a2..5873a7f1485 100644--- a/lib/site_setting_extension.rb+++ b/lib/site_setting_extension.rb@@ -362,6 +362,18 @@ module SiteSettingExtension.select do |setting_name, _|is_hidden = current_hidden_settings.include?(setting_name)+ if type_supervisor.dependencies[setting_name].present? &&+ type_supervisor.dependencies.behaviors[setting_name] == :hidden+ # Hidden if any of the dependent settings are not true+ is_hidden =+ !type_supervisor.dependencies[setting_name].all? do |dependency|+ # TODO (martin) Fix this when https://github.com/discourse/discourse/pull/37283 is merged+ # to take into account the dynamic upcoming change settings based on promotion status+ # using UpcomingChange.resolved_value+ current[dependency.to_sym]+ end+ end+next true if !is_hiddennext false if !include_hiddennext true if filter_allowed_hidden.nil?diff --git a/lib/site_settings/dependency_graph.rb b/lib/site_settings/dependency_graph.rbindex 874a95e6cdc..2657e08b6a6 100644--- a/lib/site_settings/dependency_graph.rb+++ b/lib/site_settings/dependency_graph.rb@@ -6,22 +6,33 @@ endclass SiteSettings::DependencyGraphinclude TSort+ attr_reader :dependencies, :behaviors+def initialize(dependencies = {})@dependencies = dependencies+ @behaviors = {}enddef []=(setting, value)dependencies[setting] = valueend+ def [](setting)+ dependencies[setting]+ end++ def change_behavior(setting, behavior)+ behavior = behavior.to_sym+ raise ArgumentError.new("Behavior must be :hidden") unless behavior == :hidden+ behaviors[setting] = behavior+ end+def order@order ||= tsortendprivate- attr_reader :dependencies-def tsort_each_child(node, &block)dependencies.fetch(node, []).each(&block)enddiff --git a/lib/site_settings/type_supervisor.rb b/lib/site_settings/type_supervisor.rbindex 08ec948849c..7820b8ff517 100644--- a/lib/site_settings/type_supervisor.rb+++ b/lib/site_settings/type_supervisor.rb@@ -23,6 +23,7 @@ class SiteSettings::TypeSupervisorschemarequires_confirmationdepends_on+ depends_behaviorauthorized_extensionsmax_file_size_kb].freeze@@ -158,6 +159,7 @@ class SiteSettings::TypeSupervisorend@dependencies[name] = opts[:depends_on] || []+ @dependencies.change_behavior(name, opts[:depends_behavior]) if opts[:depends_behavior]enddef to_rb_value(name, value, override_type = nil)diff --git a/spec/lib/site_setting_extension_spec.rb b/spec/lib/site_setting_extension_spec.rbindex 9c5adb7cdcc..024fedbdc77 100644--- a/spec/lib/site_setting_extension_spec.rb+++ b/spec/lib/site_setting_extension_spec.rb@@ -690,6 +690,88 @@ RSpec.describe SiteSettingExtension doendend+ describe "dependent settings" do+ context "when a dependent setting depends_behavior is not set" do+ before do+ settings.setting(:cool_thing_image, nil, depends_on: [:enable_cool_thing])+ settings.refresh!+ end++ it "is present in all_settings" do+ expect(settings.all_settings.find { |s| s[:setting] == :cool_thing_image }).not_to be_blank+ end+ end++ context "when a dependent setting depends_behavior is hidden" do+ before do+ settings.setting(+ :cool_thing_image,+ nil,+ depends_on: [:enable_cool_thing],+ depends_behavior: :hidden,+ )+ settings.refresh!+ end++ context "when the depends_on setting is an upcoming change" do+ context "when the upcoming change is enabled by an admin" do+ before do+ settings.setting(+ :enable_cool_thing,+ true,+ upcoming_change: {+ status: :alpha,+ impact: "feature,staff",+ },+ )+ settings.refresh!+ end++ it "is present in all_settings" do+ expect(+ settings.all_settings.find { |s| s[:setting] == :cool_thing_image },+ ).not_to be_blank+ end+ end++ context "when the upcoming change is automatically enabled because of the promotion status" do+ # TODO (martin) Fix this when https://github.com/discourse/discourse/pull/37283 is merged+ # to take into account the dynamic upcoming change settings based on promotion status+ # using UpcomingChange.resolved_value+ xit "is present in all_settings" do+ expect(+ settings.all_settings.find { |s| s[:setting] == :cool_thing_image },+ ).not_to be_blank+ end+ end+ end++ context "when the depends_on setting is true" do+ before do+ settings.setting(:enable_cool_thing, true)+ settings.refresh!+ end++ it "is present in all_settings" do+ expect(+ settings.all_settings.find { |s| s[:setting] == :cool_thing_image },+ ).not_to be_blank+ end+ end++ context "when the depends_on setting is false" do+ before do+ settings.setting(:enable_cool_thing, false)+ settings.refresh!+ end++ it "is not present in all_settings" do+ expect(settings.all_settings.find { |s| s[:setting] == :cool_thing_image }).to be_blank+ end+ end+ end+ end+describe "hidden" dobefore dosettings.setting(:other_setting, "Blah")