29 May 2013

Using RefineryCMS signin but changing after_sign_in_path_for

The reason for this is so that we can make use of Refinery/Devise user authentication & roles (incl nifty modalbox login views) but get the redirect after the signin/signup/signout to return back to the origination.
The sample here is shown from the refinerycms-stores engine I am developing. So in this example: stores is the engine namespace, and therefore the filepaths are presumed in the "stores" path (either within app/vendor/extensions, or via a gem).
create a file:  stores/lib/refinery/stores/authenticated_systems.rb
module Refinery
  module AuthenticatedSystem
   
    def after_sign_in_path_for(resource_or_scope)
      store_root
    end

    def after_sign_out_path_for(resource_or_scope)
      store_root
    end

    def after_update_path_for(resource)
      store_root
    end

    def after_sign_up_path_for(resource)
      store_root
    end

private
    def store_root
      refinery.stores_root_url
    end
  end
end 
add a require in stores/lib/refinery/stores.rb to reference that file (it's the line following the autoload):
require 'refinerycms-core'

module Refinery
  autoload :StoresGenerator, 'generators/refinery/stores_generator'
    require 'refinery/stores/authenticated_system'

  module Stores
    require 'refinery/stores/engine'
add the route into stores/config/routes.rb (its the root :to ... line)
  namespace :stores do
   root :to => 'stores#index'
    resources :stores, :only => [:index, :show]  do
      collection do
        post :add_to_cart
        post :empty_cart
        post :checkout
      end
    end
 
restart your server

No comments:

Post a Comment