diff --git a/app/serializers/current_user_serializer.rb b/app/serializers/current_user_serializer.rb index 9d5c44743..75824c96a 100644 --- a/app/serializers/current_user_serializer.rb +++ b/app/serializers/current_user_serializer.rb @@ -72,9 +72,20 @@ class CurrentUserSerializer < BasicUserSerializer end def custom_fields - return {} unless SiteSetting.public_user_custom_fields.present? - fields = User.custom_fields_for_ids([object.id], SiteSetting.public_user_custom_fields.split('|')) - return fields.present? ? fields[object.id] : {} + fields = nil + if SiteSetting.public_user_custom_fields.present? + fields = SiteSetting.public_user_custom_fields.split('|') + end + DiscoursePluginRegistry.serialized_current_user_fields.each do |f| + fields ||= [] + fields << f + end + + if fields.present? + User.custom_fields_for_ids([object.id], fields)[object.id] + else + {} + end end end diff --git a/lib/discourse_plugin_registry.rb b/lib/discourse_plugin_registry.rb index bfb8f67de..d85eba6a5 100644 --- a/lib/discourse_plugin_registry.rb +++ b/lib/discourse_plugin_registry.rb @@ -13,6 +13,8 @@ class DiscoursePluginRegistry attr_accessor :sass_variables attr_accessor :handlebars attr_accessor :custom_html + attr_accessor :serialized_current_user_fields + # Default accessor values def javascripts @@ -46,6 +48,10 @@ class DiscoursePluginRegistry def handlebars @handlebars ||= Set.new end + + def serialized_current_user_fields + @serialized_current_user_fields ||= Set.new + end end def register_js(filename, options={}) diff --git a/spec/components/plugin/instance_spec.rb b/spec/components/plugin/instance_spec.rb index bb7de3cac..9e9773345 100644 --- a/spec/components/plugin/instance_spec.rb +++ b/spec/components/plugin/instance_spec.rb @@ -161,6 +161,18 @@ describe Plugin::Instance do end end + context "serialized_current_user_fields" do + it "correctly serializes custom user fields" do + DiscoursePluginRegistry.serialized_current_user_fields << "has_car" + user = Fabricate(:user) + user.custom_fields["has_car"] = "true" + user.save! + + payload = JSON.parse(CurrentUserSerializer.new(user, scope: Guardian.new(user)).to_json) + payload["current_user"]["custom_fields"]["has_car"].should == "true" + end + end + context "register_color_scheme" do it "can add a color scheme for the first time" do plugin = Plugin::Instance.new nil, "/tmp/test.rb"