switch_user 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 703c786469cf8a796e024e2ec4cca3037d75c944
4
- data.tar.gz: dc4d80d1a338401476ef22205bfee5bc215b07ca
3
+ metadata.gz: 596db07135f6c4bdb1e194369e2adb8c8fdc63ce
4
+ data.tar.gz: 9eb590dff95b54d2800a93040f6fae5404a211b3
5
5
  SHA512:
6
- metadata.gz: df717cc06e736487dde734d3f19fb390c0a2ffde9f69adbbd3a0ba365912e9a2c0d211ef9f865ae5b0740cd1fe558c3c72dd85160bc0ba6ec344fc464c75b7a3
7
- data.tar.gz: c47e07ed11ff0bb6a0b0f7e6514e92f963c6eb5d526750d77136dff973571bd94635c9ac08faacccc2f6495f04742c4cb21f7df0f72d944a858a862a59d3b3c3
6
+ metadata.gz: 903229ce08e3bd292798005b756bdeef451f7c68efc639bd6c34d70c3a3e1e4815cfedc64d045dd46a32075a051319c4f0093e6463eca8ea83069c9d31649237
7
+ data.tar.gz: 5cff807ec71b15ff9b6bc135df6c408e17cd198742b2359f90ba908c3451a16765a9a3a7cdf84691201a93e5033b52f8413093506d38788a56def28f21113dcb
data/.gitignore CHANGED
@@ -2,6 +2,8 @@ pkg/*
2
2
  *.gem
3
3
  .bundle
4
4
  .rvmrc
5
+ .ruby-version
6
+ .versions.conf
5
7
  Gemfile.lock
6
8
  log/*.log
7
9
  .DS_Store
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Next Release
2
2
 
3
+ ## 1.2.0 (12/01/2015)
4
+
5
+ * Replace select option to `group_option`
6
+
3
7
  ## 1.1.0 (07/01/2015)
4
8
 
5
9
  * Rails 3.x compatibility
data/README.md CHANGED
@@ -62,7 +62,7 @@ SwitchUser.setup do |config|
62
62
  # available_users is a hash,
63
63
  # key is the model name of user (:user, :admin, or any name you use),
64
64
  # value is a block that return the users that can be switched.
65
- config.available_users = { :user => lambda { User.all } }
65
+ config.available_users = { :user => lambda { User.all } } # use User.scoped instead for rails 3.2
66
66
 
67
67
  # available_users_identifiers is a hash,
68
68
  # keys in this hash should match a key in the available_users hash
@@ -158,4 +158,4 @@ This feature should be used with extreme caution because of the security implica
158
158
 
159
159
  Copyright © 2010 - 2015 Richard Huang (flyerhzm@gmail.com), released under the MIT license
160
160
 
161
- [0]: https://github.com/tablatom/hobo
161
+ [0]: https://github.com/tablatom/hobo
@@ -4,15 +4,24 @@ module SwitchUserHelper
4
4
  return unless available?
5
5
 
6
6
  if provider.current_user
7
- selected_user = "user_#{current_user.id}"
7
+ selected_user = "user_#{provider.current_user.id}"
8
8
  else
9
9
  selected_user = nil
10
10
  end
11
11
 
12
+ grouped_options_container = {}.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
+ end
18
+ end
19
+
20
+ option_tags = grouped_options_for_select(grouped_options_container, selected_user)
21
+
12
22
  render :partial => "switch_user/widget",
13
23
  :locals => {
14
- :options => SwitchUser.all_users,
15
- :current_scope => selected_user
24
+ :option_tags => option_tags
16
25
  }
17
26
  end
18
27
 
@@ -1,4 +1,4 @@
1
1
  <% if SwitchUser.switch_back %>
2
2
  <%= check_box_tag "remember_user", "remember_user", provider.original_user.present?, :onchange => "location.href = '/switch_user/remember_user?remember=' + encodeURIComponent(this.checked)" %>
3
3
  <% end %>
4
- <%= select_tag "switch_user_identifier", options_from_collection_for_select(options, :scope_id, :label, current_scope), :onchange => "location.href = '/switch_user?scope_identifier=' + encodeURIComponent(this.options[this.selectedIndex].value)" %>
4
+ <%= select_tag "switch_user_identifier", option_tags, :onchange => "location.href = '/switch_user?scope_identifier=' + encodeURIComponent(this.options[this.selectedIndex].value)" %>
@@ -1,3 +1,3 @@
1
1
  module SwitchUser
2
- VERSION = "1.1.0"
2
+ VERSION = "1.2.0"
3
3
  end
@@ -0,0 +1,134 @@
1
+ require 'spec_helper'
2
+ require 'switch_user'
3
+ require 'switch_user_helper'
4
+
5
+ RSpec.describe SwitchUserHelper, :type => :helper do
6
+ before do
7
+ SwitchUser.provider = :dummy
8
+ end
9
+
10
+ let(:user) { double(:user, :id => 1) }
11
+ let(:provider) {
12
+ _provider = SwitchUser::Provider::Dummy.new(controller)
13
+ _provider.instance_variable_set(:@user, user)
14
+ _provider
15
+ }
16
+
17
+ describe "#switch_user_select" do
18
+ let(:guest_record) { SwitchUser::GuestRecord.new }
19
+ let(:user_record) { double(:user_record, :scope => :user, :label => 'user1', :scope_id => 'user_1') }
20
+
21
+ let(:guest_option_tags) { %Q^<optgroup label="Guest"><option value="">Guest</option></optgroup>^ }
22
+ let(:user_option_tags) { %Q^<optgroup label="User"><option value="user_1">user1</option></optgroup>^ }
23
+ let(:user_selected_option_tags) { %Q^<optgroup label="User"><option selected="selected" value="user_1">user1</option></optgroup>^ }
24
+
25
+
26
+ before do
27
+ allow(SwitchUser).to receive(:switch_back).and_return(false)
28
+
29
+ allow(helper).to receive(:available?).and_return(true)
30
+
31
+ allow(helper).to receive(:provider).and_return(provider)
32
+ allow(provider).to receive(:current_user).and_return(user)
33
+
34
+ allow(SwitchUser).to receive(:all_users).and_return([guest_record, user_record])
35
+ end
36
+
37
+ it "when unavailable" do
38
+ allow(helper).to receive(:available?).and_return(false)
39
+
40
+ expect(helper.switch_user_select).to eq(nil)
41
+ end
42
+
43
+ it "when current_user is nil and all_users is []" do
44
+ allow(provider).to receive(:current_user).and_return(nil)
45
+ allow(SwitchUser).to receive(:all_users).and_return([])
46
+
47
+ expect(helper.switch_user_select).not_to match(%r{</option>})
48
+ end
49
+
50
+ it "when current_user is nil and all_users is [guest_record]" do
51
+ allow(provider).to receive(:current_user).and_return(nil)
52
+ allow(SwitchUser).to receive(:all_users).and_return([guest_record])
53
+
54
+ expect(helper.switch_user_select).to match(%r{#{guest_option_tags}})
55
+ end
56
+
57
+ it "when current_user is nil and all_users is [guest_record, user_record]" do
58
+ allow(provider).to receive(:current_user).and_return(nil)
59
+ allow(SwitchUser).to receive(:all_users).and_return([guest_record, user_record])
60
+
61
+ expect(helper.switch_user_select).to match(%r{#{guest_option_tags}})
62
+ expect(helper.switch_user_select).to match(%r{#{user_option_tags}})
63
+ end
64
+
65
+ it "when current_user is user and all_users is []" do
66
+ allow(provider).to receive(:current_user).and_return(user)
67
+ allow(SwitchUser).to receive(:all_users).and_return([])
68
+
69
+ expect(helper.switch_user_select).not_to match(%r{</option>})
70
+ end
71
+
72
+ it "when current_user is user and all_users is [guest_record, user_record]" do
73
+ allow(provider).to receive(:current_user).and_return(user)
74
+ allow(SwitchUser).to receive(:all_users).and_return([guest_record, user_record])
75
+
76
+ expect(helper.switch_user_select).to match(%r{#{guest_option_tags}})
77
+ expect(helper.switch_user_select).to match(%r{#{user_selected_option_tags}})
78
+ end
79
+
80
+ it "when current_user is default allow and all_users is default allow" do
81
+ expect(helper.switch_user_select).to match(%r{#{guest_option_tags}})
82
+ expect(helper.switch_user_select).to match(%r{#{user_selected_option_tags}})
83
+ end
84
+
85
+ end
86
+
87
+ describe "#user_tag_value" do
88
+ it "for user" do
89
+ user = double(:user, :id => 1)
90
+
91
+ expect(helper.send(:user_tag_value, user, :id, :user)).to eq('user_1')
92
+ end
93
+ end
94
+
95
+ describe "#user_tag_label" do
96
+ it "when name has call method" do
97
+ user = double(:user)
98
+ name = ->(user){ 'user1' }
99
+
100
+ expect(helper.send(:user_tag_label, user, name)).to eq('user1')
101
+ end
102
+
103
+ it "when name not has call method" do
104
+ user = double(:name, :name => 'user1')
105
+ name = :name
106
+
107
+ expect(helper.send(:user_tag_label, user, name)).to eq('user1')
108
+ end
109
+ end
110
+
111
+ describe "#available?" do
112
+ it "return true" do
113
+ allow_any_instance_of(SwitchUser.guard_class).to receive(:view_available?).and_return(true)
114
+
115
+ expect(helper.send(:available?)).to eq(true)
116
+ end
117
+
118
+ it "return false" do
119
+ allow_any_instance_of(SwitchUser.guard_class).to receive(:view_available?).and_return(false)
120
+
121
+ expect(helper.send(:available?)).to eq(false)
122
+ end
123
+ end
124
+
125
+ describe "#provider" do
126
+ it "normal" do
127
+ allow(SwitchUser::Provider).to receive(:init).with(controller).and_return(provider)
128
+
129
+ expect(helper.send(:provider)).to eq(provider)
130
+ end
131
+ end
132
+
133
+
134
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: switch_user
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Huang
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-07-01 00:00:00.000000000 Z
12
+ date: 2015-12-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -206,6 +206,7 @@ files:
206
206
  - lib/switch_user/user_set.rb
207
207
  - lib/switch_user/version.rb
208
208
  - spec/controllers/switch_user_controller_spec.rb
209
+ - spec/helpers/switch_user_helper_spec.rb
209
210
  - spec/integration/switch_user_spec.rb
210
211
  - spec/provider/authlogic_spec.rb
211
212
  - spec/provider/clearance_spec.rb
@@ -243,7 +244,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
243
244
  version: 1.3.6
244
245
  requirements: []
245
246
  rubyforge_project: switch_user
246
- rubygems_version: 2.4.7
247
+ rubygems_version: 2.4.5.1
247
248
  signing_key:
248
249
  specification_version: 4
249
250
  summary: Easily switch current user to speed up development