Module: Devise::Controllers::Helpers::ClassMethods
- Defined in:
- lib/devise/controllers/helpers.rb
Instance Method Summary collapse
-
#devise_group(group_name, opts = {}) ⇒ Object
Define authentication filters and accessor helpers for a group of mappings.
- #log_process_action(payload) ⇒ Object
Instance Method Details
#devise_group(group_name, opts = {}) ⇒ Object
Define authentication filters and accessor helpers for a group of mappings. These methods are useful when you are working with multiple mappings that share some functionality. They are pretty much the same as the ones defined for normal mappings.
Example:
inside BlogsController (or any other controller, it doesn't matter which):
devise_group :blogger, contains: [:user, :admin]
Generated methods:
authenticate_blogger! # Redirects unless user or admin are signed in
blogger_signed_in? # Checks whether there is either a user or an admin signed in
current_blogger # Currently signed in user or admin
current_bloggers # Currently signed in user and admin
Use:
before_action :authenticate_blogger! # Redirects unless either a user or an admin are authenticated
before_action ->{ authenticate_blogger! :admin } # Redirects to the admin login page
current_blogger :user # Preferably returns a User if one is signed in
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/devise/controllers/helpers.rb', line 39 def devise_group(group_name, opts = {}) mappings = "[#{ opts[:contains].map { |m| ":#{m}" }.join(',') }]" class_eval <<-METHODS, __FILE__, __LINE__ + 1 def authenticate_#{group_name}!(favorite = nil, opts = {}) unless #{group_name}_signed_in? mappings = #{mappings} mappings.unshift mappings.delete(favorite.to_sym) if favorite mappings.each do |mapping| opts[:scope] = mapping opts[:locale] = I18n.locale warden.authenticate!(opts) if !devise_controller? || opts.delete(:force) end end end def #{group_name}_signed_in? #{mappings}.any? do |mapping| warden.authenticate?(scope: mapping) end end def current_#{group_name}(favorite = nil) mappings = #{mappings} mappings.unshift mappings.delete(favorite.to_sym) if favorite mappings.each do |mapping| current = warden.authenticate(scope: mapping) return current if current end nil end def current_#{group_name.to_s.pluralize} #{mappings}.map do |mapping| warden.authenticate(scope: mapping) end.compact end if respond_to?(:helper_method) helper_method "current_#{group_name}", "current_#{group_name.to_s.pluralize}", "#{group_name}_signed_in?" end METHODS end |
#log_process_action(payload) ⇒ Object
83 84 85 86 |
# File 'lib/devise/controllers/helpers.rb', line 83 def log_process_action(payload) payload[:status] ||= 401 unless payload[:exception] super end |