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:

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

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

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

Leave a Reply

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

You are commenting using your 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