scinote-web/app/notifications/repository_item_date_notification.rb

50 lines
1.3 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
class RepositoryItemDateNotification < BaseNotification
def self.subtype
:item_date_reminder
end
def title
unit = human_readable_unit(params[:reminder_unit], params[:reminder_value])
2023-12-11 20:18:08 +08:00
I18n.t(
'notifications.content.item_date_reminder.message_html',
repository_row_name: subject.name,
value: params[:reminder_value],
2023-12-11 20:18:08 +08:00
units: unit
)
end
def subject
RepositoryRow.find(params[:repository_row_id])
rescue ActiveRecord::RecordNotFound
NonExistantRecord.new(params[:repository_row_name])
end
after_deliver do
if params[:repository_date_time_value_id]
RepositoryDateTimeValue.find(params[:repository_date_time_value_id]).update(notification_sent: true)
elsif params[:repository_date_value_id]
RepositoryDateValue.find(params[:repository_date_value_id]).update(notification_sent: true)
end
end
private
def human_readable_unit(seconds, value)
return unless seconds
units_hash = {
'2419200' => 'month',
'604800' => 'week',
'86400' => 'day'
}
base_unit = units_hash.fetch(seconds) do
raise ArgumentError, "Unrecognized time unit for seconds value: #{seconds}"
end
value.to_i > 1 ? base_unit.pluralize : base_unit
end
end