Use lower case group names in URLs

This commit is contained in:
Robin Ward 2014-02-18 16:43:02 -05:00
parent 43612e9fde
commit 18e98851e3
3 changed files with 24 additions and 9 deletions

View File

@ -12,6 +12,10 @@ Discourse.GroupRoute = Discourse.Route.extend({
return Discourse.Group.find(params.name);
},
serialize: function(model) {
return { name: model.get('name').toLowerCase() };
},
afterModel: function(model) {
var self = this;
return Discourse.Group.findGroupCounts(model.get('name')).then(function (counts) {

View File

@ -1,29 +1,33 @@
class GroupsController < ApplicationController
def show
group = Group.where(name: params.require(:id)).first
guardian.ensure_can_see!(group)
render_serialized(group, BasicGroupSerializer)
render_serialized(find_group(:id), BasicGroupSerializer)
end
def counts
group = Group.where(name: params.require(:group_id)).first
guardian.ensure_can_see!(group)
group = find_group(:group_id)
render json: {counts: { posts: group.posts_for(guardian).count,
members: group.users.count } }
end
def posts
group = Group.where(name: params.require(:group_id)).first
guardian.ensure_can_see!(group)
group = find_group(:group_id)
posts = group.posts_for(guardian, params[:before_post_id]).limit(20)
render_serialized posts.to_a, GroupPostSerializer
end
def members
group = Group.where(name: params.require(:group_id)).first
guardian.ensure_can_see!(group)
group = find_group(:group_id)
render_serialized(group.users.order('username_lower asc').limit(200).to_a, GroupUserSerializer)
end
private
def find_group(param_name)
name = params.require(param_name)
group = Group.where("lower(name) = ?", name.downcase).first
guardian.ensure_can_see!(group)
group
end
end

View File

@ -16,6 +16,13 @@ describe GroupsController do
response.should be_success
::JSON.parse(response.body)['basic_group']['id'].should == group.id
end
it "works even with an upper case group name" do
Guardian.any_instance.expects(:can_see?).with(group).returns(true)
xhr :get, :show, id: group.name.upcase
response.should be_success
::JSON.parse(response.body)['basic_group']['id'].should == group.id
end
end
describe "counts" do