docbrown/app/controllers/admin/badge_achievements_controller.rb
Julianna Tetreault 24a3b50d4a
Add the Ability to Remove Badge Achievements From Users (#9896) [deploy]
* Moves badge_achievement-related code from /admin/bagdes to /admin/badge_achievements
  - Adds an Admin::BadgeAchievements::Controller
  - Moves #award and #award_badges to Admin::BadgeAchievements::Controller
  - Removes #award and #award_badges from Admin::Badges::Controller
  - Moves award.html.erb from /admin/badges to /admin/badge_achievements
  - Removes badge_achievement routes from /admin/badges
  - Adds a redirect for /admin/badges/badge_achievements to routes.rb
  - Cleans up and refactors code in controllers, views, and routes

* Add more actions to Admin::BadgeAchievements::Controller and matching routes
  - Adds an #index and #destory action to the BadgeAchievements::Controller
  - Adds destroy to the badge_achievements routes
  - Adjusts redirects for badge_achievements in routes.rb
  - Removes dead code from the index view for Badges
  - Refactors the existing code in the index view for BadgeAchievements

* Adds an /admin/badge_achievements_spec and cleans up the /admin/badges_spec
  - Removes the badge_achievement-related tests from badges_spec.rb
  - Adds an additional test around deleting badge_achievements

* Add pagination to badge_achievements index view

* Adds badge_achievements to Admin menu items and add comment to routes.rb
  -Add badge_achievements to the Admin nav bar
  -Add a comment regarding redirects for badge_achievements

* Resolve JS console stacktrace notices and change wording on deletion buttons

* Add a Back to Badge Achievements button to the badge award form

* Adjust styling to fix failing /admin/badges_spec.rb

* Uses SQL paging to optimize scalibility and adds search functionality to index.html.erb
  - Use SQL paging in Admin::BadgeAchievements::Controller in #index and #award
  - Add a search by user ID to Badge Achievements index view
  - Add a limit of 15 badges to be shown on the Badge Achievement index view
  - Add a User ID column to the Badge Achievement index view

* Add Award Badge button back to Badge Achievement index.html.erb

* Change @badge to be more explicit and reword warning around badge deletion

* Adds award badge button back, rewords success message, and adjusts SQL paging
2020-08-25 11:14:47 -06:00

49 lines
1.4 KiB
Ruby

module Admin
class BadgeAchievementsController < Admin::ApplicationController
layout "admin"
def index
@q = BadgeAchievement
.includes(:badge)
.includes(:user)
.order(created_at: :desc)
.ransack(params[:q])
@badge_achievements = @q.result.page(params[:page] || 1).per(15)
end
def destroy
@badge_achievement = BadgeAchievement.find(params[:id])
if @badge_achievement.destroy
flash[:success] = "Badge achievement has been deleted!"
else
flash[:danger] = @badge_achievement.errors_as_sentence
end
redirect_to admin_badge_achievements_path
end
def award
@all_badges = Badge.all.select(:title, :slug)
end
def award_badges
raise ArgumentError, "Please choose a badge to award" if permitted_params[:badge].blank?
usernames = permitted_params[:usernames].downcase.split(/\s*,\s*/)
message = permitted_params[:message_markdown].presence || "Congrats!"
BadgeAchievements::BadgeAwardWorker.perform_async(usernames, permitted_params[:badge], message)
flash[:success] = "Badges are being rewarded. The task will finish shortly."
redirect_to admin_badge_achievements_path
rescue ArgumentError => e
flash[:danger] = e.message
redirect_to admin_badge_achievements_path
end
private
def permitted_params
params.permit(:usernames, :badge, :message_markdown)
end
end
end