pretender 0.2.1 → 0.3.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: 60e5acb138e7418d36f5db31434a5804ccd72230
4
- data.tar.gz: 9d87f62ab46dd07471752f82d0a323f585c6ffda
3
+ metadata.gz: 36cb9bc71fad9818f10c20ffe60bfc902cad923f
4
+ data.tar.gz: 228d7b9633467284d37e210369492f28069c97df
5
5
  SHA512:
6
- metadata.gz: f1f77c7fb923d3ae2f896ac1d366ef3dcb8d0dc568aaa09ce1fd6dd5fa31ad3cde50f5e9df2712a2b7179483b316d5183b3ff0c32bd1876408a29da5409b82b1
7
- data.tar.gz: 1cbd2fe07379ed8f205d6d71e2ccd38fddd90b0766e8a266cccca25660f030652e2cd8ea1cfd5b1f2915e9032ec6e9e6d208b5a08d9f79764dddb6bb296a57ab
6
+ metadata.gz: ad6b5a8dd18a8821a7ebb4798043e91caea257c403297d3d6df7ee1757c174567bc8d24a2c3650987c22beb90185172ebd4dec41476c9d87c7924870377ebae8
7
+ data.tar.gz: 0d4940d8440b364b9211d26f75e75e9b62d97229d0fc2e8fd2e4006639733d9481d6f832ae520ac2376b9e7bca327d5906287a1f973a42a0b616ddd9f0ac3631
@@ -1,3 +1,9 @@
1
+ ## 0.3.0
2
+
3
+ - Fixed compatibility with Clearance
4
+ - Added support for Rails API
5
+ - Added support for custom primary key
6
+
1
7
  ## 0.2.1
2
8
 
3
9
  - Better error message
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 40 lines of code :-)
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 Admin::UsersController < ApplicationController
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
- Show when someone is signed in as another user in your application layout.
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", stop_impersonating_path %>
104
+ <%= link_to "Back to admin", stop_impersonating_users_path, method: :post %>
80
105
  <% end %>
81
106
  ```
82
107
 
@@ -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| scope.to_s.classify.constantize.where(:id => id).first }
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
- raise Pretender::Error, "#{impersonated_method} must be defined before the impersonates method" unless ActionController::Base.method_defined?(impersonated_method)
20
- ActionController::Base.instance_method(impersonated_method).bind(self).call
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 true_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
- ActionController::Base.send(:extend, Pretender)
57
+ extend Pretender
52
58
  end
@@ -1,3 +1,3 @@
1
1
  module Pretender
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3.0"
3
3
  end
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.2.1
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: 2016-07-07 00:00:00.000000000 Z
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.4.5.1
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