Inside a mailer file, I have 11 methods all of which start with the line
@reservation = reservation
Is there a way to make this DRY? I tried the following:
def set_reservation
@reservation = reservation
end
and then
before_action :set_reservation
Unfortunately, this always gave me something along these lines:
AgentReservationMailer#send_reserve_complete_mail: processed outbound mail in 1.7ms
NameError: undefined local variable or method `reservation' for #<AgentReservationMailer:0x007ffc9ae5bb38>
I'm still a very junior level developer, but I would like to try and make things look as professional as I can - is what I am trying to do even possible though?
The reason you're seeing the error is that the mailer does not know of a variable reservation
inside the set_reservation
method. I'm assuming that the 11 methods you mention, which use the
@reservation = reservation
take reservation
as an argument. As it stands, there really is no need to try and reduce duplication.
As a side note, DRY is not a principle you should follow blindly. If you had a couple of lines that were the same in each method, then that would indeed justify an "extract method" refactoring. But replacing that @reservation = reservation
assignment with e.g. a method call set_reservation(reservation)
, you'd still end up repeating one line across all methods.