From 92cb52e76ef299caada71caec76983d1f8669954 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Tue, 14 Jan 2014 19:18:04 +0100 Subject: [PATCH] BUGFIX: groups page wasn't working --- app/assets/javascripts/admin/models/group.js | 7 +- .../admin/templates/groups.js.handlebars | 5 +- .../javascripts/discourse/lib/autocomplete.js | 27 +++---- .../discourse/views/user_selector_view.js | 73 ++++++++++--------- app/models/group.rb | 10 +-- 5 files changed, 57 insertions(+), 65 deletions(-) diff --git a/app/assets/javascripts/admin/models/group.js b/app/assets/javascripts/admin/models/group.js index cc13392fd..fc979115c 100644 --- a/app/assets/javascripts/admin/models/group.js +++ b/app/assets/javascripts/admin/models/group.js @@ -106,11 +106,10 @@ Discourse.Group = Discourse.Model.extend({ alias_level: this.get('alias_level'), usernames: this.get('usernames') } - }, - complete: function(){ - group.set('disableSave', false); } - }).then(null, function(e){ + }).then(function(){ + group.set('disableSave', false); + }, function(e){ var message = $.parseJSON(e.responseText).errors; bootbox.alert(message); }); diff --git a/app/assets/javascripts/admin/templates/groups.js.handlebars b/app/assets/javascripts/admin/templates/groups.js.handlebars index 79b3c6610..01857562f 100644 --- a/app/assets/javascripts/admin/templates/groups.js.handlebars +++ b/app/assets/javascripts/admin/templates/groups.js.handlebars @@ -4,7 +4,7 @@ @@ -25,8 +25,7 @@ {{/if}}
- +
{{userSelector usernames=usernames id="group-users" placeholderKey="admin.groups.selector_placeholder" tabindex="1" disabledBinding="automatic"}}
diff --git a/app/assets/javascripts/discourse/lib/autocomplete.js b/app/assets/javascripts/discourse/lib/autocomplete.js index 98fe2641c..fa2d085c9 100644 --- a/app/assets/javascripts/discourse/lib/autocomplete.js +++ b/app/assets/javascripts/discourse/lib/autocomplete.js @@ -47,7 +47,6 @@ function mapKeyPressToActualCharacter(isShiftKey, characterCode) { } $.fn.autocomplete = function(options) { - var autocompletePlugin = this; if (this.length === 0) return; @@ -85,18 +84,14 @@ $.fn.autocomplete = function(options) { var addInputSelectedItem = function(item) { var transformed; - if (options.transformComplete) { - transformed = options.transformComplete(item); - } - if (options.single){ - // dump what we have in single mode, just in case - inputSelectedItems = []; - } - if (!_.isArray(transformed)) { - transformed = [transformed || item]; - } + + if (options.transformComplete) { transformed = options.transformComplete(item); } + // dump what we have in single mode, just in case + if (options.single) { inputSelectedItems = []; } + if (!_.isArray(transformed)) { transformed = [transformed || item]; } + var divs = transformed.map(function(itm) { - var d = $("
" + (itm) + "
"); + var d = $("
" + itm + "
"); var prev = me.parent().find('.item:last'); if (prev.length === 0) { me.parent().prepend(d); @@ -104,12 +99,10 @@ $.fn.autocomplete = function(options) { prev.after(d); } inputSelectedItems.push(itm); - return divs; + return d[0]; }); - - if (options.onChangeItems) { - options.onChangeItems(inputSelectedItems); - } + + if (options.onChangeItems) { options.onChangeItems(inputSelectedItems); } $(divs).find('a').click(function() { closeAutocomplete(); diff --git a/app/assets/javascripts/discourse/views/user_selector_view.js b/app/assets/javascripts/discourse/views/user_selector_view.js index f3ce8a7f0..28e67650a 100644 --- a/app/assets/javascripts/discourse/views/user_selector_view.js +++ b/app/assets/javascripts/discourse/views/user_selector_view.js @@ -1,9 +1,8 @@ Discourse.UserSelector = Discourse.TextField.extend({ - didInsertElement: function(){ - - var userSelectorView = this; - var selected = []; + didInsertElement: function() { + var userSelectorView = this, + selected = []; $(this.get('element')).val(this.get('usernames')).autocomplete({ template: Discourse.UserSelector.templateFunction(), @@ -11,9 +10,10 @@ Discourse.UserSelector = Discourse.TextField.extend({ disabled: this.get('disabled'), single: this.get('single'), allowAny: this.get('allowAny'), + dataSource: function(term) { var exclude = selected; - if (userSelectorView.get('excludeCurrentUser')){ + if (userSelectorView.get('excludeCurrentUser')) { exclude = exclude.concat([Discourse.User.currentProp('username')]); } return Discourse.UserSearch.search({ @@ -23,13 +23,14 @@ Discourse.UserSelector = Discourse.TextField.extend({ include_groups: userSelectorView.get('include_groups') }); }, + transformComplete: function(v) { - if (v.username) { - return v.username; - } else { - return v.usernames; - } - }, + if (v.username) { + return v.username; + } else { + return v.usernames; + } + }, onChangeItems: function(items) { items = _.map(items, function(i) { @@ -52,41 +53,41 @@ Discourse.UserSelector = Discourse.TextField.extend({ }); -Handlebars.registerHelper("showMax", function(context, block){ +Handlebars.registerHelper("showMax", function(context, block) { var maxLength = parseInt(block.hash.max) || 3; if (context.length > maxLength){ return context.slice(0, maxLength).join(", ") + ", +" + (context.length - maxLength); } else { return context.join(", "); } - }); - Discourse.UserSelector.reopenClass({ // I really want to move this into a template file, but I need a handlebars template here, not an ember one - templateFunction: function(){ - this.compiled = this.compiled || Handlebars.compile("
" + - "" + - "
"); + templateFunction: function() { + this.compiled = this.compiled || Handlebars.compile( + "
" + + "" + + "
"); return this.compiled; } }); diff --git a/app/models/group.rb b/app/models/group.rb index fb28a5bac..848b1a4b8 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -110,8 +110,8 @@ class Group < ActiveRecord::Base end def self.search_group(name, current_user) - levels = [ALIAS_LEVELS[:everyone]] + if current_user.admin? levels = [ALIAS_LEVELS[:everyone], ALIAS_LEVELS[:only_admins], @@ -123,11 +123,11 @@ class Group < ActiveRecord::Base ALIAS_LEVELS[:members_mods_and_admins]] end - return Group.where("name LIKE :term_like AND (" + - " alias_level in (:levels)" + - " OR (alias_level = #{ALIAS_LEVELS[:everyone]} AND id in (" + + Group.where("name LIKE :term_like AND (" + + " alias_level in (:levels)" + + " OR (alias_level = #{ALIAS_LEVELS[:everyone]} AND id in (" + "SELECT group_id FROM group_users WHERE user_id= :user_id)" + - ")" + + ")" + ")", term_like: "#{name.downcase}%", levels: levels, user_id: current_user.id) end