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
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s