FEATURE: Support custom preferences for users, injected by plugins

This commit is contained in:
Sam 2014-06-11 15:50:37 +10:00
parent d13d4fc158
commit 03087679f0
7 changed files with 45 additions and 5 deletions

View File

@ -204,7 +204,8 @@ Discourse.User = Discourse.Model.extend({
'external_links_in_new_tab',
'mailing_list_mode',
'enable_quoting',
'disable_jump_reply');
'disable_jump_reply',
'custom_fields');
_.each(['muted','watched','tracked'], function(s){
var cats = user.get(s + 'Categories').map(function(c){ return c.get('id')});

View File

@ -79,7 +79,7 @@ Discourse.TopicRoute = Discourse.Route.extend({
// Use replaceState to update the URL once it changes
postChangedRoute: Discourse.debounce(function(currentPost) {
// do nothing if we are transitioning to another route
if (this.get("isTransitioning")) { return; }
if (this.get("isTransitioning") || Discourse.TopicRoute.disableReplaceState) { return; }
var topic = this.modelFor('topic');
if (topic && currentPost) {

View File

@ -175,6 +175,7 @@
{{preference-checkbox labelKey="user.enable_quoting" checked=enable_quoting}}
{{preference-checkbox labelKey="user.dynamic_favicon" checked=dynamic_favicon}}
{{preference-checkbox labelKey="user.disable_jump_reply" checked=disable_jump_reply}}
{{view Discourse.CustomPreferences}}
</div>
<div class="control-group category">

View File

@ -13,6 +13,9 @@ Discourse.PreferencesView = Discourse.View.extend({
uploading: false,
uploadProgress: 0,
customPreferences: function(){
}.property(),
didInsertElement: function() {
var self = this;
var $upload = $("#profile-background-input");
@ -53,3 +56,29 @@ Discourse.PreferencesView = Discourse.View.extend({
$("#profile-background-input").fileupload("destroy");
}
});
Discourse.PreferencesView.reopenClass({
registerCustomSection: function(viewClass){
var customSections = this.customSections;
if(!customSections){
customSections = Em.A();
this.customSections = customSections;
}
customSections.addObject(viewClass);
}
});
Discourse.CustomPreferences = Discourse.ContainerView.extend({
init: function(){
this._super();
var self = this;
var sections = Discourse.PreferencesView.customSections;
if(sections){
sections.forEach(function(view){
self.pushObject(view.create({user: self.get('controller.model')}));
});
}
}
});

View File

@ -71,7 +71,8 @@ class UserSerializer < BasicUserSerializer
:private_messages_stats,
:disable_jump_reply,
:gravatar_avatar_upload_id,
:custom_avatar_upload_id
:custom_avatar_upload_id,
:custom_fields
def gravatar_avatar_upload_id
object.user_avatar.try(:gravatar_upload_id)

View File

@ -64,6 +64,10 @@ class UserUpdater
user_profile.send("#{attribute.to_s}=", attributes[attribute])
end
if fields = attributes[:custom_fields]
user.custom_fields = fields
end
User.transaction do
user_profile.save
user.save

View File

@ -946,10 +946,14 @@ describe UsersController do
user = Fabricate(:user, name: 'Billy Bob')
log_in_user(user)
put :update, username: user.username, name: 'Jim Tom'
put :update, username: user.username, name: 'Jim Tom', custom_fields: {test: :it}
expect(response).to be_success
expect(user.reload.name).to eq 'Jim Tom'
user.reload
expect(user.name).to eq 'Jim Tom'
expect(user.custom_fields['test']).to eq 'it'
end
it 'returns user JSON' do