Use role caching for Rolify to avoid N+1 queries

Resolves #2018

See https://github.com/RolifyCommunity/rolify#cached-roles-to-avoid-n1-issue
This commit is contained in:
James Mason 2018-03-28 17:31:18 -07:00
parent 30ba7b133b
commit 98defe2d0f
13 changed files with 44 additions and 35 deletions

View file

@ -30,7 +30,8 @@ feature Conference do
.to eq('Conference was successfully created.')
expect(Conference.count).to eq(expected_count)
expect(Conference.last.organization).to eq(organization)
expect(user.has_role? :organizer, Conference.last).to eq(true)
user.reload
expect(user.has_cached_role? :organizer, Conference.last).to eq(true)
end
scenario 'update conference', feature: true, js: true do

View file

@ -58,7 +58,7 @@ feature Role do
click_button 'Add'
user_with_no_role.reload
expect(user_with_no_role.has_role?(role.name, conference)).to eq true
expect(user_with_no_role.has_cached_role?(role.name, conference)).to eq true
end
scenario "removes role #{role_name}", feature: true, js: true do
@ -69,7 +69,8 @@ feature Role do
expect(find('.alert').text).to eq "×Successfully removed role #{role_name} from user #{user_with_role.email}"
expect(by_role_name).to eq(role_name) | eq('organizer')
expect(user_with_role.has_role?(role_name, conference)).to eq false
user_with_role.reload
expect(user_with_role.has_cached_role?(role_name, conference)).to eq false
end
end
@ -118,14 +119,15 @@ feature Role do
click_button 'Add'
user_with_no_role.reload
expect(user_with_no_role.has_role?('organization_admin', organization)).to eq true
expect(user_with_no_role.has_cached_role?('organization_admin', organization)).to eq true
end
scenario 'successfully removes role organization_admin' do
click_link('Admins', href: admins_admin_organization_path(organization.id))
first('tr').find('.btn-danger').click
expect(organization_admin.has_role?('organization_admin', organization)).to eq false
organization_admin.reload
expect(organization_admin.has_cached_role?('organization_admin', organization)).to eq false
end
end

View file

@ -347,17 +347,17 @@ describe Track do
end
it 'gives the role of the track organizer to the submitter of the track' do
expect(@submitter.has_role?(:track_organizer, self_organized_track)).to eq false
expect(@submitter.has_cached_role?(:track_organizer, self_organized_track)).to eq false
self_organized_track.assign_role_to_submitter
expect(@submitter.has_role?(:track_organizer, self_organized_track)).to eq true
expect(@submitter.has_cached_role?(:track_organizer, self_organized_track)).to eq true
end
it 'is executed when the track is confirmed' do
self_organized_track.state = 'accepted'
self_organized_track.save!
expect(@submitter.has_role?(:track_organizer, self_organized_track)).to eq false
expect(@submitter.has_cached_role?(:track_organizer, self_organized_track)).to eq false
self_organized_track.confirm
expect(@submitter.has_role?(:track_organizer, self_organized_track)).to eq true
expect(@submitter.has_cached_role?(:track_organizer, self_organized_track)).to eq true
end
end
@ -374,9 +374,10 @@ describe Track do
end
it 'revokes the role of the track organizer' do
expect(@a_track_organizer.has_role?(:track_organizer, self_organized_track)).to eq true
expect(@a_track_organizer.has_cached_role?(:track_organizer, self_organized_track)).to eq true
self_organized_track.revoke_role_and_cleanup
expect(@a_track_organizer.has_role?(:track_organizer, self_organized_track)).to eq false
@a_track_organizer.reload
expect(@a_track_organizer.has_cached_role?(:track_organizer, self_organized_track)).to eq false
end
it 'destroys the track\'s schedules' do
@ -403,14 +404,16 @@ describe Track do
self_organized_track.state = 'confirmed'
self_organized_track.save!
self_organized_track.cancel
expect(@a_track_organizer.has_role?(:track_organizer, self_organized_track)).to eq false
@a_track_organizer.reload
expect(@a_track_organizer.has_cached_role?(:track_organizer, self_organized_track)).to eq false
@event_of_self_organized_track.reload
expect(@event_of_self_organized_track.track).to eq nil
end
it 'is executed when the track is withdrawn' do
self_organized_track.withdraw
expect(@a_track_organizer.has_role?(:track_organizer, self_organized_track)).to eq false
@a_track_organizer.reload
expect(@a_track_organizer.has_cached_role?(:track_organizer, self_organized_track)).to eq false
@event_of_self_organized_track.reload
expect(@event_of_self_organized_track.track).to eq nil
end

View file

@ -395,22 +395,22 @@ describe User do
expect(another_user.roles[1]).to eq(cfp_role)
end
describe '#has_role?' do
describe '#has_cached_role?' do
describe 'when user has a role' do
it 'returns true when the user has the role' do
user = create(:user, role_ids: organizer_role.id)
expect(user.has_role?('organizer', conference)).to be true
expect(user.has_cached_role?('organizer', conference)).to be true
end
it 'returns false when the user does not have the role' do
user = create(:user, role_ids: cfp_role.id)
expect(user.has_role?('organizer', conference)).to be false
expect(user.has_cached_role?('organizer', conference)).to be false
end
end
it 'returns false when the user does not have a role' do
user = create(:user, role_ids: [])
expect(user.has_role?('organizer', conference)).to be false
expect(user.has_cached_role?('organizer', conference)).to be false
end
end
end