switch_user 1.5.2 → 1.5.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2bfca8e1275bfff1ea79c5940ccbc7a0ca5230eaeaa0813e9bf8e6cb766d5996
4
- data.tar.gz: 5a4a3a7c8dfa3095621294521224f779030171f07503fac7f6774d3564e4d715
3
+ metadata.gz: 4211c3d6aa5b7360b61069cc07798462adf5341a7d139809c8435de17854b617
4
+ data.tar.gz: b63ea0d7464484cbb099153c2f96f4a100f907a719647dd8f45d8461455c8afe
5
5
  SHA512:
6
- metadata.gz: 786b82c5c9e76a77a44adaaaba5c98cb629b966c10e663487eba47f6b1190ed61e9c15a826be7ad010c4c61227f5dd8f112ccdd76478519153e6bbabab6a8054
7
- data.tar.gz: df4c18185eaf7819d3121b681c244f4df0524e8622c2c7710a964683ec678e6d558bf108d31b4396f5756fedb99bdfab2b5c4aa34b256467a3e5fe4d7d3c44d3
6
+ metadata.gz: 1286a0855b08342856252ac8b7db07cf9cbb0c65b01c702c4d8aac7d59ace250bf51957606183843ea5fb3d0be1c312f2de2bcc58cedb7c494f6f85b69c8b50c
7
+ data.tar.gz: d52d65016d91198f69e78dedc9dfff53c788debe24a589dfc98619621ad054abf774f31df87289dd067fff948130fe5126c17737f1d45a44f8e667ce0a10b35f
@@ -1,5 +1,9 @@
1
1
  # Next Release
2
2
 
3
+ ## 1.5.3 (10/20/2020)
4
+
5
+ * Fix zeitwerk autloading issue
6
+
3
7
  ## 1.5.2 (03/01/2019)
4
8
 
5
9
  * Clearance has moved their controller methods to be private
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # switch_user
2
2
 
