Skip to content

Commit 6b536dc

Browse files
committed
work in progress ... groups
1 parent 7571152 commit 6b536dc

16 files changed

Lines changed: 189 additions & 21 deletions

File tree

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11
Discourse.AdminGroupsController = Ember.ArrayController.extend({
22
itemController: 'adminGroup',
3-
edit: function(action){
4-
this.get('content').select(action);
3+
4+
edit: function(group){
5+
this.get('model').select(group);
6+
group.loadUsers();
7+
},
8+
9+
refreshAutoGroups: function(){
10+
var controller = this;
11+
12+
this.set('refreshingAutoGroups', true);
13+
Discourse.ajax('/admin/groups/refresh_automatic_groups', {type: 'POST'}).then(function(){
14+
controller.set('model', Discourse.Group.findAll());
15+
controller.set('refreshingAutoGroups',false);
16+
});
517
}
618
});
719

8-
Discourse.AdminGroupController = Ember.ObjectController.extend({
20+
Discourse.AdminGroupController = Ember.Controller.extend({
921

1022
});
Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,57 @@
11
Discourse.Group = Discourse.Model.extend({
2+
userCountDisplay: function(){
3+
var c = this.get('user_count');
4+
// don't display zero its ugly
5+
if(c > 0) {
6+
return c;
7+
}
8+
}.property('user_count'),
9+
10+
loadUsers: function() {
11+
var group = this;
12+
13+
Discourse.ajax('/admin/groups/' + this.get('id') + '/users').then(function(payload){
14+
var users = Em.A()
15+
payload.each(function(user){
16+
users.addObject(Discourse.User.create(user));
17+
});
18+
group.set('users', users)
19+
});
20+
},
21+
22+
usernames: function() {
23+
var users = this.get('users');
24+
var usernames = "";
25+
if(users) {
26+
usernames = $.map(users, function(user){
27+
return user.get('username');
28+
}).join(',')
29+
}
30+
return usernames;
31+
}.property('users')
232

333
});
434

535
Discourse.Group.reopenClass({
636
findAll: function(){
737
var list = Discourse.SelectableArray.create();
838

9-
list.addObject(Discourse.Group.create({id: 1, name: "all mods", members: ["A","b","c"]}));
10-
list.addObject(Discourse.Group.create({id: 2, name: "other mods", members: ["A","b","c"]}));
39+
Discourse.ajax("/admin/groups").then(function(groups){
40+
groups.each(function(group){
41+
list.addObject(Discourse.Group.create(group));
42+
});
43+
});
1144

1245
return list;
1346
},
1447

1548
find: function(id) {
1649
var promise = new Em.Deferred();
17-
50+
1851
setTimeout(function(){
1952
promise.resolve(Discourse.Group.create({id: 1, name: "all mods", members: ["A","b","c"]}));
2053
}, 1000);
21-
54+
2255
return promise;
2356
}
2457
});
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
Discourse.AdminGroupsRoute = Discourse.Route.extend({
2-
model: function() {
3-
return Discourse.Group.findAll();
4-
},
52
renderTemplate: function() {
63
this.render('admin/templates/groups',{into: 'admin/templates/admin'});
4+
},
5+
6+
setupController: function(controller, model) {
7+
controller.set('model', Discourse.Group.findAll());
78
}
89
});
910

app/assets/javascripts/admin/templates/groups.js.handlebars

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,23 @@
33
<div class='content-list span6'>
44
<h3>{{i18n admin.groups.edit}}</h3>
55
<ul>
6-
{{#each group in controller}}
6+
{{#each group in model}}
77
<li>
8-
<a href="#" {{action "edit" group}} {{bindAttr class="group.active"}}>{{group.name}}</a>
8+
<a href="#" {{action "edit" group}} {{bindAttr class="group.active"}}>{{group.name}} <span class="count">{{group.userCountDisplay}}</span></a>
99
</li>
1010
{{/each}}
1111
</ul>
12+
<div>
13+
<button {{bindAttr disabled="refreshingAutoGroups"}} {{action "refreshAutoGroups"}}>Refresh Automatic Groups</button>
14+
</div>
1215
</div>
1316

1417
<div class='content-editor'>
15-
{{#if content.active}}
16-
{{#with content.active}}
17-
<h3>{{name}}</h3>
18+
{{#if model.active}}
19+
{{#with model.active}}
20+
<h3>{{name}}</h3>
1821
{{view Discourse.UserSelector id="private-message-users" class="span8" placeholderKey="admin.groups.selector_placeholder" tabindex="1" usernamesBinding="usernames"}}
19-
<button>Save</button>
22+
<button {{bindAttr disabled="allowSave"}}>Save</button>
2023

2124
{{/with}}
2225
{{else}}

app/assets/javascripts/discourse/models/selectable_array.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// this allows you to track the selected item in an array, ghetto for now
22
Discourse.SelectableArray = Em.ArrayProxy.extend({
3-
content: [],
3+
init: function() {
4+
this.content = [];
5+
this._super();
6+
},
47
selectIndex: function(index){
58
this.select(this[index]);
69
},

app/assets/stylesheets/admin/admin_base.scss

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@
33
@import "foundation/mixins";
44
@import "foundation/helpers";
55

6+
7+
.content-list li a span.count {
8+
font-size: 12px;
9+
float: right;
10+
margin-right: 10px;
11+
background-color: #eee;
12+
padding: 2px 5px;
13+
border-radius: 5px;
14+
color: #555;
15+
}
16+
617
.admin-content {
718
margin-bottom: 50px;
819
.admin-contents {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
11
class Admin::GroupsController < Admin::AdminController
2+
def index
3+
groups = Group.order(:name).all
4+
render_serialized(groups, AdminGroupSerializer)
5+
end
6+
7+
def refresh_automatic_groups
8+
Group.refresh_automatic_groups!
9+
render json: "ok"
10+
end
11+
212
def show
313
end
14+
15+
def users
16+
group = Group.find(params[:group_id].to_i)
17+
render_serialized(group.users, BasicUserSerializer)
18+
end
419
end

app/models/group.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ def self.refresh_automatic_group!(name)
2525
id = AUTO_GROUPS[name]
2626

2727
unless group = self[name]
28-
group = Group.new(name: name.to_s, automatic: true)
28+
group = Group.new(name: "", automatic: true)
2929
group.id = id
3030
group.save!
3131
end
3232

33+
group.name = I18n.t("groups.default_names.#{name}")
3334

3435
real_ids = case name
3536
when :admins
@@ -55,9 +56,15 @@ def self.refresh_automatic_group!(name)
5556
end
5657

5758
group.save!
59+
60+
# we want to ensure consistency
61+
Group.reset_counters(group.id, :group_users)
5862
end
5963

6064
def self.refresh_automatic_groups!(*args)
65+
if args.length == 0
66+
args = AUTO_GROUPS.map{|k,v| k}
67+
end
6168
args.each do |group|
6269
refresh_automatic_group!(group)
6370
end

app/models/group_user.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
class GroupUser < ActiveRecord::Base
2-
belongs_to :group
2+
belongs_to :group, counter_cache: "user_count"
33
belongs_to :user
44
end
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class AdminGroupSerializer < ApplicationSerializer
2+
attributes :id, :automatic, :name, :user_count
3+
end

0 commit comments

Comments
 (0)