Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added provider#current_users_without_scope

This provides us with a way to load up all current users for all scopes. This
is useful as seen in the SwitchUserHelper where we don't know which scope to
check.
  • Loading branch information...
commit 9d12f7d6b629a1514333ceb0e639835ace21268b 1 parent 7496d4e
@lcowell lcowell authored
View
8 app/helpers/switch_user_helper.rb
@@ -1,11 +1,11 @@
module SwitchUserHelper
def switch_user_select
if available?
- current_user = provider.current_user
options = ""
options += content_tag(:option, "Guest", :value => "", :selected => !current_user)
SwitchUser.available_users.each do |scope, user_proc|
+ current_user = provider.current_user(scope)
identifier = SwitchUser.available_users_identifiers[scope]
name = SwitchUser.available_users_names[scope]
@@ -32,11 +32,7 @@ def switch_user_select
end
def available?
- user = nil
- SwitchUser.available_users.keys.each do |scope|
- user = send("current_#{scope}")
- break if user
- end
+ user = provider.current_users_without_scope.first
SwitchUser.view_guard.call(user, request)
end
View
8 lib/switch_user/provider/base.rb
@@ -1,6 +1,13 @@
module SwitchUser
module Provider
class Base
+ def current_users_without_scope
+ SwitchUser.available_scopes.inject([]) do |users, scope|
+ user = current_user(scope)
+ users << user if user
+ users
+ end
+ end
def login_exclusive(user, args)
requested_scope = args.fetch(:scope, :user).to_sym
@@ -13,7 +20,6 @@ def login_exclusive(user, args)
end
end
end
-
end
end
end
View
11 spec/provider/devise_spec.rb
@@ -35,6 +35,7 @@ def current_admin
describe SwitchUser::Provider::Devise do
let(:controller) { DeviseController.new }
let(:provider) { SwitchUser::Provider::Devise.new(controller) }
+ let(:user) { stub(:user) }
it_behaves_like "a provider"
@@ -46,7 +47,6 @@ def current_admin
end
describe "#login_exclusive" do
- let(:user) { stub(:user) }
before do
SwitchUser.stub(:available_users => {:user => nil, :admin => nil})
provider.login(user, :admin)
@@ -62,4 +62,13 @@ def current_admin
end
end
+
+ describe "#all_current_users" do
+ it "pulls users from an alternate scope" do
+ SwitchUser.stub(:available_users => {:user => nil, :admin => nil})
+ provider.login(user, :admin)
+
+ provider.current_users_without_scope.should == [user]
+ end
+ end
end
View
6 spec/support/provider.rb
@@ -18,4 +18,10 @@
it "responds to login_exclusive" do
provider.should respond_to(:login_exclusive)
end
+
+ it "knows if there are any users logged in" do
+ provider.login(user)
+
+ provider.current_users_without_scope.should == [user]
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.