1
0
Fork 0
mirror of https://github.com/heartcombo/devise.git synced 2022-11-09 12:18:31 -05:00
heartcombo--devise/lib/devise/controllers/helpers.rb

120 lines
4.1 KiB
Ruby
Raw Normal View History

2009-10-12 08:37:42 -03:00
module Devise
module Controllers
# Those helpers are used only inside Devise controllers and should not be
# included in ApplicationController since they all depend on the url being
# accessed.
2009-10-12 08:37:42 -03:00
module Helpers
def self.included(base)
base.class_eval do
helper_method :resource, :resource_name, :resource_class, :devise_mapping, :devise_controller?
hide_action :resource, :resource_name, :resource_class, :devise_mapping, :devise_controller?
skip_before_filter *Devise.mappings.keys.map { |m| :"authenticate_#{m}!" }
before_filter :is_devise_resource?
2009-10-12 08:37:42 -03:00
end
end
2009-10-17 12:10:15 -03:00
# Gets the actual resource stored in the instance variable
2009-10-12 08:37:42 -03:00
def resource
instance_variable_get(:"@#{resource_name}")
end
2009-10-17 12:10:15 -03:00
# Proxy to devise map name
2009-10-12 08:37:42 -03:00
def resource_name
devise_mapping.name
end
2009-10-17 12:10:15 -03:00
# Proxy to devise map class
2009-10-12 08:37:42 -03:00
def resource_class
devise_mapping.to
end
# Attempt to find the mapped route for devise based on request path
def devise_mapping
@devise_mapping ||= Devise::Mapping.find_by_path(request.path)
end
# Overwrites devise_controller? to return true
def devise_controller?
true
end
2009-10-12 09:56:12 -03:00
protected
2009-10-12 08:37:42 -03:00
# Redirects to stored uri before signing in or the default path and clear
# return to.
def redirect_back_or_to(default)
redirect_to(stored_location_for(resource_name) || default)
end
# Checks for the existence of the resource root path. If it exists,
# returns it, otherwise returns the default root_path.
# Used after authenticating a user, confirming it's account or updating
# it's password, so we are able to redirect to scoped root paths.
# Examples (for a user scope):
# map.user_root '/users', :controller => 'users' # creates user_root_path
#
# map.namespace :users do |users|
# users.root # creates user_root_path
# end
def home_or_root_path
home_path = :"#{resource_name}_root_path"
respond_to?(home_path, true) ? send(home_path) : root_path
end
# Checks whether it's a devise mapped resource or not.
def is_devise_resource? #:nodoc:
raise ActionController::UnknownAction unless devise_mapping && devise_mapping.allows?(controller_name)
2009-10-12 09:56:12 -03:00
end
2009-10-12 08:37:42 -03:00
2009-10-17 12:10:15 -03:00
# Sets the resource creating an instance variable
2009-10-12 09:56:12 -03:00
def resource=(new_resource)
instance_variable_set(:"@#{resource_name}", new_resource)
end
2009-10-12 08:37:42 -03:00
# Build a devise resource without setting password and password confirmation fields.
2009-10-27 21:31:12 -02:00
def build_resource
self.resource ||= begin
attributes = params[resource_name].try(:except, :password, :password_confirmation)
resource_class.new(attributes)
end
2009-10-27 21:31:12 -02:00
end
# Helper for use in before_filters where no authentication is required.
#
# Example:
# before_filter :require_no_authentication, :only => :new
def require_no_authentication
redirect_to home_or_root_path if warden.authenticated?(resource_name)
end
2009-10-17 12:10:15 -03:00
# Sets the flash message with :key, using I18n. By default you are able
# to setup your messages using specific resource scope, and if no one is
# found we look to default scope.
# Example (i18n locale file):
#
# en:
# devise:
# passwords:
# #default_scope_messages - only if resource_scope is not found
2009-10-20 01:28:01 -02:00
# user:
2009-10-17 12:10:15 -03:00
# #resource_scope_messages
#
# Please refer to README or en.yml locale file to check what messages are
# available.
def set_flash_message(key, kind, now=false)
flash_hash = now ? flash.now : flash
flash_hash[key] = I18n.t(:"#{resource_name}.#{kind}",
2009-10-12 09:56:12 -03:00
:scope => [:devise, controller_name.to_sym], :default => kind)
end
2009-10-12 08:37:42 -03:00
# Shortcut to set flash.now message. Same rules applied from set_flash_message
def set_now_flash_message(key, kind)
set_flash_message(key, kind, true)
end
2009-10-12 08:37:42 -03:00
end
end
end