DEV: introduce new API to look up dynamic site setting

DEV: introduce new API to look up dynamic site setting

This removes all uses of both send and public_send from consumers of SiteSetting and instead introduces a get helper for dynamic lookup

This leads to much cleaner and safer code long term as we are always explicit to test that a site setting is really there before sending an arbitrary string to the class

It also removes a couple of risky stubs from the auth provider test

diff --git a/app/controllers/static_controller.rb b/app/controllers/static_controller.rb
index f86b02b..d20cd7d 100644
--- a/app/controllers/static_controller.rb
+++ b/app/controllers/static_controller.rb
@@ -27,7 +27,7 @@ class StaticController < ApplicationController
 
     if map.has_key?(@page)
       site_setting_key = map[@page][:redirect]
-      url = SiteSetting.send(site_setting_key)
+      url = SiteSetting.get(site_setting_key)
       return redirect_to(url) unless url.blank?
     end
 
diff --git a/app/jobs/onceoff/migrate_url_site_settings.rb b/app/jobs/onceoff/migrate_url_site_settings.rb
index 49b0911..104793f 100644
--- a/app/jobs/onceoff/migrate_url_site_settings.rb
+++ b/app/jobs/onceoff/migrate_url_site_settings.rb
@@ -73,7 +73,7 @@ module Jobs
           for_site_setting: true
         ).create_for(Discourse.system_user.id)
 
-        SiteSetting.public_send("#{new_setting}=", upload)
+        SiteSetting.set(new_setting, upload)
       end
     end
 
diff --git a/app/jobs/regular/enable_bootstrap_mode.rb b/app/jobs/regular/enable_bootstrap_mode.rb
index 32431ea..1bbc2cf 100644
--- a/app/jobs/regular/enable_bootstrap_mode.rb
+++ b/app/jobs/regular/enable_bootstrap_mode.rb
@@ -10,8 +10,8 @@ module Jobs
       return unless user.is_singular_admin?
 
       # let's enable bootstrap mode settings
-      SiteSetting.set_and_log('default_trust_level', TrustLevel[1]) if SiteSetting.send('default_trust_level') == TrustLevel[0]
-      SiteSetting.set_and_log('default_email_digest_frequency', 1440) if SiteSetting.send('default_email_digest_frequency') == 10080
+      SiteSetting.set_and_log('default_trust_level', TrustLevel[1]) if SiteSetting.get('default_trust_level') == TrustLevel[0]
+      SiteSetting.set_and_log('default_email_digest_frequency', 1440) if SiteSetting.get('default_email_digest_frequency') == 10080
       SiteSetting.set_and_log('bootstrap_mode_enabled', true)
     end
   end
diff --git a/app/jobs/scheduled/clean_up_deprecated_url_site_settings.rb b/app/jobs/scheduled/clean_up_deprecated_url_site_settings.rb
index fd12fcb..47b62d6 100644
--- a/app/jobs/scheduled/clean_up_deprecated_url_site_settings.rb
+++ b/app/jobs/scheduled/clean_up_deprecated_url_site_settings.rb
@@ -5,7 +5,7 @@ module Jobs
     def execute(args)
       Jobs::MigrateUrlSiteSettings::SETTINGS.each do |old_setting, new_setting|
         if SiteSetting.where("name = ? AND value IS NOT NULL", new_setting).exists?
-          SiteSetting.public_send("#{old_setting}=", nil, warn: false)
+          SiteSetting.set(old_setting, nil, warn: false)
           SiteSetting.find_by(name: old_setting).destroy!
         end
       end
diff --git a/app/jobs/scheduled/disable_bootstrap_mode.rb b/app/jobs/scheduled/disable_bootstrap_mode.rb
index 108a293..cf84708 100644
--- a/app/jobs/scheduled/disable_bootstrap_mode.rb
+++ b/app/jobs/scheduled/disable_bootstrap_mode.rb
@@ -7,8 +7,8 @@ module Jobs
       total_users = User.human_users.count
 
       if SiteSetting.bootstrap_mode_min_users == 0 || total_users > SiteSetting.bootstrap_mode_min_users
-        SiteSetting.set_and_log('default_trust_level', TrustLevel[0]) if SiteSetting.send('default_trust_level') == TrustLevel[1]
-        SiteSetting.set_and_log('default_email_digest_frequency', 10080) if SiteSetting.send('default_email_digest_frequency') == 1440
+        SiteSetting.set_and_log('default_trust_level', TrustLevel[0]) if SiteSetting.get('default_trust_level') == TrustLevel[1]
+        SiteSetting.set_and_log('default_email_digest_frequency', 10080) if SiteSetting.get('default_email_digest_frequency') == 1440
         SiteSetting.set_and_log('bootstrap_mode_enabled', false)
       end
     end
