action_interceptor 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +27 -27
- data/lib/action_interceptor/common.rb +61 -0
- data/lib/action_interceptor/controller.rb +5 -55
- data/lib/action_interceptor/version.rb +1 -1
- data/lib/action_interceptor/view.rb +3 -55
- data/spec/lib/action_interceptor/controller_spec.rb +2 -0
- data/spec/lib/action_interceptor/view_spec.rb +6 -2
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 164253286739cc3f66b62a86b6b8c8650ebf441b
|
4
|
+
data.tar.gz: 96fc1302173b587524f3ede0cf8bfdeaf5953f5f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8d25e02cd76eca730ef751cc702305a67f14b32a5df1e2958fc52e26eaf4a600dca359430f304c9d3fffd2f060b97ab26664d7762ce02c881c24982f2df0e86c
|
7
|
+
data.tar.gz: b1063a7a075619f8e8fdd511cec3b738595b1f7de4421d1d9d1bb87616f1ef7da5f8b2f11500bf6d2a3a4cf727d102e494cbb46f4a7065dbce616f8f0a884ed5
|
data/README.md
CHANGED
@@ -87,19 +87,38 @@ end
|
|
87
87
|
As shown above, interceptions work like before_filters and
|
88
88
|
can be skipped using the skip_interceptor method.
|
89
89
|
|
90
|
-
|
90
|
+
Just by including the gem in your app, the following convenience methods
|
91
|
+
will also be added to all controllers as helper methods, so they will also
|
92
|
+
be available in views: `current_page?(url)`, `current_url`, `current_url_hash`,
|
93
|
+
`with_interceptor(&block)` and `without_interceptor(&block)`.
|
94
|
+
|
95
|
+
- `with_interceptor(&block)` executes the given block:
|
96
|
+
- Adding the intercepted URL param to all links and redirects
|
97
|
+
- As if it was declared in the context of `self`
|
98
|
+
- `without_interceptor(&block)` executes the given block:
|
99
|
+
- With the default URL params for all links and redirects
|
100
|
+
- As if it was declared in the context of `self`
|
101
|
+
|
102
|
+
- `current_url_hash` returns a hash containing the `intercepted_url_key` and the
|
103
|
+
`current_url`, signed and encrypted.
|
104
|
+
|
105
|
+
- And the following methods, backported from Rails 4:
|
106
|
+
- `current_url` returns the current url.
|
107
|
+
- `current_page?(url)` returns true iif the given url is the `current_url`.
|
108
|
+
|
109
|
+
When called, the `acts_as_interceptor` method will ensure the following:
|
91
110
|
|
92
111
|
- The `url_options` method for that controller will be overriden, causing all
|
93
112
|
links and redirects for the controller and associated views to include
|
94
|
-
the signed return url. This can be skipped by
|
95
|
-
|
96
|
-
|
97
|
-
links and
|
113
|
+
the signed return url. This behavior can be skipped by passing
|
114
|
+
`:override_url_options => false` to the `acts_as_interceptor` call,
|
115
|
+
like so: `acts_as_interceptor :override_url_options => false`.
|
116
|
+
In that case, you are responsible for wrapping any internal links and
|
117
|
+
redirects in `with_interceptor` blocks.
|
98
118
|
|
99
119
|
- The following convenience methods will be added to the controller:
|
100
|
-
`redirect_back(options = {})`, `intercepted_url`,
|
101
|
-
`
|
102
|
-
`url_options_without_interceptor` and `url_options_with_interceptor`.
|
120
|
+
`redirect_back(options = {})`, `intercepted_url`,
|
121
|
+
`intercepted_url=` and `intercepted_url_hash`.
|
103
122
|
These methods have the following behavior:
|
104
123
|
|
105
124
|
- redirect_back(options = {}) redirects the user back to where the
|
@@ -113,14 +132,6 @@ The `acts_as_interceptor` method will ensure the following:
|
|
113
132
|
- `intercepted_url_hash` returns a hash containing the `interceptor_url_key`
|
114
133
|
and the signed `intercepted_url`.
|
115
134
|
|
116
|
-
- `without_interceptor(&block)` executes a block with the old url options.
|
117
|
-
|
118
|
-
- `url_options_without_interceptor` returns the old url options.
|
119
|
-
|
120
|
-
- `url_options_with_interceptor` returns the old url options merged with
|
121
|
-
the `intercepted_url_hash`. Can be used even if you specified
|
122
|
-
`override_url_options: false`.
|
123
|
-
|
124
135
|
When users complete the given task, use the following method to
|
125
136
|
redirect them back to where the interception occurred:
|
126
137
|
|
@@ -134,17 +145,6 @@ Alternatively, you can use `intercepted_url` in views:
|
|
134
145
|
<%= link_to 'Back', intercepted_url %>
|
135
146
|
```
|
136
147
|
|
137
|
-
Finally, just by including the gem in your app, the following convenience
|
138
|
-
methods will be added to all controllers: `current_url`, `current_url_hash`,
|
139
|
-
`current_page?(url)` and `with_interceptor(&block)`.
|
140
|
-
|
141
|
-
- `current_url` returns the current url.
|
142
|
-
- `current_url_hash` returns a hash containing the `intercepted_url_key` and the
|
143
|
-
`current_url`, signed and encrypted.
|
144
|
-
- `current_page?(url)` returns true iif the given url is the `current_url`.
|
145
|
-
- `with_interceptor(&block)` executes the given block as if it was an
|
146
|
-
interceptor for the current controller.
|
147
|
-
|
148
148
|
## Contributing
|
149
149
|
|
150
150
|
1. Fork it
|
@@ -0,0 +1,61 @@
|
|
1
|
+
module ActionInterceptor
|
2
|
+
module Common
|
3
|
+
|
4
|
+
protected
|
5
|
+
|
6
|
+
def url_for(options = {})
|
7
|
+
url = super
|
8
|
+
return url unless use_interceptor
|
9
|
+
|
10
|
+
@interceptor_url_for_hash ||= is_interceptor ? \
|
11
|
+
intercepted_url_hash : current_url_hash
|
12
|
+
|
13
|
+
uri = URI(url)
|
14
|
+
new_query = URI.decode_www_form(uri.query || '') + \
|
15
|
+
@interceptor_url_for_hash.to_a
|
16
|
+
uri.query = URI.encode_www_form(new_query)
|
17
|
+
uri.to_s
|
18
|
+
end
|
19
|
+
|
20
|
+
# Executes the given block as if it was inside an interceptor
|
21
|
+
def with_interceptor(&block)
|
22
|
+
previous_use_interceptor = use_interceptor
|
23
|
+
|
24
|
+
begin
|
25
|
+
# Send the referer with intercepted requests
|
26
|
+
# So we don't rely on the user's browser to do it for us
|
27
|
+
self.use_interceptor = true
|
28
|
+
|
29
|
+
# Execute the block as if it was defined in this controller
|
30
|
+
instance_exec &block
|
31
|
+
rescue LocalJumpError => e
|
32
|
+
# Silently ignore `return` errors in the block
|
33
|
+
# and return the given value
|
34
|
+
e.exit_value
|
35
|
+
ensure
|
36
|
+
self.use_interceptor = previous_use_interceptor
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
# Executes the given block as if it was not inside an interceptor
|
41
|
+
def without_interceptor(&block)
|
42
|
+
previous_use_interceptor = use_interceptor
|
43
|
+
|
44
|
+
begin
|
45
|
+
# Send the referer with intercepted requests
|
46
|
+
# So we don't rely on the user's browser to do it for us
|
47
|
+
self.use_interceptor = false
|
48
|
+
|
49
|
+
# Execute the block as if it was defined in this controller
|
50
|
+
instance_exec &block
|
51
|
+
rescue LocalJumpError => e
|
52
|
+
# Silently ignore `return` errors in the block
|
53
|
+
# and return the given value
|
54
|
+
e.exit_value
|
55
|
+
ensure
|
56
|
+
self.use_interceptor = previous_use_interceptor
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
end
|
@@ -1,9 +1,12 @@
|
|
1
|
+
require 'action_interceptor/common'
|
1
2
|
require 'action_interceptor/encryptor'
|
2
3
|
require 'action_interceptor/undefined_interceptor'
|
3
4
|
|
4
5
|
module ActionInterceptor
|
5
6
|
module Controller
|
6
7
|
|
8
|
+
include Common
|
9
|
+
|
7
10
|
def self.included(base)
|
8
11
|
base.class_attribute :is_interceptor, :use_interceptor,
|
9
12
|
:interceptor_filters
|
@@ -13,7 +16,8 @@ module ActionInterceptor
|
|
13
16
|
|
14
17
|
base.before_filter :delete_intercepted_url
|
15
18
|
|
16
|
-
base.helper_method :
|
19
|
+
base.helper_method :is_interceptor, :use_interceptor, :use_interceptor=,
|
20
|
+
:current_page?, :current_url, :current_url_hash,
|
17
21
|
|
18
22
|
base.extend(ClassMethods)
|
19
23
|
end
|
@@ -40,60 +44,6 @@ module ActionInterceptor
|
|
40
44
|
@current_url_hash = {key => url}
|
41
45
|
end
|
42
46
|
|
43
|
-
def url_for(options = {})
|
44
|
-
url = super
|
45
|
-
return url unless self.use_interceptor
|
46
|
-
|
47
|
-
@intercepted_url_hash ||= self.is_interceptor ? intercepted_url_hash : \
|
48
|
-
current_url_hash
|
49
|
-
|
50
|
-
uri = URI(url)
|
51
|
-
new_query = URI.decode_www_form(uri.query || '') + \
|
52
|
-
@intercepted_url_hash.to_a
|
53
|
-
uri.query = URI.encode_www_form(new_query)
|
54
|
-
uri.to_s
|
55
|
-
end
|
56
|
-
|
57
|
-
# Executes the given block as if it was inside an interceptor
|
58
|
-
def with_interceptor(&block)
|
59
|
-
previous_use_interceptor = self.use_interceptor
|
60
|
-
|
61
|
-
begin
|
62
|
-
# Send the referer with intercepted requests
|
63
|
-
# So we don't rely on the user's browser to do it for us
|
64
|
-
self.use_interceptor = true
|
65
|
-
|
66
|
-
# Execute the block as if it was defined in this controller
|
67
|
-
instance_exec &block
|
68
|
-
rescue LocalJumpError => e
|
69
|
-
# Silently ignore `return` errors in the block
|
70
|
-
# and return the given value
|
71
|
-
e.exit_value
|
72
|
-
ensure
|
73
|
-
self.use_interceptor = previous_use_interceptor
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
|
-
# Executes the given block as if it was not inside an interceptor
|
78
|
-
def without_interceptor(&block)
|
79
|
-
previous_use_interceptor = self.use_interceptor
|
80
|
-
|
81
|
-
begin
|
82
|
-
# Send the referer with intercepted requests
|
83
|
-
# So we don't rely on the user's browser to do it for us
|
84
|
-
self.use_interceptor = false
|
85
|
-
|
86
|
-
# Execute the block as if it was defined in this controller
|
87
|
-
instance_exec &block
|
88
|
-
rescue LocalJumpError => e
|
89
|
-
# Silently ignore `return` errors in the block
|
90
|
-
# and return the given value
|
91
|
-
e.exit_value
|
92
|
-
ensure
|
93
|
-
self.use_interceptor = previous_use_interceptor
|
94
|
-
end
|
95
|
-
end
|
96
|
-
|
97
47
|
def delete_intercepted_url
|
98
48
|
session.delete(ActionInterceptor.intercepted_url_key)
|
99
49
|
end
|
@@ -1,61 +1,9 @@
|
|
1
|
+
require 'action_interceptor/common'
|
2
|
+
|
1
3
|
module ActionInterceptor
|
2
4
|
module View
|
3
5
|
|
4
|
-
|
5
|
-
# because the view's super method is different
|
6
|
-
def url_for(options = {})
|
7
|
-
url = super
|
8
|
-
return url unless controller.use_interceptor
|
9
|
-
|
10
|
-
@intercepted_url_hash ||= controller.is_interceptor ? \
|
11
|
-
intercepted_url_hash : current_url_hash
|
12
|
-
|
13
|
-
uri = URI(url)
|
14
|
-
new_query = URI.decode_www_form(uri.query || '') + \
|
15
|
-
@intercepted_url_hash.to_a
|
16
|
-
uri.query = URI.encode_www_form(new_query)
|
17
|
-
uri.to_s
|
18
|
-
end
|
19
|
-
|
20
|
-
# Executes the given block as if it was inside an interceptor
|
21
|
-
def with_interceptor(&block)
|
22
|
-
previous_use_interceptor = controller.use_interceptor
|
23
|
-
|
24
|
-
begin
|
25
|
-
# Send the referer with intercepted requests
|
26
|
-
# So we don't rely on the user's browser to do it for us
|
27
|
-
controller.use_interceptor = true
|
28
|
-
|
29
|
-
# Execute the block as if it was defined in this controller
|
30
|
-
instance_exec &block
|
31
|
-
rescue LocalJumpError => e
|
32
|
-
# Silently ignore `return` errors in the block
|
33
|
-
# and return the given value
|
34
|
-
e.exit_value
|
35
|
-
ensure
|
36
|
-
controller.use_interceptor = previous_use_interceptor
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
# Executes the given block as if it was not inside an interceptor
|
41
|
-
def without_interceptor(&block)
|
42
|
-
previous_use_interceptor = controller.use_interceptor
|
43
|
-
|
44
|
-
begin
|
45
|
-
# Send the referer with intercepted requests
|
46
|
-
# So we don't rely on the user's browser to do it for us
|
47
|
-
controller.use_interceptor = false
|
48
|
-
|
49
|
-
# Execute the block as if it was defined in this controller
|
50
|
-
instance_exec &block
|
51
|
-
rescue LocalJumpError => e
|
52
|
-
# Silently ignore `return` errors in the block
|
53
|
-
# and return the given value
|
54
|
-
e.exit_value
|
55
|
-
ensure
|
56
|
-
controller.use_interceptor = previous_use_interceptor
|
57
|
-
end
|
58
|
-
end
|
6
|
+
include Common
|
59
7
|
|
60
8
|
end
|
61
9
|
end
|
@@ -20,6 +20,8 @@ module ActionInterceptor
|
|
20
20
|
:current_url, true)).to be_true
|
21
21
|
expect(ActionController::Base.new.respond_to?(
|
22
22
|
:current_url_hash, true)).to be_true
|
23
|
+
expect(ActionController::Base.new.respond_to?(
|
24
|
+
:url_for, true)).to be_true
|
23
25
|
expect(ActionController::Base.new.respond_to?(
|
24
26
|
:with_interceptor, true)).to be_true
|
25
27
|
expect(ActionController::Base.new.respond_to?(
|
@@ -4,8 +4,12 @@ module ActionInterceptor
|
|
4
4
|
describe View do
|
5
5
|
|
6
6
|
it 'modifies ActionView::Base' do
|
7
|
-
expect(ActionView::Base.new
|
8
|
-
|
7
|
+
expect(ActionView::Base.new.respond_to?(
|
8
|
+
:url_for, true)).to be_true
|
9
|
+
expect(ActionView::Base.new.respond_to?(
|
10
|
+
:with_interceptor, true)).to be_true
|
11
|
+
expect(ActionView::Base.new.respond_to?(
|
12
|
+
:without_interceptor, true)).to be_true
|
9
13
|
end
|
10
14
|
|
11
15
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: action_interceptor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dante Soares
|
@@ -66,6 +66,7 @@ files:
|
|
66
66
|
- Rakefile
|
67
67
|
- config/initializers/action_interceptor.rb
|
68
68
|
- lib/action_interceptor.rb
|
69
|
+
- lib/action_interceptor/common.rb
|
69
70
|
- lib/action_interceptor/controller.rb
|
70
71
|
- lib/action_interceptor/encryptor.rb
|
71
72
|
- lib/action_interceptor/engine.rb
|