mirror of
https://github.com/codeninjasllc/discourse.git
synced 2024-11-27 09:36:19 -05:00
FEATURE: support serializing user custom fields by plugins
This commit is contained in:
parent
1e9f3fa593
commit
62abb873df
3 changed files with 32 additions and 3 deletions
|
@ -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
|
||||
|
|
|
@ -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={})
|
||||
|
|
|
@ -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"
|
||||
|
|
Loading…
Reference in a new issue