Last active
January 28, 2024 13:02
-
-
Save mike1011/7f8385f3bbe217f19ac787d0e065a9bd to your computer and use it in GitHub Desktop.
Adding Rails Devise Confirmable module to a non-user model so that it can be confirmed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Use Case - Use Devise Confirmable module to confirm newly added organization(org) using email(association) | |
We want Three things to work - | |
Use confirmable - send_confirmation_instructions to send email when new org is added | |
Define Routes - organization_confirmation_url, so that user can use this route to confirm the org | |
Add confirmable columns inside existing organizations table, update token,timestamps on db | |
====== HERE WE START ========= | |
- `organization.rb` | |
has_one :contact_detail ##contains email | |
devise :confirmable | |
after_commit :send_verification_email_to_the_organization, on: :create | |
private | |
def send_verification_email_to_the_organization | |
self.send_confirmation_instructions.deliver | |
end | |
##use to bypass devise looking for email within the current model, | |
def will_save_change_to_email? | |
false | |
end | |
##behaves like model has an email attribute too | |
attr_accessor :email | |
def email | |
self.contact_detail&.primary_email | |
end | |
- `routes.rb` | |
devise_for :organizations,:controllers => { | |
:confirmations => "organizations/confirmations", | |
} | |
- creating new controller at controllers/organizatons/confirmations_controller.rb | |
##no methods needed, Devise will do the job | |
class Organizations::ConfirmationsController < Devise::ConfirmationsController | |
end | |
- `migration to add new confirmable columns to organizations table` | |
add_column :organizations, :confirmation_token, :string | |
add_column :organizations, :confirmed_at, :datetime | |
add_column :organizations, :confirmation_sent_at, :datetime | |
add_column :organizations, :unconfirmed_email, :string | |
===== thats it==== | |
on Rails Console, you may directly test the above setup= | |
o=Organization.first | |
o.send_confirmation_instructions.deliver | |
###this will update confirmation_token, confirmation_sent_at and email will be send from Devise::Mailer#confirmation_instructions with the default template | |
containing organization_confirmation_url(:token =< "asdfjkadxfkldsj"). | |
##when user will click on the confirmation link, it will land on the newly created organizations/confirmations#show action, which is | |
already defined in the Parent class of Devise::ConfirmationsController.You may also think of adding after_confirmation_path_for(resource_name, resource) method in | |
controller to decide what to do after successful confirmation of org | |
==== Hope this helps ============ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment