Sha256: 7aa177ec6430ca8d47125ac8ab92f1f9810591ffebb67add1dc2550d666079c6

Contents?: true

Size: 1.07 KB

Versions: 61

Compression:

Stored size: 1.07 KB

Contents

module MinimumViableProduct
  module SessionConcern
    extend ActiveSupport::Concern

    COOKIE_ID = '_session'

    included do
      helper_method :current_user
    end

    def sign_in(user)
      session[COOKIE_ID] = user.id
    end

    def current_user
      @current_user ||= begin
                          if session[COOKIE_ID]
                            Conversion.find(session[COOKIE_ID])
                          end
                        rescue ActiveRecord::RecordNotFound
                          session[COOKIE_ID] = nil
                        end
    end

    # http://blog.laaz.org/tech/2012/12/27/rails-redirect_back_or_default/
    def store_location
      session[:return_to] = if request.get?
        request.url
      else
        request.referer
      end
    end

    # http://blog.laaz.org/tech/2012/12/27/rails-redirect_back_or_default/
    def redirect_back_or_default(default = root_url)
      redirect_to(session.delete(:return_to) || request.referer || default)
    end
    alias_method :redirect_to_back_or_default, :redirect_back_or_default
  end
end

Version data entries

61 entries across 60 versions & 2 rubygems

Version Path
minimum-viable-product-0.0.4 app/controllers/mvp/concerns/session_concern.rb