3
- [![Build Status](https://secure.travis-ci.org/flyerhzm/switch_user.png)](http://travis-ci.org/flyerhzm/switch_user)
3
+ [![Build Status](https://secure.travis-ci.org/flyerhzm/switch_user.svg)](http://travis-ci.org/flyerhzm/switch_user)
4
4
  [![AwesomeCode Status for flyerhzm/switch_user](https://awesomecode.io/projects/5f029f36-047b-4332-8405-9c7913b195bc/status)](https://awesomecode.io/repos/flyerhzm/switch_user)
5
5
 
6
6
  Inspired from [hobo][0], switch_user provides a convenient way to switch current user without needing to log out and log in manually.
@@ -15,7 +15,7 @@ switch_user is very useful in such use cases
15
15
 
16
16
  ## Example
17
17
 
18
- Visit here: <http://switch-user-example.heroku.com/admin>, switch the current user in the select box.
18
+ Visit here: <http://switch-user-example.herokuapp.com/admin>, switch the current user in the select box.
19
19
 
20
20
  And source code here: <https://github.com/flyerhzm/switch_user_example>
21
21
 
@@ -2,33 +2,30 @@
2
2
 
3
3
  module SwitchUserHelper
4
4
  SelectOption = Struct.new(:label, :scope_id)
5
+
5
6
  def switch_user_select(options = {})
6
7
  return unless available?
7
8
 
8
9
  selected_user = nil
9
10
 
10
- grouped_options_container = {}.tap do |h|
11
- SwitchUser.all_users.each do |record|
12
- scope = record.is_a?(SwitchUser::GuestRecord) ? :Guest : record.scope.to_s.capitalize
13
- h[scope] ||= []
14
- h[scope] << [record.label, record.scope_id]
11
+ grouped_options_container =
12
+ {}.tap do |h|
13
+ SwitchUser.all_users.each do |record|
14
+ scope = record.is_a?(SwitchUser::GuestRecord) ? :Guest : record.scope.to_s.capitalize
15
+ h[scope] ||= []
16
+ h[scope] << [record.label, record.scope_id]
17
+
18
+ next unless selected_user.nil?
19
+ next if record.is_a?(SwitchUser::GuestRecord)
15
20
 
16
- next unless selected_user.nil?
17
- next if record.is_a?(SwitchUser::GuestRecord)
18
- if provider.current_user?(record.user, record.scope)
19
- selected_user = record.scope_id
21
+ selected_user = record.scope_id if provider.current_user?(record.user, record.scope)
20
22
  end
21
23
  end
22
- end
23
24
 
24
25
  option_tags = grouped_options_for_select(grouped_options_container.to_a, selected_user)
25
26
 
26
27
  render partial: 'switch_user/widget',
27
- locals: {
28
- option_tags: option_tags,
29
- classes: options[:class],
30
- styles: options[:style]
31
- }
28
+ locals: { option_tags: option_tags, classes: options[:class], styles: options[:style] }
32
29
  end
33
30
 
34
31
  private
@@ -3,7 +3,7 @@
3
3
  module SwitchUser
4
4
  module Generators
5
5
  class InstallGenerator < Rails::Generators::Base
6
- TEMPLATES_PATH = File.expand_path('../templates', __FILE__)
6
+ TEMPLATES_PATH = File.expand_path('templates', __dir__)
7
7
  source_root File.expand_path(Engine.root, __FILE__)
8
8
 
9
9
  def install_initializer
@@ -23,7 +23,7 @@ module SwitchUser
23
23
  mattr_accessor :login_exclusive
24
24
  mattr_accessor :controller_guard
25
25
  mattr_accessor :view_guard
26
- mattr_reader :guard_class
26
+ mattr_reader :guard_class
27
27
 
28
28
  def self.setup
29
29
  yield self
@@ -42,11 +42,12 @@ module SwitchUser
42
42
  end
43
43
 
44
44
  def self.data_sources
45
- sources = available_users.map do |scope, loader|
46
- identifier = available_users_identifiers.fetch(scope)
47
- name = available_users_names.fetch(scope)
48
- DataSource.new(loader, scope, identifier, name)
49
- end
45
+ sources =
46
+ available_users.map do |scope, loader|
47
+ identifier = available_users_identifiers.fetch(scope)
48
+ name = available_users_names.fetch(scope)
49
+ DataSource.new(loader, scope, identifier, name)
50
+ end
50
51
  sources.unshift(GuestDataSource.new) if helper_with_guest
51
52
  DataSources.new(sources)
52
53
  end
@@ -5,9 +5,9 @@ module SwitchUser
5
5
  # TODO: is this the best arguments for the initializer ?
6
6
  # TODO should @provider be set and current/original_user be added as # accessors ?
7
7
  def initialize(controller, provider)
8
- @controller = controller
9
- @request = controller.request
10
- @current_user = provider.current_user
8
+ @controller = controller
9
+ @request = controller.request
10
+ @current_user = provider.current_user
11
11
  @original_user = provider.original_user
12
12
  end
13
13
 
@@ -12,11 +12,12 @@ module SwitchUser
12
12
  autoload :Session, 'switch_user/provider/session'
13
13
 
14
14
  def self.init(controller)
15
- klass_part = if SwitchUser.provider.is_a?(Hash)
16
- SwitchUser.provider[:name]
17
- else
18
- SwitchUser.provider
19
- end
15
+ klass_part =
16
+ if SwitchUser.provider.is_a?(Hash)
17
+ SwitchUser.provider[:name]
18
+ else
19
+ SwitchUser.provider
20
+ end
20
21
 
21
22
  klass_part = klass_part.to_s.classify
22
23
 
@@ -33,14 +33,12 @@ module SwitchUser
33
33
  def original_user
34
34
  user_identifier = @controller.session[:original_user_scope_identifier]
35
35
 
36
- if user_identifier
37
- UserLoader.prepare(scope_identifier: user_identifier).user
38
- end
36
+ UserLoader.prepare(scope_identifier: user_identifier).user if user_identifier
39
37
  end
40
38
 
41
39
  def original_user=(user)
42
- user_type = user.class.to_s.underscore
43
- column_name = SwitchUser.available_users_identifiers[user_type.to_sym]
40
+ user_type = user.class.to_s.underscore
41
+ column_name = SwitchUser.available_users_identifiers[user_type.to_sym]
44
42
  user_identifier = "#{user_type}_#{user.send(column_name)}"
45
43
 
46
44
  @controller.session[:original_user_scope_identifier] = user_identifier
@@ -24,9 +24,8 @@ module SwitchUser
24
24
  end
25
25
 
26
26
  def restore_original_user_identifier
27
- if @original_user_scope_identifier
28
- @controller.session[:original_user_scope_identifier] = @original_user_scope_identifier
29
- end
27
+ @controller.session[:original_user_scope_identifier] =
28
+ @original_user_scope_identifier if @original_user_scope_identifier
30
29
  end
31
30
 
32
31
  def current_user(_scope = nil)
@@ -3,8 +3,10 @@
3
3
  module SwitchUser
4
4
  class Engine < Rails::Engine
5
5
  initializer 'switch_user.view' do
6
- ActiveSupport.on_load(:action_view) do
7
- include SwitchUserHelper
6
+ config.to_prepare do
7
+ ActiveSupport.on_load(:action_view) do
8
+ include SwitchUserHelper
9
+ end
8
10
  end
9
11
  end
10
12
  end
@@ -15,25 +15,25 @@ module SwitchUser
15
15
 
16
16
  _user_scope = _user_scope.to_s
17
17
 
18
- unless SwitchUser.available_scopes.include?(_user_scope) || SwitchUser.available_scopes.include?(_user_scope.to_sym)
19
- raise SwitchUser::InvalidScope, "don't allow this user sign in, please check config.available_users"
20
- end
18
+ raise SwitchUser::InvalidScope,
19
+ "don't allow this user sign in, please check config.available_users" unless SwitchUser.available_scopes
20
+ .include?(_user_scope) || SwitchUser.available_scopes.include?(_user_scope.to_sym)
21
21
 
22
22
  _user_id =
23
23
  case user_record_or_scope
24
24
  when ActiveRecord::Base
25
- identifier = SwitchUser.available_users_identifiers[_user_scope] || SwitchUser.available_users_identifiers[_user_scope.to_sym]
26
- if identifier.nil?
27
- raise SwitchUser::InvalidScope, "don't allow switch this user, please check config.available_users_identifiers"
28
- end
25
+ identifier =
26
+ SwitchUser.available_users_identifiers[_user_scope] ||
27
+ SwitchUser.available_users_identifiers[_user_scope.to_sym]
28
+ raise SwitchUser::InvalidScope,
29
+ "don't allow switch this user, please check config.available_users_identifiers" if identifier.nil?
30
+
29
31
  user_record_or_scope.send identifier
30
32
  else
31
33
  user_id
32
34
  end
33
35
 
34
- if _user_id.to_s.empty?
35
- raise InvalidArgument, "don't allow switch this user, user_id is empty"
36
- end
36
+ raise InvalidArgument, "don't allow switch this user, user_id is empty" if _user_id.to_s.empty?
37
37
 
38
38
  scope_identifier = "#{_user_scope}_#{_user_id}"
39
39
 
@@ -5,7 +5,7 @@ module SwitchUser
5
5
  def self.init_from_config
6
6
  SwitchUser.available_users.map do |scope, base_scope|
7
7
  identifier = SwitchUser.available_users_identifiers[scope]
8
- label = SwitchUser.available_users_names[scope]
8
+ label = SwitchUser.available_users_names[scope]
9
9
  new(scope, identifier, label, base_scope)
10
10
  end
11
11
  end
@@ -17,17 +17,19 @@ module SwitchUser
17
17
  end
18
18
 
19
19
  attr_reader :scope, :user_class, :identifier, :label, :base_scope
20
+
20
21
  def initialize(scope, identifier, label, base_scope)
21
- @scope = scope
22
+ @scope = scope
22
23
  @user_class = normalize_class(scope)
23
24
  @identifier = identifier
24
- @label = label
25
+ @label = label
25
26
  @base_scope = normalize_scope(base_scope)
26
27
  end
27
28
 
28
29
  def find_user(id)
29
30
  Record.build(users.where(id: id).first, self)
30
31
  end
32
+
31
33
  alias [] find_user
32
34
 
33
35
  def users
@@ -56,9 +58,10 @@ module SwitchUser
56
58
  user_class.respond_to?(:scoped) ? user_class.scoped : user_class.all
57
59
  end
58
60
  end
61
+
59
62
  class Record < Struct.new(:id, :label, :scope)
60
63
  def self.build(user, set)
61
- id = user.send(set.identifier)
64
+ id = user.send(set.identifier)
62
65
  label = user.send(set.label)
63
66
  scope = set.scope
64
67
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SwitchUser
4
- VERSION = '1.5.2'
4
+ VERSION = '1.5.3'
5
5
  end
@@ -10,11 +10,7 @@ RSpec.describe SwitchUserController, type: :controller do
10
10
  end
11
11
 
12
12
  let(:admin) { double(:admin, admin?: true) }
13
- let(:provider) {
14
- double(:provider,
15
- original_user: admin,
16
- current_user: nil)
17
- }
13
+ let(:provider) { double(:provider, original_user: admin, current_user: nil) }
18
14
  describe '#set_current_user' do
19
15
  it 'redirects the user to the specified location' do
20
16
  SwitchUser.redirect_path = ->(_, _) { '/path' }
@@ -26,14 +22,14 @@ RSpec.describe SwitchUserController, type: :controller do
26
22
 
27
23
  it 'denies access according to the guard block' do
28
24
  SwitchUser.controller_guard = ->(_, _, _) { false }
29
- expect {
30
- get :set_current_user
31
- }.to raise_error(ActionController::RoutingError)
25
+ expect { get :set_current_user }.to raise_error(ActionController::RoutingError)
32
26
  end
33
27
 
34
28
  describe 'requests with a privileged original_user' do
35
29
  before do
36
- SwitchUser.controller_guard = ->(current_user, _, original_user) { current_user.try(:admin?) || original_user.try(:admin?) }
30
+ SwitchUser.controller_guard = lambda do |current_user, _, original_user|
31
+ current_user.try(:admin?) || original_user.try(:admin?)
32
+ end
37
33
  end
38
34
  it 'allows access using the original_user param' do
39
35
  allow(controller).to receive(:provider).and_return(provider)
@@ -11,10 +11,10 @@ RSpec.describe SwitchUserHelper, type: :helper do
11
11
 
12
12
  let(:user) { double(:user, id: 1) }
13
13
  let(:admin) { double(:admin, id: 1) }
14
- let(:provider) {
14
+ let(:provider) do
15
15
  _provider = SwitchUser::Provider::Dummy.new(controller)
16
16
  _provider
17
- }
17
+ end
18
18
 
19
19
  describe '#switch_user_select' do
20
20
  let(:guest_record) { SwitchUser::GuestRecord.new }
@@ -23,9 +23,13 @@ RSpec.describe SwitchUserHelper, type: :helper do
23
23
 
24
24
  let(:guest_option_tags) { '<optgroup label="Guest"><option value="">Guest</option></optgroup>' }
25
25
  let(:user_option_tags) { '<optgroup label="User"><option value="user_1">user1</option></optgroup>' }
26
- let(:user_selected_option_tags) { '<optgroup label="User"><option selected="selected" value="user_1">user1</option></optgroup>' }
26
+ let(:user_selected_option_tags) do
27
+ '<optgroup label="User"><option selected="selected" value="user_1">user1</option></optgroup>'
28
+ end
27
29
  let(:admin_option_tags) { '<optgroup label="Admin"><option value="admin_1">admin1</option></optgroup>' }
28
- let(:admin_selected_option_tags) { '<optgroup label="Admin"><option selected="selected" value="admin_1">admin1</option></optgroup>' }
30
+ let(:admin_selected_option_tags) do
31
+ '<optgroup label="Admin"><option selected="selected" value="admin_1">admin1</option></optgroup>'
32
+ end
29
33
 
30
34
  before do
31
35
  allow(SwitchUser).to receive(:switch_back).and_return(false)
@@ -29,7 +29,9 @@ RSpec.describe 'Using SwitchUser', type: :request do
29
29
  context 'using switch_back' do
30
30
  before do
31
31
  SwitchUser.switch_back = true
32
- SwitchUser.controller_guard = ->(current_user, _request, original_user) { current_user && current_user.admin? || original_user && original_user.admin? }
32
+ SwitchUser.controller_guard = lambda do |current_user, _request, original_user|
33
+ current_user && current_user.admin? || original_user && original_user.admin?
34
+ end
33
35
  end
34
36
 
35
37
  it 'can switch back to a different user through remember_user endpoint' do
@@ -8,6 +8,7 @@ class FakeWardenSessionSerializer
8
8
 
9
9
  def store(user, scope)
10
10
  return unless user
11
+
11
12
  user_hash[scope] = user
12
13
  end
13
14
  end
@@ -3,8 +3,7 @@
3
3
  require 'spec_helper'
4
4
  require 'switch_user/provider/dummy'
5
5
 
6
- class SessionController < TestController
7
- end
6
+ class SessionController < TestController; end
8
7
 
9
8
  RSpec.describe SwitchUser::Provider::Session do
10
9
  let(:controller) { SessionController.new }
@@ -5,7 +5,7 @@ require 'switch_user/provider/session'
5
5
 
6
6
  class SessionController < TestController
7
7
  def current_user
8
- User.find_by_id(session[:uid]) if session[:uid]
8
+ User.find_by(id: session[:uid]) if session[:uid]
9
9
  end
10
10
  end
11
11
 
@@ -10,7 +10,7 @@ class ApplicationController < ActionController::Base
10
10
  end
11
11
 
12
12
  def current_user
13
- User.find_by_id(session[SwitchUser.session_key])
13
+ User.find_by(id: session[SwitchUser.session_key])
14
14
  end
15
15
 
16
16
  def login
@@ -66,6 +66,7 @@ module MyApp
66
66
  config.action_dispatch.show_exceptions = false
67
67
  end
68
68
  end
69
+
69
70
  Rails.application.initialize!
70
71
  Rails.application.routes.draw do
71
72
  get 'dummy/protected', to: 'dummy#protected'
@@ -83,12 +84,10 @@ connection.create_table :users do |t|
83
84
  t.column :admin, :boolean
84
85
  end
85
86
 
86
- class User < ActiveRecord::Base
87
- end
87
+ class User < ActiveRecord::Base; end
88
88
 
89
89
  connection.create_table :clients do |t|
90
90
  t.column :email, :string
91
91
  end
92
92
 
93
- class Client < ActiveRecord::Base
94
- end
93
+ class Client < ActiveRecord::Base; end
@@ -27,9 +27,7 @@ RSpec.describe SwitchUser::UserLoader do
27
27
  expect(loaded_user).to eq user
28
28
  end
29
29
  it 'raises an error for an invalid scope' do
30
- expect {
31
- loaded_user = SwitchUser::UserLoader.prepare(nil, '1')
32
- }.to raise_error(SwitchUser::InvalidScope)
30
+ expect { loaded_user = SwitchUser::UserLoader.prepare(nil, '1') }.to raise_error(SwitchUser::InvalidScope)
33
31
  end
34
32
  end
35
33
 
@@ -44,7 +42,7 @@ RSpec.describe SwitchUser::UserLoader do
44
42
  it 'returns nil if no user is found' do
45
43
  loader = SwitchUser::UserLoader.new('user', 3)
46
44
 
47
- expect(User.find_by_id(3)).to be_nil
45
+ expect(User.find_by(id: 3)).to be_nil
48
46
  expect(loader.user).to eq nil
49
47
  end
50
48
 
@@ -14,7 +14,6 @@ Gem::Specification.new do |s|
14
14
  s.description = 'Easily switch current user to speed up development'
15
15
 
16
16
  s.required_rubygems_version = '>= 1.3.6'
17
- s.rubyforge_project = 'switch_user'
18
17
 
19
18
  s.add_development_dependency 'actionpack'
20
19
  s.add_development_dependency 'activerecord'
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: switch_user
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.2
4
+ version: 1.5.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Huang
8
8
  - Luke Cowell
9
- autorequire:
9
+ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2019-03-01 00:00:00.000000000 Z
12
+ date: 2020-10-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: actionpack
@@ -246,7 +246,7 @@ homepage: http://rubygems.org/gems/switch_user
246
246
  licenses:
247
247
  - MIT
248
248
  metadata: {}
249
- post_install_message:
249
+ post_install_message:
250
250
  rdoc_options: []
251
251
  require_paths:
252
252
  - lib
@@ -261,8 +261,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
261
261
  - !ruby/object:Gem::Version
262
262
  version: 1.3.6
263
263
  requirements: []
264
- rubygems_version: 3.0.1
265
- signing_key:
264
+ rubygems_version: 3.1.2
265
+ signing_key:
266
266
  specification_version: 4
267
267
  summary: Easily switch current user to speed up development
268
268
  test_files: []