module Refinery::AuthenticatedSystem

Public Class Methods

included(base) click to toggle source
# File lib/refinery/authenticated_system.rb, line 65
def self.included(base)
  if base.respond_to? :helper_method
    base.send :helper_method, :current_refinery_user, :current_user_session,
                              :refinery_user_signed_in?, :refinery_user?
  end
end

Public Instance Methods

after_sign_in_path_for(resource_or_scope) click to toggle source

This just defines the devise method for after sign in to support extension namespace isolation…

# File lib/refinery/authenticated_system.rb, line 48
def after_sign_in_path_for(resource_or_scope)
  pop_stored_location ||
  sanitized_stored_location_for(resource_or_scope) ||
  signed_in_root_path(resource_or_scope)
end
after_sign_out_path_for(resource_or_scope) click to toggle source
# File lib/refinery/authenticated_system.rb, line 54
def after_sign_out_path_for(resource_or_scope)
  refinery.root_path
end
signed_in_root_path(resource_or_scope) click to toggle source

This defines the devise method for refinery routes

# File lib/refinery/authenticated_system.rb, line 22
def signed_in_root_path(resource_or_scope)
  scope = Devise::Mapping.find_scope!(resource_or_scope)
  home_path = "#{scope}_root_path"
  if respond_to?(home_path, true)
    refinery.send(home_path)
  elsif respond_to?(:admin_root_path)
    refinery.admin_root_path
  else
    "/"
  end
end

Protected Instance Methods

pop_stored_location() click to toggle source

Clear and return the stored location

# File lib/refinery/authenticated_system.rb, line 11
def pop_stored_location
  session.delete(:return_to)
end
redirect_back_or_default(default) click to toggle source

Redirect to the URI stored by the most recent store_location call or to the passed default.

# File lib/refinery/authenticated_system.rb, line 17
def redirect_back_or_default(default)
  redirect_to(pop_stored_location || default)
end
refinery_user?() click to toggle source
# File lib/refinery/authenticated_system.rb, line 58
def refinery_user?
  refinery_user_signed_in? && current_refinery_user.has_role?(:refinery)
end
sanitized_stored_location_for(resource_or_scope) click to toggle source

Pops the stored url, trims the sneaky “//” from it, and returns it.

Making sure bad urls aren’t stored in the first place should probably be a part of the Devise::FailureApp

# File lib/refinery/authenticated_system.rb, line 38
def sanitized_stored_location_for(resource_or_scope)
  # `stored_location_for` is the devise method that pops the
  # scoped `return_to` key
  location = stored_location_for(resource_or_scope)
  location.sub!("//", "/") if location.respond_to?(:sub!)
  location
end
store_location() click to toggle source

Store the URI of the current request in the session.

We can return to this location by calling redirect_back_or_default.

# File lib/refinery/authenticated_system.rb, line 6
def store_location
  session[:return_to] = request.fullpath
end