Fix incorrect condition in `groups:grant_badge` rake task.

`#find` raises an error if the id given to it is invalid. As a result,
the conditional to check whether a `group` or `badge` is `present?` will
not be executed if any of the ids are invalid.

Follow up to
6ba914033c.
This commit is contained in:
Guo Xiang Tan 2019-05-09 14:51:29 +08:00
parent 867c1a5ac9
commit 626808e100
1 changed files with 21 additions and 12 deletions

View File

@ -8,21 +8,30 @@ task "groups:grant_badge", [:group_id, :badge_id] => [:environment] do |_, args|
exit 1 exit 1
end end
group = Group.find(group_id) group = Group.find_by(id: group_id)
badge = Badge.find(badge_id)
if group.present? && badge.present? unless group
puts "Granting badge '#{badge.name}' to all users in group '#{group.name}'..." puts "ERROR: `group_id` is invalid"
exit 1
end
count = 0 badge = Badge.find_by(id: badge_id)
group.users.each do |user|
begin unless badge
BadgeGranter.grant(badge, user) puts "ERROR: `badge_id` is invalid"
rescue => e exit 1
puts "", "Failed to grant badge to #{user.username}", e, e.backtrace.join("\n") end
end
putc "." if (count += 1) % 5 == 0 puts "Granting badge '#{badge.name}' to all users in group '#{group.name}'..."
count = 0
group.users.each do |user|
begin
BadgeGranter.grant(badge, user)
rescue => e
puts "", "Failed to grant badge to #{user.username}", e, e.backtrace.join("\n")
end end
putc "." if (count += 1) % 5 == 0
end end
puts "", "Done! Badge granted to #{count} members.", "" puts "", "Done! Badge granted to #{count} members.", ""