Skip to content

Instantly share code, notes, and snippets.

@iamnader
Created December 9, 2010 18:49
Show Gist options
  • Save iamnader/735133 to your computer and use it in GitHub Desktop.
Save iamnader/735133 to your computer and use it in GitHub Desktop.
Extending Devise::Mailer to accept attributes available to the view
module DeviseMailerWithAttributes
class Devise::Mailer < ::ActionMailer::Base
alias_method :old_confirmation_instructions, :confirmation_instructions
alias_method :old_reset_password_instructions, :reset_password_instructions
alias_method :old_unlock_instructions, :unlock_instructions
alias_method :old_invitation_instructions, :invitation_instructions
def confirmation_instructions(record, attributes={})
set_attributes(attributes)
old_confirmation_instructions(record)
end
def reset_password_instructions(record, attributes={})
set_attributes(attributes)
old_reset_password_instructions(record)
end
def unlock_instructions(record, attributes={})
set_attributes(attributes)
old_unlock_instructions(record)
end
def invitation_instructions(record, attributes={})
set_attributes(attributes)
old_invitation_instructions(record)
end
def set_attributes(attributes)
attributes.each {|k,v| instance_variable_set("@#{k}", v) }
end
end
class User
alias_method :old_send_confirmation_instructions, :send_confirmation_instructions
alias_method :old_send_reset_password_instructions, :send_reset_password_instructions
alias_method :old_send_unlock_instructions, :send_unlock_instructions
alias_method :old_invite!, :invite!
def send_reset_password_instructions(attributes={})
generate_reset_password_token!
::Devise.mailer.reset_password_instructions(self, attributes).deliver
end
def send_confirmation_instructions(attributes={})
generate_confirmation_token! if self.confirmation_token.nil?
::Devise.mailer.confirmation_instructions(self, attributes).deliver
end
def send_unlock_instructions(attributes={})
::Devise.mailer.unlock_instructions(self, attributes).deliver
end
def invite!(attributes={})
if new_record? || invited?
self.skip_confirmation! if self.new_record? && self.respond_to?(:skip_confirmation!)
generate_invitation_token if self.invitation_token.nil?
self.invitation_sent_at = Time.now.utc
save(:validate => false)
::Devise.mailer.invitation_instructions(self, attributes).deliver
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment