From fd63d89753a9e2842c4f8ad1a8c40006fa9d59f2 Mon Sep 17 00:00:00 2001 From: Sam Date: Fri, 7 Aug 2015 11:41:48 +1000 Subject: [PATCH] FEATURE: simpler definition of enum types --- app/models/category_style_setting.rb | 19 ------------------- config/site_settings.yml | 7 ++++++- lib/site_setting_extension.rb | 22 +++++++++++++++++++--- 3 files changed, 25 insertions(+), 23 deletions(-) delete mode 100644 app/models/category_style_setting.rb diff --git a/app/models/category_style_setting.rb b/app/models/category_style_setting.rb deleted file mode 100644 index e62720ebc04..00000000000 --- a/app/models/category_style_setting.rb +++ /dev/null @@ -1,19 +0,0 @@ -# TODO all enums should probably move out of models -# TODO we should be able to do this kind of stuff without a backing class -require_dependency 'enum_site_setting' - -class CategoryStyleSetting < EnumSiteSetting - - VALUES = ["bar", "box", "bullet"] - - def self.valid_value?(val) - VALUES.include?(val) - end - - def self.values - VALUES.map do |l| - {name: l, value: l} - end - end - -end diff --git a/config/site_settings.yml b/config/site_settings.yml index 42401150f9d..2bf7eb4369c 100644 --- a/config/site_settings.yml +++ b/config/site_settings.yml @@ -12,6 +12,7 @@ # type: email - Must be a valid email address. # type: username - Must match the username of an existing user. # type: list - A list of values, chosen from a set of valid values defined in the choices option. +# type: enum - A single value, chosen from a set of valid values in the choices option. # # A type:list setting with the word 'colors' in its name will make color values have a bold line of the corresponding color # @@ -147,7 +148,11 @@ basic: category_style: client: true default: 'bullet' - enum: 'CategoryStyleSetting' + type: enum + choices: + - bar + - box + - bullet preview: 'Category' enable_mobile_theme: client: true diff --git a/lib/site_setting_extension.rb b/lib/site_setting_extension.rb index facb22bb4b4..228a7e6f395 100644 --- a/lib/site_setting_extension.rb +++ b/lib/site_setting_extension.rb @@ -3,6 +3,11 @@ require_dependency 'site_settings/db_provider' module SiteSettingExtension + # For plugins, so they can tell if a feature is supported + def supported_types + [:email, :username, :list, :enum] + end + # part 1 of refactor, centralizing the dependency here def provider=(val) @provider = val @@ -171,7 +176,13 @@ module SiteSettingExtension category: categories[s], preview: previews[s] } - opts.merge!({valid_values: enum_class(s).values, translate_names: enum_class(s).translate_names?}) if type == :enum + + if type == :enum && enum_class(s) + opts.merge!({valid_values: enum_class(s).values, translate_names: enum_class(s).translate_names?}) + elsif type == :enum + opts.merge!({valid_values: choices[s].map{|c| {name: c, value: c}}, translate_names: false}) + end + opts[:choices] = choices[s] if choices.has_key? s opts end @@ -278,7 +289,11 @@ module SiteSettingExtension end if type == types[:enum] - raise Discourse::InvalidParameters.new(:value) unless enum_class(name).valid_value?(val) + if enum_class(name) + raise Discourse::InvalidParameters.new(:value) unless enum_class(name).valid_value?(val) + else + raise Discourse::InvalidParameters.new(:value) unless choices[name].include?(val) + end end if v = validators[name] @@ -408,7 +423,8 @@ module SiteSettingExtension 'username' => UsernameSettingValidator, types[:fixnum] => IntegerSettingValidator, types[:string] => StringSettingValidator, - 'list' => StringSettingValidator + 'list' => StringSettingValidator, + 'enum' => StringSettingValidator } @validator_mapping[type_name] end