diff --git a/app/models/category.rb b/app/models/category.rb
index 18a6c18..096ed96 100644
--- a/app/models/category.rb
+++ b/app/models/category.rb
@@ -285,7 +285,7 @@ class Category < ActiveRecord::Base
   def remove_site_settings
     SiteSetting.all_settings.each do |s|
       if s[:type] == 'category' && s[:value].to_i == self.id
-        SiteSetting.send("#{s[:setting]}=", '')
+        SiteSetting.set(s[:setting], '')
       end
     end
 
diff --git a/app/models/embedding.rb b/app/models/embedding.rb
index a0be5ce..3e9b1a3 100644
--- a/app/models/embedding.rb
+++ b/app/models/embedding.rb
@@ -23,7 +23,7 @@ class Embedding < OpenStruct
 
   def save
     Embedding.settings.each do |s|
-      SiteSetting.send("#{s}=", send(s))
+      SiteSetting.set(s, public_send(s))
     end
     true
   rescue Discourse::InvalidParameters => p
diff --git a/app/models/post_action.rb b/app/models/post_action.rb
index 5fd9d50..2d31b64 100644
--- a/app/models/post_action.rb
+++ b/app/models/post_action.rb
@@ -164,10 +164,10 @@ class PostAction < ActiveRecord::Base
 
     %w(like flag bookmark).each do |type|
       if send("is_#{type}?")
-        limit = SiteSetting.send("max_#{type}s_per_day")
+        limit = SiteSetting.get("max_#{type}s_per_day")
 
         if is_like? && user && user.trust_level >= 2
-          multiplier = SiteSetting.send("tl#{user.trust_level}_additional_likes_per_day_multiplier").to_f
+          multiplier = SiteSetting.get("tl#{user.trust_level}_additional_likes_per_day_multiplier").to_f
           multiplier = 1.0 if multiplier < 1.0
 
           limit = (limit * multiplier).to_i
diff --git a/app/models/topic.rb b/app/models/topic.rb
index b8a0b18..02fe6dd 100644
--- a/app/models/topic.rb
+++ b/app/models/topic.rb
@@ -1433,7 +1433,7 @@ class Topic < ActiveRecord::Base
   end
 
   def apply_per_day_rate_limit_for(key, method_name)
-    RateLimiter.new(user, "#{key}-per-day", SiteSetting.send(method_name), 1.day.to_i)
+    RateLimiter.new(user, "#{key}-per-day", SiteSetting.get(method_name), 1.day.to_i)
   end
 
   def create_invite_notification!(target_user, notification_type, username)
diff --git a/app/models/user.rb b/app/models/user.rb
index 79d55de..cde8851 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -1351,7 +1351,7 @@ class User < ActiveRecord::Base
     values = []
 
     %w{watching watching_first_post tracking muted}.each do |s|
-      category_ids = SiteSetting.send("default_categories_#{s}").split("|")
+      category_ids = SiteSetting.get("default_categories_#{s}").split("|")
       category_ids.each do |category_id|
         values << "(#{self.id}, #{category_id}, #{CategoryUser.notification_levels[s.to_sym]})"
       end
diff --git a/app/serializers/auth_provider_serializer.rb b/app/serializers/auth_provider_serializer.rb
index f2bc5cd..82310b7 100644
--- a/app/serializers/auth_provider_serializer.rb
+++ b/app/serializers/auth_provider_serializer.rb
@@ -5,17 +5,17 @@ class AuthProviderSerializer < ApplicationSerializer
              :icon
 
   def title_override
-    return SiteSetting.send(object.title_setting) if object.title_setting
+    return SiteSetting.get(object.title_setting) if object.title_setting
     object.title
   end
 
   def pretty_name_override
-    return SiteSetting.send(object.pretty_name_setting) if object.pretty_name_setting
+    return SiteSetting.get(object.pretty_name_setting) if object.pretty_name_setting
     object.pretty_name
   end
 
   def full_screen_login
-    return SiteSetting.send(object.full_screen_login_setting) if object.full_screen_login_setting
+    return SiteSetting.get(object.full_screen_login_setting) if object.full_screen_login_setting
     return object.full_screen_login if object.full_screen_login
     false
   end
diff --git a/lib/auth/open_id_authenticator.rb b/lib/auth/open_id_authenticator.rb
index ac80b53..7cd54a7 100644
--- a/lib/auth/open_id_authenticator.rb
+++ b/lib/auth/open_id_authenticator.rb
@@ -10,7 +10,7 @@ class Auth::OpenIdAuthenticator < Auth::Authenticator
   end
 
   def enabled?
-    SiteSetting.send(@enabled_site_setting)
+    SiteSetting.get(@enabled_site_setting)
   end
 
   def description_for_user(user)
diff --git a/lib/plugin/instance.rb b/lib/plugin/instance.rb
index 2611f55..28dc167 100644
--- a/lib/plugin/instance.rb
+++ b/lib/plugin/instance.rb

[... diff too long, it was truncated ...]

GitHub sha: 9be70a22

4 Likes