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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 36cb9bc71fad9818f10c20ffe60bfc902cad923f
4
- data.tar.gz: 228d7b9633467284d37e210369492f28069c97df
3
+ metadata.gz: fc80b6979512a9246224aa4d68f4dbda8b0c70f8
4
+ data.tar.gz: 71aadf8f167f0f0350f0c0c3b89743753fe8ef78
5
5
  SHA512:
6
- metadata.gz: ad6b5a8dd18a8821a7ebb4798043e91caea257c403297d3d6df7ee1757c174567bc8d24a2c3650987c22beb90185172ebd4dec41476c9d87c7924870377ebae8
7
- data.tar.gz: 0d4940d8440b364b9211d26f75e75e9b62d97229d0fc2e8fd2e4006639733d9481d6f832ae520ac2376b9e7bca327d5906287a1f973a42a0b616ddd9f0ac3631
6
+ metadata.gz: c6e774897e0fc2b2b7a4bb2044ce22c6ea32d6263d20cf9ab441dc5a7d83dc6b218fcce333b661076a06c669047ec951671aa9c47f0bdf30c6b833465e60e080
7
+ data.tar.gz: 503223df0e48c6afce6e0d830a28ffac72bf55965e0db6ac7d4c8a439ef422a46bad0bbce06a6cfaae560a540c2ded2788f5919c81eec9c3f91dc595f5d94abd
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## 0.3.1
2
+
3
+ - Fixed `stack level too deep` error
4
+
1
5
  ## 0.3.0
2
6
 
3
7
  - Fixed compatibility with Clearance
@@ -1,3 +1,3 @@
1
1
  module Pretender
2
- VERSION = "0.3.0"
2
+ VERSION = "0.3.1"
3
3
  end
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
- def impersonates(scope = :user, opts = {})
8
- impersonated_method = opts[:method] || :"current_#{scope}"
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
- }
14
- true_method = :"true_#{scope}"
15
- session_key = :"impersonated_#{scope}_id"
16
- impersonated_var = :"@impersonated_#{scope}"
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
- # define methods
19
- if method_defined?(impersonated_method) || private_method_defined?(impersonated_method)
20
- alias_method true_method, impersonated_method
21
- else
22
- define_method true_method do
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
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
- end
29
- helper_method(true_method) if respond_to?(:helper_method)
30
+ helper_method(true_method) if respond_to?(:helper_method)
30
31
 
31
- define_method impersonated_method do
32
- unless instance_variable_get(impersonated_var)
33
- # only fetch impersonation if user is logged in and impersonation_id exists
34
- true_resource = send(true_method)
35
- if session[session_key] && !true_resource
36
- session[session_key] = nil
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
- value = (session[session_key] && impersonate_with.call(session[session_key])) || true_resource
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
- define_method :"impersonate_#{scope}" do |resource|
45
- instance_variable_set(impersonated_var, resource)
46
- session[session_key] = resource.id
47
- end
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
- define_method :"stop_impersonating_#{scope}" do
50
- instance_variable_set(impersonated_var, nil)
51
- session[session_key] = nil
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.0
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 00:00:00.000000000 Z
11
+ date: 2017-06-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionpack