pretender 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/lib/pretender/version.rb +1 -1
- data/lib/pretender.rb +41 -39
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fc80b6979512a9246224aa4d68f4dbda8b0c70f8
|
4
|
+
data.tar.gz: 71aadf8f167f0f0350f0c0c3b89743753fe8ef78
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c6e774897e0fc2b2b7a4bb2044ce22c6ea32d6263d20cf9ab441dc5a7d83dc6b218fcce333b661076a06c669047ec951671aa9c47f0bdf30c6b833465e60e080
|
7
|
+
data.tar.gz: 503223df0e48c6afce6e0d830a28ffac72bf55965e0db6ac7d4c8a439ef422a46bad0bbce06a6cfaae560a540c2ded2788f5919c81eec9c3f91dc595f5d94abd
|
data/CHANGELOG.md
CHANGED
data/lib/pretender/version.rb
CHANGED
data/lib/pretender.rb
CHANGED
@@ -4,55 +4,57 @@ require "active_support"
|
|
4
4
|
module Pretender
|
5
5
|
class Error < StandardError; end
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
7
|
+
module Methods
|
8
|
+
def impersonates(scope = :user, opts = {})
|
9
|
+
impersonated_method = opts[:method] || :"current_#{scope}"
|
10
|
+
impersonate_with = opts[:with] || proc { |id|
|
11
|
+
klass = scope.to_s.classify.constantize
|
12
|
+
primary_key = klass.respond_to?(:primary_key) ? klass.primary_key : :id
|
13
|
+
klass.find_by(primary_key => id)
|
14
|
+
}
|
15
|
+
true_method = :"true_#{scope}"
|
16
|
+
session_key = :"impersonated_#{scope}_id"
|
17
|
+
impersonated_var = :"@impersonated_#{scope}"
|
17
18
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
19
|
+
# define methods
|
20
|
+
if method_defined?(impersonated_method) || private_method_defined?(impersonated_method)
|
21
|
+
alias_method true_method, impersonated_method
|
22
|
+
else
|
23
|
+
sc = superclass
|
24
|
+
define_method true_method do
|
25
|
+
# TODO handle private methods
|
26
|
+
raise Pretender::Error, "#{impersonated_method} must be defined before the impersonates method" unless sc.method_defined?(impersonated_method)
|
27
|
+
sc.instance_method(impersonated_method).bind(self).call
|
28
|
+
end
|
27
29
|
end
|
28
|
-
|
29
|
-
helper_method(true_method) if respond_to?(:helper_method)
|
30
|
+
helper_method(true_method) if respond_to?(:helper_method)
|
30
31
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
32
|
+
define_method impersonated_method do
|
33
|
+
unless instance_variable_get(impersonated_var)
|
34
|
+
# only fetch impersonation if user is logged in and impersonation_id exists
|
35
|
+
true_resource = send(true_method)
|
36
|
+
if session[session_key] && !true_resource
|
37
|
+
session[session_key] = nil
|
38
|
+
end
|
39
|
+
value = (session[session_key] && impersonate_with.call(session[session_key])) || true_resource
|
40
|
+
instance_variable_set(impersonated_var, value) if value
|
37
41
|
end
|
38
|
-
|
39
|
-
instance_variable_set(impersonated_var, value) if value
|
42
|
+
instance_variable_get(impersonated_var)
|
40
43
|
end
|
41
|
-
instance_variable_get(impersonated_var)
|
42
|
-
end
|
43
44
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
45
|
+
define_method :"impersonate_#{scope}" do |resource|
|
46
|
+
instance_variable_set(impersonated_var, resource)
|
47
|
+
session[session_key] = resource.id
|
48
|
+
end
|
48
49
|
|
49
|
-
|
50
|
-
|
51
|
-
|
50
|
+
define_method :"stop_impersonating_#{scope}" do
|
51
|
+
instance_variable_set(impersonated_var, nil)
|
52
|
+
session[session_key] = nil
|
53
|
+
end
|
52
54
|
end
|
53
55
|
end
|
54
56
|
end
|
55
57
|
|
56
58
|
ActiveSupport.on_load(:action_controller) do
|
57
|
-
extend Pretender
|
59
|
+
extend Pretender::Methods
|
58
60
|
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.3.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Kane
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-06-
|
11
|
+
date: 2017-06-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: actionpack
|