FEATURE: support serializing user custom fields by plugins

This commit is contained in:
Sam 2014-06-11 11:57:22 +10:00
parent 1e9f3fa593
commit 62abb873df
3 changed files with 32 additions and 3 deletions

View file

@ -72,9 +72,20 @@ class CurrentUserSerializer < BasicUserSerializer
end end
def custom_fields def custom_fields
return {} unless SiteSetting.public_user_custom_fields.present? fields = nil
fields = User.custom_fields_for_ids([object.id], SiteSetting.public_user_custom_fields.split('|')) if SiteSetting.public_user_custom_fields.present?
return fields.present? ? fields[object.id] : {} 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
end end

View file

@ -13,6 +13,8 @@ class DiscoursePluginRegistry
attr_accessor :sass_variables attr_accessor :sass_variables
attr_accessor :handlebars attr_accessor :handlebars
attr_accessor :custom_html attr_accessor :custom_html
attr_accessor :serialized_current_user_fields
# Default accessor values # Default accessor values
def javascripts def javascripts
@ -46,6 +48,10 @@ class DiscoursePluginRegistry
def handlebars def handlebars
@handlebars ||= Set.new @handlebars ||= Set.new
end end
def serialized_current_user_fields
@serialized_current_user_fields ||= Set.new
end
end end
def register_js(filename, options={}) def register_js(filename, options={})

View file

@ -161,6 +161,18 @@ describe Plugin::Instance do
end end
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 context "register_color_scheme" do
it "can add a color scheme for the first time" do it "can add a color scheme for the first time" do
plugin = Plugin::Instance.new nil, "/tmp/test.rb" plugin = Plugin::Instance.new nil, "/tmp/test.rb"