find in Ruby

So, I was working and found this nice idea on the code base about using the find for mail dunnings. This will be simplified to the max in order to keep the post short.

In the Payments table we have the following columns:
payment_reminder_sent_at
first_dunning_sent_at
last_dunning_sent_at

What this code do is, find in the current payment that was passed as an ActiveRecord object to this handler class.
It will find the column that is nil and send the right email through the UserMailer.

def send_next_dunning
  name = first_unsent_dunning
  UserMailer.created_mail_dunning(name, user.id, payment.id).deliver_later
end

def first_unsent_dunning
 reminders_order[1..-1].find { |d| send("#{d}_sent_at").nil? }
end

def reminders_order
  [:payment_reminder, :first_dunning, :last_dunning]
end

Leave a comment