Skip to content

Instantly share code, notes, and snippets.

@bloudermilk
Created August 8, 2012 23:03
Show Gist options
  • Save bloudermilk/3299589 to your computer and use it in GitHub Desktop.
Save bloudermilk/3299589 to your computer and use it in GitHub Desktop.
Stupid-simple invitations with Devise
class Admin < ActiveRecord::Base
attr_accessor :invite
devise :database_authenticatable, :registerable, :recoverable, :rememberable,
:trackable, :validatable
def self.new_invited(attributes)
new(attributes.merge(invite: true))
end
private
# Slightly modified version of
# Devise::Modules::Recoverable#send_reset_password_instructions that doesn't
# save the record (since we call this method right before a save) and allows
# us to trigger our own special invite mailer
def invite!
generate_reset_password_token if should_generate_reset_token?
# TODO: Invoke invite mailer here instead of reset password mailer
devise_mailer.reset_password_instructions(self).deliver
end
# Overrides Devise::Modules::Recoverable#password_required? so that #password
# isn't required when we're inviting a user.
def password_required?
invite ? false : super
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment