From cb1d6c4684371c1532433767e647ff7a04ac8bef Mon Sep 17 00:00:00 2001 From: Giovanni Caniato Date: Tue, 27 May 2014 10:48:34 +0200 Subject: [PATCH] moved helpers definition from routes to controllers --- lib/devise.rb | 13 -- lib/devise/controllers/helpers.rb | 113 ++++++++++-------- lib/devise/omniauth/url_helpers.rb | 3 - lib/devise/rails/routes.rb | 13 -- test/controllers/helpers_test.rb | 8 +- .../app/controllers/application_controller.rb | 2 + test/rails_app/config/routes.rb | 2 - 7 files changed, 66 insertions(+), 88 deletions(-) diff --git a/lib/devise.rb b/lib/devise.rb index 653e940f..153751ea 100644 --- a/lib/devise.rb +++ b/lib/devise.rb @@ -256,10 +256,6 @@ module Devise mattr_reader :mappings @@mappings = ActiveSupport::OrderedHash.new - # Store groups. - mattr_reader :groups - @@groups = {} - # Omniauth configurations. mattr_reader :omniauth_configs @@omniauth_configs = ActiveSupport::OrderedHash.new @@ -339,15 +335,6 @@ module Devise mapping end - # Adds a group to Devise. - def self.add_group(group_name, resources) - singular_sym = lambda { |thing| thing.to_s.singularize.to_sym } - - group_name = singular_sym.call(group_name) - Devise.groups[group_name] = resources.map!(&singular_sym) - @@helpers.each { |h| h.define_group_helpers(group_name) } - end - # Make Devise aware of an 3rd party Devise-module (like invitable). For convenience. # # == Options: diff --git a/lib/devise/controllers/helpers.rb b/lib/devise/controllers/helpers.rb index aee9571b..79b696ef 100644 --- a/lib/devise/controllers/helpers.rb +++ b/lib/devise/controllers/helpers.rb @@ -6,8 +6,12 @@ module Devise include Devise::Controllers::SignInOut include Devise::Controllers::StoreLocation - included do + included do |base| helper_method :warden, :signed_in?, :devise_controller? + + base.class_eval do + extend GroupHelpers + end end module ClassMethods @@ -68,67 +72,70 @@ module Devise end end - # 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: - # - # Group: - # blogger (contains User and 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_filter :authenticate_blogger! # Redirects unless either a user or an admin are authenticated - # before_filter ->{ authenticate_blogger! :admin } # Redirects to the admin login page - # current_blogger :user # Preferably returns a User if one is signed in - # - def self.define_group_helpers(group_name) - class_eval <<-METHODS, __FILE__, __LINE__ + 1 - def authenticate_#{group_name}!(favourite=nil, opts={}) - unless #{group_name}_signed_in? - mappings = Devise.groups[:#{group_name}] - mappings.unshift mappings.delete(favourite.to_sym) if favourite - mappings.each do |mapping| - opts[:scope] = mapping - warden.authenticate!(opts) if !devise_controller? || opts.delete(:force) + module GroupHelpers + # 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_helpers_for :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_filter :authenticate_blogger! # Redirects unless either a user or an admin are authenticated + # before_filter ->{ authenticate_blogger! :admin } # Redirects to the admin login page + # current_blogger :user # Preferably returns a User if one is signed in + # + def devise_group(group_name, opts={}) + opts[:contains].map! { |m| ":#{m}" } + mappings = "[#{ opts[:contains].join(',') }]" + + ActionController::Base.class_eval <<-METHODS, __FILE__, __LINE__ + 1 + def authenticate_#{group_name}!(favourite=nil, opts={}) + unless #{group_name}_signed_in? + mappings = #{mappings} + mappings.unshift mappings.delete(favourite.to_sym) if favourite + mappings.each do |mapping| + opts[:scope] = mapping + warden.authenticate!(opts) if !devise_controller? || opts.delete(:force) + end end end - end - def #{group_name}_signed_in? - Devise.groups[:#{group_name}].any? do |mapping| - warden.authenticate?(scope: mapping) + def #{group_name}_signed_in? + #{mappings}.any? do |mapping| + warden.authenticate?(scope: mapping) + end end - end - def current_#{group_name}(favourite=nil) - mappings = Devise.groups[:#{group_name}] - mappings.unshift(mappings.delete favourite.to_sym) if favourite - mappings.each do |mapping| - current = warden.authenticate(scope: mapping) - return current if current + def current_#{group_name}(favourite=nil) + mappings = #{mappings} + mappings.unshift mappings.delete(favourite.to_sym) if favourite + mappings.each do |mapping| + current = warden.authenticate(scope: mapping) + return current if current + end + nil end - nil - end - def current_#{group_name.to_s.pluralize} - records = [] - Devise.groups[:#{group_name}].each do |mapping| - records << warden.authenticate(scope: mapping) + def current_#{group_name.to_s.pluralize} + records = [] + #{mappings}.each do |mapping| + records << warden.authenticate(scope: mapping) + end + records.compact end - records.compact - end - METHODS - ActiveSupport.on_load(:action_controller) do - helper_method "current_#{group_name}", "current_#{group_name.to_s.pluralize}", "#{group_name}_signed_in?" + helper_method "current_#{group_name}", "current_#{group_name.to_s.pluralize}", "#{group_name}_signed_in?" + METHODS end end diff --git a/lib/devise/omniauth/url_helpers.rb b/lib/devise/omniauth/url_helpers.rb index 969ac468..dd123f6a 100644 --- a/lib/devise/omniauth/url_helpers.rb +++ b/lib/devise/omniauth/url_helpers.rb @@ -4,9 +4,6 @@ module Devise def self.define_helpers(mapping) end - def self.define_group_helpers(group_name) - end - def omniauth_authorize_path(resource_or_scope, *args) scope = Devise::Mapping.find_scope!(resource_or_scope) _devise_route_context.send("#{scope}_omniauth_authorize_path", *args) diff --git a/lib/devise/rails/routes.rb b/lib/devise/rails/routes.rb index 5c1080a8..2951b141 100644 --- a/lib/devise/rails/routes.rb +++ b/lib/devise/rails/routes.rb @@ -351,19 +351,6 @@ module ActionDispatch::Routing end alias :as :devise_scope - # Creates a group that can contain multiple scopes in order to abstract them - # over some functionality, the only thing it really does is defining helper methods. - # - # For example - # - # devise_group :bloggers, includes: [:users, :admins] - # - # will define: current_blogger, current_bloggers, authenticate_blogger! and blogger_signed_in? - # - def devise_group(group_name, opts={}) - Devise.add_group(group_name.to_s.singularize, opts[:includes]) - end - protected def devise_session(mapping, controllers) #:nodoc: diff --git a/test/controllers/helpers_test.rb b/test/controllers/helpers_test.rb index c691e590..0af99cbe 100644 --- a/test/controllers/helpers_test.rb +++ b/test/controllers/helpers_test.rb @@ -26,7 +26,7 @@ class ControllerAuthenticatableTest < ActionController::TestCase end test 'proxy [group]_signed_in? to authenticate? with each scope' do - Devise.groups[:commenter].each do |scope| + [:user, :admin].each do |scope| @mock_warden.expects(:authenticate?).with(scope: scope).returns(false) end @controller.commenter_signed_in? @@ -43,14 +43,14 @@ class ControllerAuthenticatableTest < ActionController::TestCase end test 'proxy current_[group] to authenticate with each scope' do - Devise.groups[:commenter].each do |scope| + [:user, :admin].each do |scope| @mock_warden.expects(:authenticate).with(scope: scope).returns(nil) end @controller.current_commenter end test 'proxy current_[plural_group] to authenticate with each scope' do - Devise.groups[:commenter].each do |scope| + [:user, :admin].each do |scope| @mock_warden.expects(:authenticate).with(scope: scope) end @controller.current_commenters @@ -77,7 +77,7 @@ class ControllerAuthenticatableTest < ActionController::TestCase end test 'proxy authenticate_[group]! to authenticate!? with each scope' do - Devise.groups[:commenter].each do |scope| + [:user, :admin].each do |scope| @mock_warden.expects(:authenticate!).with(scope: scope) @mock_warden.expects(:authenticate?).with(scope: scope).returns(false) end diff --git a/test/rails_app/app/controllers/application_controller.rb b/test/rails_app/app/controllers/application_controller.rb index a3153ffa..b6df55f4 100644 --- a/test/rails_app/app/controllers/application_controller.rb +++ b/test/rails_app/app/controllers/application_controller.rb @@ -6,4 +6,6 @@ class ApplicationController < ActionController::Base before_filter :current_user, unless: :devise_controller? before_filter :authenticate_user!, if: :devise_controller? respond_to *Mime::SET.map(&:to_sym) + + devise_group :commenter, contains: [:user, :admin] end diff --git a/test/rails_app/config/routes.rb b/test/rails_app/config/routes.rb index f218de1e..1ab9c290 100644 --- a/test/rails_app/config/routes.rb +++ b/test/rails_app/config/routes.rb @@ -104,7 +104,5 @@ Rails.application.routes.draw do get "/unauthenticated", to: "home#unauthenticated" get "/custom_strategy/new" - devise_group :commenters, includes: [:admins, :users] - root to: "home#index", via: [:get, :post] end