use more explicit and explanatory sql statement

This commit is contained in:
Alexis Reigel 2017-03-15 13:45:28 +01:00
parent 1735ed6139
commit 63e61cfd83
2 changed files with 26 additions and 8 deletions

View File

@ -144,7 +144,9 @@ module Routable
return none if paths.empty?
wheres = paths.map do |path|
"#{connection.quote(path)} LIKE CONCAT(routes.path, '%')"
"#{connection.quote(path)} = routes.path
OR
#{connection.quote(path)} LIKE CONCAT(routes.path, '/%')"
end
joins(:route).where(wheres.join(' OR '))

View File

@ -1,7 +1,7 @@
require 'spec_helper'
describe Group, 'Routable' do
let!(:group) { create(:group, name: 'group 1') }
let!(:group) { create(:group, name: 'foo') }
describe 'Validations' do
it { is_expected.to validate_presence_of(:route) }
@ -92,20 +92,24 @@ describe Group, 'Routable' do
end
describe '.member_hierarchy' do
# foo/bar would also match foo/barbaz instead of just foo/bar and foo/bar/baz
let!(:user) { create(:user) }
# _______ group _______
# group
# _______ (foo) _______
# | |
# | |
# nested_group_1 nested_group_2
# (bar) (barbaz)
# | |
# | |
# nested_group_1_1 nested_group_2_1
# (baz) (baz)
#
let!(:nested_group_1) { create :group, parent: group, name: 'group 1-1' }
let!(:nested_group_1_1) { create :group, parent: nested_group_1, name: 'group 1-1-1' }
let!(:nested_group_2) { create :group, parent: group, name: 'group 1-2' }
let!(:nested_group_2_1) { create :group, parent: nested_group_2, name: 'group 1-2-1' }
let!(:nested_group_1) { create :group, parent: group, name: 'bar' }
let!(:nested_group_1_1) { create :group, parent: nested_group_1, name: 'baz' }
let!(:nested_group_2) { create :group, parent: group, name: 'barbaz' }
let!(:nested_group_2_1) { create :group, parent: nested_group_2, name: 'baz' }
context 'user is not a member of any group' do
subject { described_class.member_hierarchy(user.id) }
@ -147,7 +151,7 @@ describe Group, 'Routable' do
end
end
context 'user is member of the first child (internal node)' do
context 'user is member of the first child (internal node), branch 1' do
before { nested_group_1.add_owner(user) }
subject { described_class.member_hierarchy(user.id) }
@ -159,6 +163,18 @@ describe Group, 'Routable' do
end
end
context 'user is member of the first child (internal node), branch 2' do
before { nested_group_2.add_owner(user) }
subject { described_class.member_hierarchy(user.id) }
it 'returns the groups in the hierarchy' do
is_expected.to match_array [
group,
nested_group_2, nested_group_2_1
]
end
end
context 'user is member of the last child (leaf node)' do
before { nested_group_1_1.add_owner(user) }
subject { described_class.member_hierarchy(user.id) }