Merge pull request #4686 from tgxworld/group_is_visible_if_user_is_group_owner

FIX: Show groups that user is owner of on groups page.
This commit is contained in:
Jeff Atwood 2017-02-11 22:18:44 -08:00 committed by GitHub
commit 3ee7a9266c
3 changed files with 57 additions and 6 deletions

View File

@ -18,12 +18,7 @@ class GroupsController < ApplicationController
page_size = 30
page = params[:page]&.to_i || 0
groups = Group.order(name: :asc).where(visible: true)
if !guardian.is_admin?
groups = groups.where(automatic: false)
end
groups = Group.visible_groups(current_user)
count = groups.count
groups = groups.offset(page * page_size).limit(page_size)

View File

@ -63,6 +63,20 @@ class Group < ActiveRecord::Base
validates :alias_level, inclusion: { in: ALIAS_LEVELS.values}
scope :visible_groups, ->(user) {
groups = Group.order(name: :asc).where("groups.id > 0")
if !user || !user.admin
owner_group_ids = GroupUser.where(user: user, owner: true).pluck(:group_id)
groups = groups.where("
(groups.automatic = false AND groups.visible = true) OR groups.id IN (?)
", owner_group_ids)
end
groups
}
scope :mentionable, lambda {|user|
levels = [ALIAS_LEVELS[:everyone]]

View File

@ -1,6 +1,8 @@
require 'rails_helper'
describe Group do
let(:admin) { Fabricate(:admin) }
let(:user) { Fabricate(:user) }
describe '#builtin' do
context "verify enum sequence" do
@ -408,4 +410,44 @@ describe Group do
expect(group.bio_cooked).to include("unicorn.png")
end
describe ".visible_groups" do
let(:group) { Fabricate(:group, visible: false) }
let(:group_2) { Fabricate(:group, visible: true) }
let(:admin) { Fabricate(:admin) }
let(:user) { Fabricate(:user) }
before do
group
group_2
end
describe 'when user is an admin' do
it 'should return the right groups' do
expect(Group.visible_groups(admin).pluck(:id).sort)
.to eq([group.id, group_2.id].concat(Group::AUTO_GROUP_IDS.keys - [0]).sort)
end
end
describe 'when user is owner of a group' do
it 'should return the right groups' do
group.add_owner(user)
expect(Group.visible_groups(user).pluck(:id).sort)
.to eq([group.id, group_2.id])
end
end
describe 'when user is not the owner of any group' do
it 'should return the right groups' do
expect(Group.visible_groups(user).pluck(:id).sort)
.to eq([group_2.id])
end
end
describe 'user is nil' do
it 'should return the right groups' do
expect(Group.visible_groups(nil).pluck(:id).sort).to eq([group_2.id])
end
end
end
end