pretender 0.2.1 → 0.3.0
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 +4 -4
- data/CHANGELOG.md +6 -0
- data/README.md +29 -4
- data/lib/pretender.rb +12 -6
- data/lib/pretender/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 36cb9bc71fad9818f10c20ffe60bfc902cad923f
|
4
|
+
data.tar.gz: 228d7b9633467284d37e210369492f28069c97df
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ad6b5a8dd18a8821a7ebb4798043e91caea257c403297d3d6df7ee1757c174567bc8d24a2c3650987c22beb90185172ebd4dec41476c9d87c7924870377ebae8
|
7
|
+
data.tar.gz: 0d4940d8440b364b9211d26f75e75e9b62d97229d0fc2e8fd2e4006639733d9481d6f832ae520ac2376b9e7bca327d5906287a1f973a42a0b616ddd9f0ac3631
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -8,7 +8,7 @@ As an admin, there are times you want to see exactly what another user sees. Me
|
|
8
8
|
|
9
9
|
:boom: [Rock on](http://www.youtube.com/watch?v=SBjQ9tuuTJQ)
|
10
10
|
|
11
|
-
Pretender is flexible and lightweight - less than
|
11
|
+
Pretender is flexible and lightweight - less than 60 lines of code :-)
|
12
12
|
|
13
13
|
Works with any authentication system - [Devise](https://github.com/plataformatec/devise), [Authlogic](https://github.com/binarylogic/authlogic), and [Sorcery](https://github.com/NoamB/sorcery) to name a few.
|
14
14
|
|
@@ -54,10 +54,16 @@ stop_impersonating_user
|
|
54
54
|
|
55
55
|
### Sample Implementation
|
56
56
|
|
57
|
+
Create a controller
|
58
|
+
|
57
59
|
```ruby
|
58
|
-
class
|
60
|
+
class UsersController < ApplicationController
|
59
61
|
before_filter :require_admin!
|
60
62
|
|
63
|
+
def index
|
64
|
+
@users = User.order(:id)
|
65
|
+
end
|
66
|
+
|
61
67
|
def impersonate
|
62
68
|
user = User.find(params[:id])
|
63
69
|
impersonate_user(user)
|
@@ -71,12 +77,31 @@ class Admin::UsersController < ApplicationController
|
|
71
77
|
end
|
72
78
|
```
|
73
79
|
|
74
|
-
|
80
|
+
Add routes
|
81
|
+
|
82
|
+
```ruby
|
83
|
+
resources :users, only: [:index] do
|
84
|
+
post :impersonate, on: :member
|
85
|
+
post :stop_impersonating, on: :collection
|
86
|
+
end
|
87
|
+
```
|
88
|
+
|
89
|
+
Create an index view
|
90
|
+
|
91
|
+
```erb
|
92
|
+
<ul>
|
93
|
+
<% @users.each do |user| %>
|
94
|
+
<li>Sign in as <%= link_to user.name, impersonate_user_path(user), method: :post %></li>
|
95
|
+
<% end %>
|
96
|
+
</ul>
|
97
|
+
```
|
98
|
+
|
99
|
+
And show when someone is signed in as another user in your application layout
|
75
100
|
|
76
101
|
```erb
|
77
102
|
<% if current_user != true_user %>
|
78
103
|
You (<%= true_user.name %>) are signed in as <%= current_user.name %>
|
79
|
-
<%= link_to "Back to admin",
|
104
|
+
<%= link_to "Back to admin", stop_impersonating_users_path, method: :post %>
|
80
105
|
<% end %>
|
81
106
|
```
|
82
107
|
|
data/lib/pretender.rb
CHANGED
@@ -6,21 +6,27 @@ module Pretender
|
|
6
6
|
|
7
7
|
def impersonates(scope = :user, opts = {})
|
8
8
|
impersonated_method = opts[:method] || :"current_#{scope}"
|
9
|
-
impersonate_with = opts[:with] || proc { |id|
|
9
|
+
impersonate_with = opts[:with] || proc { |id|
|
10
|
+
klass = scope.to_s.classify.constantize
|
11
|
+
primary_key = klass.respond_to?(:primary_key) ? klass.primary_key : :id
|
12
|
+
klass.find_by(primary_key => id)
|
13
|
+
}
|
10
14
|
true_method = :"true_#{scope}"
|
11
15
|
session_key = :"impersonated_#{scope}_id"
|
12
16
|
impersonated_var = :"@impersonated_#{scope}"
|
13
17
|
|
14
18
|
# define methods
|
15
|
-
if method_defined?(impersonated_method)
|
19
|
+
if method_defined?(impersonated_method) || private_method_defined?(impersonated_method)
|
16
20
|
alias_method true_method, impersonated_method
|
17
21
|
else
|
18
22
|
define_method true_method do
|
19
|
-
|
20
|
-
|
23
|
+
# TODO handle private methods
|
24
|
+
superclass = self.class.superclass
|
25
|
+
raise Pretender::Error, "#{impersonated_method} must be defined before the impersonates method" unless superclass.method_defined?(impersonated_method)
|
26
|
+
superclass.instance_method(impersonated_method).bind(self).call
|
21
27
|
end
|
22
28
|
end
|
23
|
-
helper_method
|
29
|
+
helper_method(true_method) if respond_to?(:helper_method)
|
24
30
|
|
25
31
|
define_method impersonated_method do
|
26
32
|
unless instance_variable_get(impersonated_var)
|
@@ -48,5 +54,5 @@ module Pretender
|
|
48
54
|
end
|
49
55
|
|
50
56
|
ActiveSupport.on_load(:action_controller) do
|
51
|
-
|
57
|
+
extend Pretender
|
52
58
|
end
|
data/lib/pretender/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pretender
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Kane
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-06-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: actionpack
|
@@ -104,7 +104,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
104
104
|
version: '0'
|
105
105
|
requirements: []
|
106
106
|
rubyforge_project:
|
107
|
-
rubygems_version: 2.
|
107
|
+
rubygems_version: 2.6.11
|
108
108
|
signing_key:
|
109
109
|
specification_version: 4
|
110
110
|
summary: Easy to switch back and forth between roles, minimal code changes, and plays
|