nuringa

Untitled

Jan 25th, 2021
564
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. namespace :permissions do
  2.   desc "update members with membership permissions"
  3.   task membership_permissions: :environment do
  4.     permissions = [
  5.       Permission.find_by(identifier: 'toggle_ready_status'),
  6.       Permission.find_by(identifier: 'destroy_membership')
  7.     ]
  8.  
  9.     memberships = Membership.creator + Membership.member
  10.  
  11.     puts "#{memberships.count} memberships found"
  12.  
  13.     memberships.each_with_index do |membership, i|
  14.       permissions.each do |permission|
  15.         next if PermissionsUser.exists?(user: membership.user, permission: permission, board: membership.board)
  16.  
  17.         begin
  18.           PermissionsUser.create!(user: membership.user, permission: permission, board: membership.board)
  19.         rescue StandardError => e
  20.           puts "Failed to add #{permission.identifier} for #{membership.user.email}: #{e.message}"
  21.         end
  22.  
  23.         puts "100 memberships processed - #{memberships.count - i} remains" if i % 100 == 0
  24.       end;'ok'
  25.     end;'ok'
  26.   end
  27. end
RAW Paste Data