Last active
December 29, 2015 06:49
-
-
Save onthespotqa/7631577 to your computer and use it in GitHub Desktop.
Devise Override code
This file contains 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
#Confirmations Controller located in controllers/riders | |
class Riders::ConfirmationsController < Devise::ConfirmationsController | |
skip_before_filter :require_no_authentication | |
skip_before_filter :authenticate_user! | |
# PATCH /riders/confirmation | |
def update | |
with_unconfirmed_confirmable do | |
if @confirmable.has_no_password? | |
@confirmable.attempt_set_password(params[:rider]) | |
if @confirmable.valid? | |
do_confirm | |
else | |
do_show | |
@confirmable.errors.clear | |
end | |
else | |
self.class.add_error_on(self, :email, :password_allready_set) | |
end | |
end | |
if !@confirmable.errors.empty? | |
render "riders/confirmations/show" | |
end | |
end | |
# GET /riders/confirmation?confirmation_token=abcdef | |
def show | |
with_unconfirmed_confirmable do | |
if @confirmable.has_no_password? | |
do_show | |
else | |
do_confirm | |
end | |
end | |
if !@confirmable.errors.empty? | |
self.resource = @confirmable | |
render 'riders/confirmations/new' #Change this if you don't have the views on default path | |
end | |
end | |
protected | |
def with_unconfirmed_confirmable | |
original_token = params[:confirmation_token] | |
@confirmation_token = Devise.token_generator.digest(User, :confirmation_token, original_token) | |
@confirmable = Rider.find_or_initialize_with_error_by(:confirmation_token, @confirmation_token) | |
if !@confirmable.new_record? | |
@confirmable.only_if_unconfirmed {yield} | |
end | |
end | |
def do_show | |
@confirmation_token = params[:confirmation_token] | |
@requires_password = true | |
self.resource = @confirmable | |
render "riders/confirmations/show" #Change this if you don't have the views on default path | |
end | |
def do_confirm | |
@confirmable.confirm! | |
set_flash_message :notice, :confirmed | |
sign_in_and_redirect(resource_name, @confirmable) | |
end | |
end |
This file contains 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
#The devise code in my routes.rb file | |
devise_for :riders, :controllers => {:confirmations => 'riders/confirmations'} | |
devise_scope :rider do | |
patch '/riders/confirmation' => 'riders/confirmations#update', :via => :patch, :as => :update_rider_confirmation | |
end |
This file contains 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
#My riders/confirmations/show template. | |
= simple_form_for resource, :as => resource_name, :url => update_rider_confirmation_path, :html => {:method => 'patch'}, :id => 'activation-form' do |f| | |
= devise_error_messages! | |
- if @requires_password | |
= f.input :password, as: :password, label: 'Choose a Password:' | |
= f.input :password_confirmation,as: :password, label: 'Password Confirmation:' | |
= hidden_field_tag(:confirmation_token, @confirmation_token) | |
= f.submit "Activate" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment