2009-10-12 07:37:42 -04:00
|
|
|
module Devise
|
|
|
|
module Controllers
|
2009-10-27 19:26:40 -04:00
|
|
|
# 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 07:37:42 -04:00
|
|
|
module Helpers
|
|
|
|
|
|
|
|
def self.included(base)
|
|
|
|
base.class_eval do
|
2009-11-18 07:22:01 -05:00
|
|
|
unloadable
|
|
|
|
|
2009-11-19 08:43:31 -05:00
|
|
|
helper_method :resource, :scope_name, :resource_name, :resource_class, :devise_mapping, :devise_controller?
|
|
|
|
hide_action :resource, :scope_name, :resource_name, :resource_class, :devise_mapping, :devise_controller?
|
2009-10-27 19:26:40 -04:00
|
|
|
|
2009-11-06 06:33:18 -05:00
|
|
|
skip_before_filter *Devise.mappings.keys.map { |m| :"authenticate_#{m}!" }
|
2009-10-27 19:26:40 -04:00
|
|
|
before_filter :is_devise_resource?
|
2009-10-12 07:37:42 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-10-17 11:10:15 -04:00
|
|
|
# Gets the actual resource stored in the instance variable
|
2009-10-12 07:37:42 -04:00
|
|
|
def resource
|
|
|
|
instance_variable_get(:"@#{resource_name}")
|
|
|
|
end
|
|
|
|
|
2009-10-17 11:10:15 -04:00
|
|
|
# Proxy to devise map name
|
2009-10-12 07:37:42 -04:00
|
|
|
def resource_name
|
|
|
|
devise_mapping.name
|
|
|
|
end
|
2009-11-19 08:43:31 -05:00
|
|
|
alias :scope_name :resource_name
|
2009-10-12 07:37:42 -04:00
|
|
|
|
2009-10-17 11:10:15 -04:00
|
|
|
# Proxy to devise map class
|
2009-10-12 07:37:42 -04:00
|
|
|
def resource_class
|
|
|
|
devise_mapping.to
|
|
|
|
end
|
|
|
|
|
2009-10-27 19:26:40 -04:00
|
|
|
# Attempt to find the mapped route for devise based on request path
|
|
|
|
def devise_mapping
|
2010-01-06 08:31:00 -05:00
|
|
|
@devise_mapping ||= begin
|
|
|
|
mapping = Devise::Mapping.find_by_path(request.path)
|
|
|
|
mapping ||= Devise.mappings[Devise.default_scope] if Devise.use_default_scope
|
|
|
|
mapping
|
|
|
|
end
|
2009-10-27 19:26:40 -04:00
|
|
|
end
|
|
|
|
|
2009-11-06 06:33:18 -05:00
|
|
|
# Overwrites devise_controller? to return true
|
|
|
|
def devise_controller?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2009-10-12 08:56:12 -04:00
|
|
|
protected
|
2009-10-12 07:37:42 -04:00
|
|
|
|
2009-10-27 19:26:40 -04:00
|
|
|
# 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 08:56:12 -04:00
|
|
|
end
|
2009-10-12 07:37:42 -04:00
|
|
|
|
2009-10-17 11:10:15 -04:00
|
|
|
# Sets the resource creating an instance variable
|
2009-10-12 08:56:12 -04:00
|
|
|
def resource=(new_resource)
|
|
|
|
instance_variable_set(:"@#{resource_name}", new_resource)
|
|
|
|
end
|
2009-10-12 07:37:42 -04:00
|
|
|
|
2009-10-29 06:43:45 -04:00
|
|
|
# Build a devise resource without setting password and password confirmation fields.
|
2009-10-27 19:31:12 -04:00
|
|
|
def build_resource
|
2009-10-29 06:43:45 -04:00
|
|
|
self.resource ||= begin
|
|
|
|
attributes = params[resource_name].try(:except, :password, :password_confirmation)
|
2009-11-21 08:58:02 -05:00
|
|
|
resource_class.new(attributes || {})
|
2009-10-29 06:43:45 -04:00
|
|
|
end
|
2009-10-27 19:31:12 -04:00
|
|
|
end
|
|
|
|
|
2009-10-27 19:26:40 -04:00
|
|
|
# Helper for use in before_filters where no authentication is required.
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
# before_filter :require_no_authentication, :only => :new
|
|
|
|
def require_no_authentication
|
2009-11-19 08:16:31 -05:00
|
|
|
redirect_to after_sign_in_path_for(resource_name) if warden.authenticated?(resource_name)
|
2009-10-27 19:26:40 -04:00
|
|
|
end
|
|
|
|
|
2009-10-17 11:10:15 -04: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-19 23:28:01 -04:00
|
|
|
# user:
|
2009-10-17 11:10:15 -04:00
|
|
|
# #resource_scope_messages
|
|
|
|
#
|
|
|
|
# Please refer to README or en.yml locale file to check what messages are
|
|
|
|
# available.
|
2009-10-21 09:20:10 -04:00
|
|
|
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 08:56:12 -04:00
|
|
|
:scope => [:devise, controller_name.to_sym], :default => kind)
|
|
|
|
end
|
2009-10-12 07:37:42 -04:00
|
|
|
|
2009-10-21 09:20:10 -04: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-11-21 17:07:37 -05:00
|
|
|
# Render a view for the specified scope. Turned off by default.
|
2009-12-15 12:48:00 -05:00
|
|
|
# Accepts just :controller as option.
|
|
|
|
def render_with_scope(action, options={})
|
2009-12-19 14:32:15 -05:00
|
|
|
controller_name = options.delete(:controller) || self.controller_name
|
2009-11-21 17:07:37 -05:00
|
|
|
if Devise.scoped_views
|
|
|
|
begin
|
2009-12-14 16:04:28 -05:00
|
|
|
render :template => "#{controller_name}/#{devise_mapping.as}/#{action}"
|
2009-11-21 17:07:37 -05:00
|
|
|
rescue ActionView::MissingTemplate
|
2009-12-15 12:48:00 -05:00
|
|
|
render action, :controller => controller_name
|
2009-11-21 17:07:37 -05:00
|
|
|
end
|
|
|
|
else
|
2009-12-15 12:48:00 -05:00
|
|
|
render action, :controller => controller_name
|
2009-11-21 17:07:37 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-10-12 07:37:42 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|