diff --git a/app/models/group.rb b/app/models/group.rb index ed993001bb0..0d24e7a814c 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -686,7 +686,7 @@ class Group < ActiveRecord::Base def self.lookup_groups(group_ids: [], group_names: []) if group_ids.present? - group_ids = group_ids.split(",") if group_ids.is_a?(String) + group_ids = group_ids.to_s.split(",") if !group_ids.is_a?(Array) group_ids.map!(&:to_i) groups = Group.where(id: group_ids) if group_ids.present? end diff --git a/spec/models/group_spec.rb b/spec/models/group_spec.rb index d2f9ad1f0af..7a32c369e09 100644 --- a/spec/models/group_spec.rb +++ b/spec/models/group_spec.rb @@ -579,6 +579,23 @@ RSpec.describe Group do expect(group.id).to eq Group[group.name.to_sym].id end + it "allows you to lookup a group by integer id" do + group = Fabricate(:group) + expect(group.id).to eq Group.lookup_groups(group_ids: group.id).first.id + end + + it "allows you to lookup groups by comma separated string" do + group1 = Fabricate(:group) + group2 = Fabricate(:group) + expect([group1, group2]).to eq Group.lookup_groups(group_ids: "#{group1.id},#{group2.id}") + end + + it "allows you to lookup groups by array" do + group1 = Fabricate(:group) + group2 = Fabricate(:group) + expect([group1, group2]).to eq Group.lookup_groups(group_ids: [group1.id, group2.id]) + end + it "can find desired groups correctly" do expect(Group.desired_trust_level_groups(2).sort).to eq [10, 11, 12] end