Convert Discourse.User to use Discourse.Singleton

This commit is contained in:
Robin Ward 2013-08-08 12:42:08 -04:00
parent 8e1fae0459
commit 51f5cf77fb
23 changed files with 90 additions and 57 deletions

View File

@ -12,11 +12,11 @@ Discourse.AdminDashboardController = Ember.Controller.extend({
problemsCheckMinutes: 1,
foundProblems: function() {
return(Discourse.User.current('admin') && this.get('problems') && this.get('problems').length > 0);
return(Discourse.User.currentProp('admin') && this.get('problems') && this.get('problems').length > 0);
}.property('problems'),
thereWereProblems: function() {
if(!Discourse.User.current('admin')) { return false }
if(!Discourse.User.currentProp('admin')) { return false }
if( this.get('foundProblems') ) {
this.set('hadProblems', true);
return true;

View File

@ -62,7 +62,7 @@ Discourse.FlaggedPost = Discourse.Post.extend({
}.property('post_actions.@each.name_key'),
canDeleteAsSpammer: function() {
return (Discourse.User.current('staff') && this.get('flaggedForSpam') && this.get('user.can_delete_all_posts') && this.get('user.can_be_deleted'));
return (Discourse.User.currentProp('staff') && this.get('flaggedForSpam') && this.get('user.can_delete_all_posts') && this.get('user.can_be_deleted'));
}.property('flaggedForSpam'),
deletePost: function() {

View File

@ -38,7 +38,7 @@ Discourse = Ember.Application.createWithMixins(Discourse.Ajax, {
$('title').text(title);
var notifyCount = this.get('notifyCount');
if (notifyCount > 0 && !Discourse.User.current('dynamic_favicon')) {
if (notifyCount > 0 && !Discourse.User.currentProp('dynamic_favicon')) {
title = "(" + notifyCount + ") " + title;
}
// chrome bug workaround see: http://stackoverflow.com/questions/2952384/changing-the-window-title-when-focussing-the-window-doesnt-work-in-chrome
@ -49,7 +49,7 @@ Discourse = Ember.Application.createWithMixins(Discourse.Ajax, {
}.observes('title', 'hasFocus', 'notifyCount'),
faviconChanged: function() {
if(Discourse.User.current('dynamic_favicon')) {
if(Discourse.User.currentProp('dynamic_favicon')) {
$.faviconNotify(
Discourse.SiteSettings.favicon_url, this.get('notifyCount')
);

View File

@ -38,7 +38,7 @@ Discourse.ClickTrack = {
userId = $link.data('user-id');
if (!userId) userId = $article.data('user-id');
var ownLink = userId && (userId === Discourse.User.current('id'));
var ownLink = userId && (userId === Discourse.User.currentProp('id'));
// Build a Redirect URL
var trackingUrl = Discourse.getURL("/clicks/track?url=" + encodeURIComponent(href));
@ -99,7 +99,7 @@ Discourse.ClickTrack = {
}
// Otherwise, use a custom URL with a redirect
if (Discourse.User.current('external_links_in_new_tab')) {
if (Discourse.User.currentProp('external_links_in_new_tab')) {
var win = window.open(trackingUrl, '_blank');
win.focus();
} else {

View File

@ -202,7 +202,7 @@ Discourse.Utilities = {
}
// ensures that new users can upload a file
if (Discourse.User.current('trust_level') === 0 && Discourse.SiteSettings['newuser_max_' + type + 's'] === 0) {
if (Discourse.User.currentProp('trust_level') === 0 && Discourse.SiteSettings['newuser_max_' + type + 's'] === 0) {
bootbox.alert(I18n.t('post.errors.' + type + '_upload_not_allowed_for_new_user'));
return false;
}

View File

@ -140,7 +140,7 @@ Discourse.ComposerController = Discourse.Controller.extend({
if (this.get('model.editingPost')) return;
// If creating a topic, use topic_count, otherwise post_count
var count = this.get('model.creatingTopic') ? Discourse.User.current('topic_count') : Discourse.User.current('reply_count');
var count = this.get('model.creatingTopic') ? Discourse.User.currentProp('topic_count') : Discourse.User.currentProp('reply_count');
if (count >= Discourse.SiteSettings.educate_until_posts) {
this.set('educationClosed', true);
this.set('educationContents', '');

View File

@ -35,7 +35,7 @@ Discourse.FlagController = Discourse.ObjectController.extend(Discourse.ModalFunc
canTakeAction: function() {
// We can only take actions on non-custom flags
if (this.get('selected.is_custom_flag')) return false;
return Discourse.User.current('staff');
return Discourse.User.currentProp('staff');
}.property('selected.is_custom_flag'),
submitText: function(){
@ -66,7 +66,7 @@ Discourse.FlagController = Discourse.ObjectController.extend(Discourse.ModalFunc
},
canDeleteSpammer: function() {
if (Discourse.User.current('staff') && this.get('selected.name_key') === 'spam') {
if (Discourse.User.currentProp('staff') && this.get('selected.name_key') === 'spam') {
return this.get('userDetails.can_be_deleted') && this.get('userDetails.can_delete_all_posts');
} else {
return false;
@ -84,7 +84,7 @@ Discourse.FlagController = Discourse.ObjectController.extend(Discourse.ModalFunc
}.observes('username'),
fetchUserDetails: function() {
if( Discourse.User.current('staff') && this.get('username') ) {
if( Discourse.User.currentProp('staff') && this.get('username') ) {
var flagController = this;
Discourse.AdminUser.find(this.get('username').toLowerCase()).then(function(user){
flagController.set('userDetails', user);

View File

@ -25,7 +25,7 @@ Discourse.ListCategoriesController = Discourse.ObjectController.extend({
}.property('categories.@each'),
canEdit: function() {
return Discourse.User.current('staff');
return Discourse.User.currentProp('staff');
}.property(),
// clear a pinned topic

View File

@ -47,8 +47,8 @@ Discourse.PreferencesController = Discourse.ObjectController.extend({
return model.save().then(function() {
// model was saved
preferencesController.set('saving', false);
if (Discourse.User.current('id') === model.get('id')) {
Discourse.User.current().set('name', model.get('name'));
if (Discourse.User.currentProp('id') === model.get('id')) {
Discourse.User.currentProp('name', model.get('name'));
}
preferencesController.set('bio_cooked',

View File

@ -9,11 +9,11 @@
Discourse.UserController = Discourse.ObjectController.extend({
viewingSelf: function() {
return this.get('content.username') === Discourse.User.current('username');
return this.get('content.username') === Discourse.User.currentProp('username');
}.property('content.username'),
canSeePrivateMessages: function() {
return this.get('viewingSelf') || Discourse.User.current('staff');
return this.get('viewingSelf') || Discourse.User.currentProp('staff');
}.property('viewingSelf')
});

View File

@ -17,12 +17,26 @@ Discourse.Singleton = Em.Mixin.create({
**/
current: function() {
if (!this._current) {
this._current = this.create({});
this._current = this.createCurrent();
}
return this._current;
},
/**
How the singleton instance is created. This can be overridden
with logic for creating (or even returning null) your instance.
By default it just calls `create` with an empty object.
@method createCurrent
@returns {Ember.Object} the instance that will be your singleton
**/
createCurrent: function() {
return this.create({});
},
/**
Returns or sets a property on the singleton instance.
@ -32,11 +46,14 @@ Discourse.Singleton = Em.Mixin.create({
@returns the value of the property
**/
currentProp: function(property, value) {
var instance = this.current();
if (!instance) { return; }
if (typeof(value) !== "undefined") {
this.current().set(property, value);
instance.set(property, value);
return value;
} else {
return this.current().get(property);
return instance.get(property);
}
}

View File

@ -120,7 +120,7 @@ Discourse.ActionSummary = Discourse.Model.extend({
var users = Em.A();
actionSummary.set('users', users);
_.each(result,function(user) {
if (user.id === Discourse.User.current('id')) {
if (user.id === Discourse.User.currentProp('id')) {
users.pushObject(Discourse.User.current());
} else {
users.pushObject(Discourse.User.create(user));

View File

@ -48,7 +48,7 @@ Discourse.Composer = Discourse.Model.extend({
canCategorize: Em.computed.and('canEditTitle', 'notCreatingPrivateMessage'),
showAdminOptions: function() {
if (this.get('creatingTopic') && Discourse.User.current('staff')) return true;
if (this.get('creatingTopic') && Discourse.User.currentProp('staff')) return true;
return false;
}.property('canEditTitle'),
@ -551,7 +551,7 @@ Discourse.Composer = Discourse.Model.extend({
flashDraftStatusForNewUser: function() {
var $draftStatus = $('#draft-status');
if (Discourse.User.current('trust_level') === 0) {
if (Discourse.User.currentProp('trust_level') === 0) {
$draftStatus.toggleClass('flash', true);
setTimeout(function() { $draftStatus.removeClass('flash'); }, 250);
}

View File

@ -38,7 +38,7 @@ Discourse.TopicDetails = Discourse.Model.extend({
if (typeof this.get('notifications_reason_id') === 'number') {
locale_string += "_" + this.get('notifications_reason_id');
}
return I18n.t(locale_string, { username: Discourse.User.current('username_lower') });
return I18n.t(locale_string, { username: Discourse.User.currentProp('username_lower') });
}.property('notification_level', 'notifications_reason_id'),

View File

@ -283,29 +283,20 @@ Discourse.User = Discourse.Model.extend({
});
Discourse.User.reopenClass({
Discourse.User.reopenClass(Discourse.Singleton, {
/**
Returns the currently logged in user
The current singleton will retrieve its attributes from the `PreloadStore`
if it exists. Otherwise, no instance is created.
@method current
@param {String} optional property to return from the user if the user exists
@returns {Discourse.User} the logged in user
@method createCurrent
@returns {Discourse.User} the user, if logged in.
**/
current: function(property) {
if (!this.currentUser) {
var userJson = PreloadStore.get('currentUser');
if (userJson) {
this.currentUser = Discourse.User.create(userJson);
}
}
// If we found the current user
if (this.currentUser && (typeof property !== "undefined")) {
return this.currentUser.get(property);
}
return this.currentUser;
createCurrent: function() {
var userJson = PreloadStore.get('currentUser');
if (userJson) { return Discourse.User.create(userJson); }
return null;
},
/**
@ -316,7 +307,7 @@ Discourse.User.reopenClass({
**/
logout: function() {
var discourseUserClass = this;
return Discourse.ajax("/session/" + Discourse.User.current('username'), {
return Discourse.ajax("/session/" + Discourse.User.currentProp('username'), {
type: 'DELETE'
}).then(function () {
discourseUserClass.currentUser = null;

View File

@ -91,11 +91,11 @@ Discourse.UserAction = Discourse.Model.extend({
}.property('descriptionKey'),
sameUser: function() {
return this.get('username') === Discourse.User.current('username');
return this.get('username') === Discourse.User.currentProp('username');
}.property('username'),
targetUser: function() {
return this.get('target_username') === Discourse.User.current('username');
return this.get('target_username') === Discourse.User.currentProp('username');
}.property('target_username'),
targetUserUrl: Discourse.computed.url('target_username', '/users/%@'),

View File

@ -48,7 +48,7 @@ Discourse.QuoteButtonView = Discourse.View.extend({
view.set('isMouseDown', true);
if ($(e.target).hasClass('quote-button') || $(e.target).hasClass('create')) return;
// do *not* deselect when quoting has been disabled by the user
if (!Discourse.User.current('enable_quoting')) return;
if (!Discourse.User.currentProp('enable_quoting')) return;
// deselects only when the user left click
// (allows anyone to `extend` their selection using shift+click)
if (e.which === 1 && !e.shiftKey) controller.deselectText();
@ -80,7 +80,7 @@ Discourse.QuoteButtonView = Discourse.View.extend({
selectText: function(target, controller) {
var $target = $(target);
// breaks if quoting has been disabled by the user
if (!Discourse.User.current('enable_quoting')) return;
if (!Discourse.User.currentProp('enable_quoting')) return;
// retrieve the post id from the DOM
var postId = $target.closest('.boxed').data('post-id');
// select the text

View File

@ -15,7 +15,7 @@ Discourse.UserSelector = Discourse.TextField.extend({
dataSource: function(term) {
var exclude = selected;
if (userSelectorView.get('excludeCurrentUser')){
exclude = exclude.concat([Discourse.User.current('username')]);
exclude = exclude.concat([Discourse.User.currentProp('username')]);
}
return Discourse.UserSearch.search({
term: term,

View File

@ -85,12 +85,12 @@ var badgeClickCount = function(id, expected) {
};
test("does not update badge clicks on my own link", function() {
this.stub(Discourse.User, 'current').returns(314);
this.stub(Discourse.User, 'currentProp').withArgs('id').returns(314);
badgeClickCount('with-badge', 1);
});
test("does not update badge clicks in my own post", function() {
this.stub(Discourse.User, 'current').returns(3141);
this.stub(Discourse.User, 'currentProp').withArgs('id').returns(3141);
badgeClickCount('with-badge-but-not-mine', 1);
});
@ -167,7 +167,7 @@ test("does not track via AJAX for attachments", function() {
test("tracks custom urls when opening in another window", function() {
var clickEvent = generateClickEventOn('a');
this.stub(Discourse.User, "current").returns(true);
this.stub(Discourse.User, "currentProp").withArgs('external_links_in_new_tab').returns(true);
ok(!track(clickEvent));
ok(this.windowOpen.calledWith('/clicks/track?url=http%3A%2F%2Fwww.google.com&post_id=42', '_blank'));
});

View File

@ -24,7 +24,7 @@ test("uploading one file", function() {
test("new user cannot upload images", function() {
Discourse.SiteSettings.newuser_max_images = 0;
this.stub(Discourse.User, 'current').withArgs("trust_level").returns(0);
this.stub(Discourse.User, 'currentProp').withArgs("trust_level").returns(0);
this.stub(bootbox, "alert");
ok(!validUpload([{name: "image.png"}]));
@ -33,7 +33,7 @@ test("new user cannot upload images", function() {
test("new user cannot upload attachments", function() {
Discourse.SiteSettings.newuser_max_attachments = 0;
this.stub(Discourse.User, 'current').withArgs("trust_level").returns(0);
this.stub(Discourse.User, 'currentProp').withArgs("trust_level").returns(0);
this.stub(bootbox, "alert");
ok(!validUpload([{name: "roman.txt"}]));

View File

@ -17,7 +17,7 @@ module("Discourse.FlagController canDeleteSpammer");
test("canDeleteSpammer not staff", function(){
var flagController = controllerFor('flag', buildPost());
this.stub(Discourse.User, 'current').returns(false); // Discourse.User.current('staff') returns false
this.stub(Discourse.User, 'currentProp').withArgs('staff').returns(false);
flagController.set('selected', Discourse.PostActionType.create({name_key: 'spam'}));
equal(flagController.get('canDeleteSpammer'), false, 'false if current user is not staff');
});
@ -28,7 +28,7 @@ var canDeleteSpammer = function(test, postActionType, expected, testName) {
};
test("canDeleteSpammer spam not selected", function(){
this.stub(Discourse.User, 'current').returns(true); // Discourse.User.current('staff') returns true
this.stub(Discourse.User, 'currentProp').withArgs('staff').returns(true);
this.flagController = controllerFor('flag', buildPost());
this.flagController.set('userDetails', buildAdminUser({can_delete_all_posts: true, can_be_deleted: true}));
canDeleteSpammer(this, 'off_topic', false, 'false if current user is staff, but selected is off_topic');
@ -38,7 +38,7 @@ test("canDeleteSpammer spam not selected", function(){
});
test("canDeleteSpammer spam selected", function(){
this.stub(Discourse.User, 'current').returns(true);
this.stub(Discourse.User, 'currentProp').withArgs('staff').returns(true);
this.flagController = controllerFor('flag', buildPost());
this.flagController.set('userDetails', buildAdminUser({can_delete_all_posts: true, can_be_deleted: true}));

View File

@ -124,6 +124,7 @@ var jsHintOpts = {
"controllerFor",
"containsInstance",
"deepEqual",
"notEqual",
"Blob",
"File"],
"node" : false,

View File

@ -34,4 +34,28 @@ test("currentProp writing", function() {
DummyModel.currentProp('adventure', null);
equal(DummyModel.currentProp('adventure'), null, 'we can set the value to null');
});
test("createCurrent", function() {
var Shoe = Ember.Object.extend({});
Shoe.reopenClass(Discourse.Singleton, {
createCurrent: function() {
return Shoe.create({toes: 5});
}
});
equal(Shoe.currentProp('toes'), 5, 'it created the class using `createCurrent`');
});
test("createCurrent that returns null", function() {
var Missing = Ember.Object.extend({});
Missing.reopenClass(Discourse.Singleton, {
createCurrent: function() {
return null;
}
});
blank(Missing.current(), "it doesn't return an instance");
blank(Missing.currentProp('madeup'), "it won't raise an error asking for a property. Will just return null.");
});