varatis
varatis

Reputation: 14740

Devise: call method on sessions#destroy

I was wondering what the best way to add extra functionality to Devise's session#destroy action would be.

To give some context, I'm making a website where Users have Carts, and when the user's session either times out or he logs out, I want his Cart to be labeled as inactive.

I found this but I'm a bit hesitant to override the Devise controller, as it seems a bit messy...

Are there any other ways to set this Cart to inactive when a user's session is destroyed?

Upvotes: 3

Views: 2344

Answers (1)

Viktor Trón
Viktor Trón

Reputation: 8884

I do suggest you derive your controller from devise and hook onto the action, so you can safely keep away from devise's internals.

# routes.rb
devise_for :users, :controllers => { :sessions => "sessions" } # etc

# sessions_controller.rb
class SessionsController < Devise::SessionsController

  after_filter :set_cart_inactive!, :only => :destroy
  def set_cart_inactive!
    unless user_signed_in? # logout successful?
       # code here
    end
  end

end

Upvotes: 5

Related Questions