diff --git a/app/assets/javascripts/discourse/routes/group_route.js b/app/assets/javascripts/discourse/routes/group_route.js index e3dce176088..971c2fe7940 100644 --- a/app/assets/javascripts/discourse/routes/group_route.js +++ b/app/assets/javascripts/discourse/routes/group_route.js @@ -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) { diff --git a/app/controllers/groups_controller.rb b/app/controllers/groups_controller.rb index 02442bf8fb1..ee8552b0982 100644 --- a/app/controllers/groups_controller.rb +++ b/app/controllers/groups_controller.rb @@ -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 diff --git a/spec/controllers/groups_controller_spec.rb b/spec/controllers/groups_controller_spec.rb index 99822c48d6b..bbaa136d201 100644 --- a/spec/controllers/groups_controller_spec.rb +++ b/spec/controllers/groups_controller_spec.rb @@ -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