Skip to content

Commit

Permalink
Add redemption and claim models and rewrite workflow accordingly
Browse files Browse the repository at this point in the history
  • Loading branch information
fosterfarrell9 committed Aug 16, 2024
1 parent 98a957a commit b712851
Show file tree
Hide file tree
Showing 26 changed files with 273 additions and 209 deletions.
2 changes: 1 addition & 1 deletion app/abilities/voucher_ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ def initialize(user)
user.can_update_personell?(voucher.lecture)
end

can :redeem, Voucher
can [:verify, :redeem], Voucher
end
end
10 changes: 10 additions & 0 deletions app/controllers/concerns/notifier.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module Notifier
extend ActiveSupport::Concern

def notify_new_editor_by_mail(editor, lecture)
NotificationMailer.with(recipient: editor,
locale: editor.locale,
lecture: lecture)
.new_editor_email.deliver_later
end
end
91 changes: 2 additions & 89 deletions app/controllers/lectures_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# LecturesController
class LecturesController < ApplicationController
include Notifier
include ActionController::RequestForgeryProtection
before_action :set_lecture, except: [:new, :create, :search]
before_action :set_lecture_cookie, only: [:show, :organizational,
Expand Down Expand Up @@ -105,7 +106,7 @@ def update
recipients = User.where(id: new_ids)

recipients.each do |r|
notify_new_editor_by_mail(r)
notify_new_editor_by_mail(r, @lecture)
end
end

Expand Down Expand Up @@ -296,27 +297,6 @@ def import_toc
redirect_to edit_lecture_path(@lecture)
end

def become_tutor
if Voucher.check_voucher(become_tutor_params[:voucher_hash])
selected_tutorials = @lecture.tutorials.where(id: become_tutor_params[:tutorial_ids])
@lecture.update_tutor_status!(current_user, selected_tutorials)
create_became_tutor_notification(selected_tutorials)
redirect_to edit_profile_path, notice: I18n.t("controllers.become_tutor_success")
else
handle_invalid_voucher
end
end

def become_editor
if Voucher.check_voucher(become_editor_params[:voucher_hash])
@lecture.update_editor_status!(current_user)
notify_new_editor_by_mail(current_user)
redirect_to edit_profile_path, notice: I18n.t("controllers.become_editor_success")
else
handle_invalid_voucher
end
end

private

def set_lecture
Expand Down Expand Up @@ -475,71 +455,4 @@ def check_if_enough_questions

redirect_to :root, alert: I18n.t("controllers.no_test")
end

def become_tutor_params
params.permit(:voucher_hash, tutorial_ids: [])
end

def become_editor_params
params.permit(:voucher_hash)
end

def handle_invalid_voucher
error_message = I18n.t("controllers.voucher_invalid")
respond_to do |format|
format.js { render "error", locals: { error_message: error_message } }
format.html { redirect_to edit_profile_path, alert: error_message }
end
end

def notify_new_editor_by_mail(editor)
NotificationMailer.with(recipient: editor,
locale: editor.locale,
lecture: @lecture)
.new_editor_email.deliver_later
end

# def create_became_tutor_notification(selected_tutorials)
# @lecture.editors_and_teacher.each do |editor|
# details = I18n.t("notifications.became_tutor", user: current_user.info,
# locale: editor.locale)
# if selected_tutorials
# details << I18n.t("notifications.tutorial_details",
# tutorials: selected_tutorials.map(&:title).join(", "),
# locale: editor.locale)
# end
# Notification.create(recipient: editor,
# notifiable: @lecture,
# action: "redemption",
# details: details)
# end
# end

def create_became_tutor_notification(selected_tutorials)
@lecture.editors_and_teacher.each do |editor|
details = build_notification_details(editor, selected_tutorials)
create_notification(editor, details)
end
end

def build_notification_details(editor, selected_tutorials)
details = I18n.t("notifications.became_tutor", user: current_user.info,
locale: editor.locale)
if selected_tutorials
tutorial_titles = selected_tutorials.map(&:title).join(", ")
details << I18n.t("notifications.tutorial_details",
tutorials: tutorial_titles,
locale: editor.locale)
end
details
end

def create_notification(editor, details)
Notification.create(
recipient: editor,
notifiable: @lecture,
action: "redemption",
details: details
)
end
end
57 changes: 56 additions & 1 deletion app/controllers/vouchers_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# app/controllers/vouchers_controller.rb
class VouchersController < ApplicationController
include Notifier
before_action :set_voucher, only: [:invalidate]
authorize_resource except: :create

Expand Down Expand Up @@ -29,7 +30,7 @@ def invalidate
end
end

def redeem
def verify
@voucher = Voucher.check_voucher(params[:voucher_hash])
respond_to do |format|
if @voucher
Expand All @@ -43,12 +44,28 @@ def redeem
end
end

def redeem
voucher = Voucher.check_voucher(redeem_voucher_params[:voucher_hash])
if voucher
lecture = voucher.lecture
redemption = process_voucher(voucher, lecture)
redemption.create_notifications!
redirect_to edit_profile_path, notice: success_message(voucher)
else
handle_invalid_voucher
end
end

private

def voucher_params
params.permit(:lecture_id, :sort)
end

def redeem_voucher_params
params.permit(:voucher_hash, tutorial_ids: [])
end

def set_voucher
@voucher = Voucher.find_by(id: params[:id])
return if @voucher
Expand All @@ -62,6 +79,36 @@ def set_related_data
I18n.locale = @lecture.locale
end

def process_voucher(voucher, lecture)
if voucher.tutor?
process_tutor_voucher(voucher, lecture)
elsif voucher.editor?
process_editor_voucher(voucher, lecture)
end
end

def process_tutor_voucher(voucher, lecture)
selected_tutorials = lecture.tutorials
.where(id: redeem_voucher_params[:tutorial_ids])
lecture.update_tutor_status!(current_user, selected_tutorials)
Redemption.create(user: current_user, voucher: voucher,
claimed_tutorials: selected_tutorials)
end

def process_editor_voucher(voucher, lecture)
lecture.update_editor_status!(current_user)
notify_new_editor_by_mail(current_user, lecture)
Redemption.create(user: current_user, voucher: voucher)
end

def success_message(voucher)
if voucher.tutor?
I18n.t("controllers.become_tutor_success")
elsif voucher.editor?
I18n.t("controllers.become_editor_success")
end
end

def handle_successful_save(format)
format.html { redirect_to edit_lecture_path(@lecture, anchor: "people") }
format.js
Expand Down Expand Up @@ -92,4 +139,12 @@ def handle_voucher_not_found
end
end
end

def handle_invalid_voucher
error_message = I18n.t("controllers.voucher_invalid")
respond_to do |format|
format.js { render "error", locals: { error_message: error_message } }
format.html { redirect_to edit_profile_path, alert: error_message }
end
end
end
68 changes: 54 additions & 14 deletions app/helpers/notifications_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def notification_menu_item_details(notification)
return medium_notification_item_details(notifiable) if notification.medium?
return course_notification_item_details(notifiable) if notification.course?
return lecture_notification_item_details(notifiable) if notification.lecture?
return redemption_notification_item_details(notification) if notification.redemption?
return redemption_notification_item_details(notifiable) if notification.redemption?

""
end
Expand Down Expand Up @@ -77,7 +77,7 @@ def notification_link(notification)
elsif notification.lecture?
lecture_notification_card_link
elsif notification.redemption?
notification.details
redemption_notification_details(notifiable)
else
notifiable.details
end
Expand All @@ -91,24 +91,64 @@ def items_card_size(small, comments_below)
end

# create text for lecture announcement in notification card header
def redemption_notification_card_header(lecture)
link_to(lecture.title_for_viewers,
edit_lecture_path(lecture, anchor: "people"),
def redemption_notification_card_header(redemption)
link_to(redemption.lecture.title_for_viewers,
edit_lecture_path(redemption.lecture, anchor: "people"),
class: "text-dark")
end

def redemption_notification_item_header(lecture)
t("notifications.redemption_in_lecture", lecture: lecture.title_for_viewers)
def redemption_notification_item_header(redemption)
t("notifications.redemption_in_lecture",
lecture: redemption.lecture.title_for_viewers)
end

def redemption_notification_item_details(notification)
extract_name_from_redemption_details(notification.details)
def redemption_notification_details(redemption)
if redemption.tutor?
details = I18n.t("notifications.became_tutor", user: redemption.user.info)
tutorial_titles = redemption.claimed_tutorials.map(&:title).join(", ")
details << I18n.t("notifications.tutorial_details",
tutorials: tutorial_titles)
else
I18n.t("notifications.became_editor", user: redemption.user.info)
end
end

# this a admittedly a hack but I did not want to add another column to
# the notifications table
def extract_name_from_redemption_details(details)
match_data = details.match(/^(.+?) \(.+\)/)
match_data ? match_data[1] : nil
def redemption_notification_item_details(redemption)
result = if redemption.tutor?
tutor_notification_item_details(redemption)
elsif redemption.editor?
editor_notification_item_details(redemption)
end

truncate_result(result)
end

private

def tutor_notification_item_details(redemption)
tutorials = redemption.claimed_tutorials.map(&:title).join(", ")
"#{t("basics.tutor")} #{redemption.user.tutorial_name}: #{tutorials}"
end

def editor_notification_item_details(redemption)
"#{t("basics.editor")} #{redemption.user.tutorial_name}"
end

def tutor_notification_details(redemption)
details = I18n.t("notifications.became_tutor",
user: redemption.user.info)
tutorial_titles = redemption.claimed_tutorials.map(&:title).join(", ")
details << I18n.t("notifications.tutorial_details",
tutorials: tutorial_titles)
end

def editor_notification_details(redemption)
I18n.t("notifications.became_editor", user: redemption.user.info)
end

def truncate_result(result)
result.first(40).tap do |truncated|
return truncated.length < 40 ? truncated : truncated + "..."

Check failure on line 151 in app/helpers/notifications_helper.rb

View workflow job for this annotation

GitHub Actions / RuboCop (Ruby)

Style/StringConcatenation: Prefer string interpolation to string concatenation.
end
end
end
8 changes: 8 additions & 0 deletions app/models/claim.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Claim class
# claims store what is beign taken over by the user when they redeem a voucher
# (e.g. a tutorial or a talk)

class Claim < ApplicationRecord
belongs_to :redemption
belongs_to :claimable, polymorphic: true
end
11 changes: 0 additions & 11 deletions app/models/contract.rb

This file was deleted.

Loading

0 comments on commit b712851

Please sign in to comment.