You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1.3 KiB
51 lines
1.3 KiB
module Legacy
|
|
class Recipient < ApplicationRecord
|
|
belongs_to :school
|
|
validates_associated :school
|
|
|
|
has_many :recipient_schedules
|
|
has_many :attempts
|
|
|
|
has_many :students
|
|
|
|
validates :name, presence: true
|
|
|
|
scope :for_school, ->(school) { where(school:) }
|
|
scope :created_in, ->(year) { where('extract(year from recipients.created_at) = ?', year) }
|
|
|
|
before_destroy :sync_lists
|
|
|
|
def self.import(school, file)
|
|
CSV.foreach(file.path, headers: true) do |row|
|
|
school.recipients.create!(row.to_hash)
|
|
# recipient_hash = row.to_hash
|
|
# recipient = school.recipients.where(phone: recipient_hash["phone"])
|
|
#
|
|
# if recipient.count == 1
|
|
# recipient.first.update(recipient_hash)
|
|
# else
|
|
# school.recipients.create!(recipient_hash)
|
|
# end
|
|
end
|
|
end
|
|
|
|
def update_counts
|
|
update(
|
|
attempts_count: attempts.count,
|
|
responses_count: attempts.with_answer.count
|
|
)
|
|
end
|
|
|
|
private
|
|
|
|
def sync_lists
|
|
school.recipient_lists.each do |recipient_list|
|
|
next if recipient_list.recipient_id_array.index(id).nil?
|
|
|
|
updated_ids = recipient_list.recipient_id_array - [id]
|
|
recipient_list.update(recipient_id_array: updated_ids)
|
|
end
|
|
end
|
|
end
|
|
end
|