action_interceptor 0.1.2 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -13
- data/lib/action_interceptor/controller.rb +64 -40
- data/lib/action_interceptor/engine.rb +1 -0
- data/lib/action_interceptor/version.rb +1 -1
- data/lib/action_interceptor/view.rb +63 -0
- data/spec/lib/action_interceptor/controller_spec.rb +19 -13
- data/spec/lib/action_interceptor/view_spec.rb +12 -0
- metadata +13 -10
checksums.yaml
CHANGED
@@ -1,15 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
|
5
|
-
data.tar.gz: !binary |-
|
6
|
-
Mjc2YjgzZTBiYzk0YjRiY2E2ZTY1Zjg1YmQwNGU4NjllYWY3MjlmNQ==
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 04669ca4f3feb14833aa912c88dff4787e3c4c1e
|
4
|
+
data.tar.gz: 63cb94be3b6dc259fb9ea5c9db942aa59549c436
|
7
5
|
SHA512:
|
8
|
-
metadata.gz:
|
9
|
-
|
10
|
-
NTdmMzI5MGRhMDZlYTM3OWI2ZTMwNTZlMDEyMDAyYTdjNDVkNmFmMTY0ODYz
|
11
|
-
ZTY4NDAwYWZjMTY3MTEzNGE5ZjczMDFiNjQ2YzA4ZjI5NGZmNDM=
|
12
|
-
data.tar.gz: !binary |-
|
13
|
-
M2IzOWJkZTMzMjgxMzZmYzY0YzRkNTM3ZjNhODNiYWI3YjNhODYyYjk4YzZh
|
14
|
-
MTNlODg2MDFiNTk2YmY4YzQyZjg5Mzc4YmIzYmEzODEwY2NlOTdiYzc1YTNl
|
15
|
-
ODc4NjVhMWRkOWZjMDgyOTMzY2NmYzAyNWFmYzVmOGY4ZjM2YzE=
|
6
|
+
metadata.gz: 175943387c1c16fa25aa2c5a827dd130a56eadb4706bdb72d3228cd48127e49e5c59ed3c10dc9ce1f87dd947c9cebefa7ebb034d3ceb3927bd8e95a905ab48b0
|
7
|
+
data.tar.gz: f56f418f2136734120cd716df80d6a3d41a830a59723b3273d6e64839bd808fac43c21f648195ca2f10ce76750d6db5dadc922efe7f739a563b7c46fb88112df
|
@@ -5,21 +5,30 @@ module ActionInterceptor
|
|
5
5
|
module Controller
|
6
6
|
|
7
7
|
def self.included(base)
|
8
|
-
base.class_attribute :is_interceptor, :
|
8
|
+
base.class_attribute :is_interceptor, :use_interceptor,
|
9
|
+
:interceptor_filters
|
9
10
|
base.is_interceptor = false
|
11
|
+
base.use_interceptor = false
|
10
12
|
base.interceptor_filters = {}
|
13
|
+
|
14
|
+
base.before_filter :delete_intercepted_url
|
15
|
+
|
16
|
+
base.helper_method :current_page?, :current_url, :current_url_hash
|
17
|
+
|
11
18
|
base.extend(ClassMethods)
|
12
19
|
end
|
13
20
|
|
14
|
-
|
15
|
-
"#{request.protocol}#{request.host_with_port}#{request.fullpath}"
|
16
|
-
end
|
21
|
+
protected
|
17
22
|
|
18
23
|
def current_page?(url)
|
19
24
|
# Blank is the current page
|
20
25
|
url.blank? || URI(url).path == request.path
|
21
26
|
end
|
22
27
|
|
28
|
+
def current_url
|
29
|
+
"#{request.protocol}#{request.host_with_port}#{request.fullpath}"
|
30
|
+
end
|
31
|
+
|
23
32
|
def current_url_hash
|
24
33
|
return @current_url_hash if @current_url_hash
|
25
34
|
|
@@ -31,15 +40,48 @@ module ActionInterceptor
|
|
31
40
|
@current_url_hash = {key => url}
|
32
41
|
end
|
33
42
|
|
34
|
-
|
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
|
35
58
|
def with_interceptor(&block)
|
36
|
-
|
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
|
37
80
|
|
38
81
|
begin
|
39
82
|
# Send the referer with intercepted requests
|
40
83
|
# So we don't rely on the user's browser to do it for us
|
41
|
-
self.
|
42
|
-
.merge(current_url_hash)
|
84
|
+
self.use_interceptor = false
|
43
85
|
|
44
86
|
# Execute the block as if it was defined in this controller
|
45
87
|
instance_exec &block
|
@@ -48,10 +90,14 @@ module ActionInterceptor
|
|
48
90
|
# and return the given value
|
49
91
|
e.exit_value
|
50
92
|
ensure
|
51
|
-
self.
|
93
|
+
self.use_interceptor = previous_use_interceptor
|
52
94
|
end
|
53
95
|
end
|
54
96
|
|
97
|
+
def delete_intercepted_url
|
98
|
+
session.delete(ActionInterceptor.intercepted_url_key)
|
99
|
+
end
|
100
|
+
|
55
101
|
module ClassMethods
|
56
102
|
|
57
103
|
def interceptor(*interceptor_names, &block)
|
@@ -85,18 +131,20 @@ module ActionInterceptor
|
|
85
131
|
end
|
86
132
|
|
87
133
|
def acts_as_interceptor(options = {})
|
88
|
-
return if is_interceptor
|
89
134
|
self.is_interceptor = true
|
135
|
+
self.use_interceptor = options[:override_url_options].nil? ? \
|
136
|
+
ActionInterceptor.override_url_options : \
|
137
|
+
options[:override_url_options]
|
90
138
|
|
91
|
-
|
92
|
-
ActionInterceptor.override_url_options : \
|
93
|
-
options[:override_url_options]
|
94
|
-
|
95
|
-
class_eval do
|
139
|
+
class_exec do
|
96
140
|
|
97
141
|
attr_writer :intercepted_url
|
98
142
|
|
99
|
-
|
143
|
+
skip_before_filter :delete_intercepted_url
|
144
|
+
|
145
|
+
helper_method :intercepted_url, :intercepted_url_hash
|
146
|
+
|
147
|
+
protected
|
100
148
|
|
101
149
|
def intercepted_url
|
102
150
|
return @intercepted_url if @intercepted_url
|
@@ -127,30 +175,6 @@ module ActionInterceptor
|
|
127
175
|
@intercepted_url_hash = {key => url}
|
128
176
|
end
|
129
177
|
|
130
|
-
alias_method :url_options_without_interceptor, :url_options
|
131
|
-
|
132
|
-
def url_options_with_interceptor
|
133
|
-
return @url_options_with_interceptor \
|
134
|
-
if @url_options_with_interceptor
|
135
|
-
|
136
|
-
@url_options_with_interceptor = intercepted_url_hash.merge(
|
137
|
-
url_options_without_interceptor)
|
138
|
-
end
|
139
|
-
|
140
|
-
alias_method :url_options, :url_options_with_interceptor \
|
141
|
-
if @override_url_options.nil? || @override_url_options
|
142
|
-
|
143
|
-
def without_interceptor(&block)
|
144
|
-
previous_url_options = url_options_with_interceptor
|
145
|
-
|
146
|
-
begin
|
147
|
-
@url_options_with_interceptor = url_options_without_interceptor
|
148
|
-
instance_exec &block
|
149
|
-
ensure
|
150
|
-
@url_options_with_interceptor = previous_url_options
|
151
|
-
end
|
152
|
-
end
|
153
|
-
|
154
178
|
def redirect_back(options = {})
|
155
179
|
url = intercepted_url
|
156
180
|
|
@@ -0,0 +1,63 @@
|
|
1
|
+
module ActionInterceptor
|
2
|
+
module View
|
3
|
+
|
4
|
+
# Can't reuse url_for from controller
|
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
|
59
|
+
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
ActionView::Base.send :include, ActionInterceptor::View
|
@@ -5,6 +5,7 @@ module ActionInterceptor
|
|
5
5
|
|
6
6
|
it 'modifies ActionController::Base' do
|
7
7
|
expect(ActionController::Base).to respond_to(:is_interceptor)
|
8
|
+
expect(ActionController::Base).to respond_to(:use_interceptor)
|
8
9
|
expect(ActionController::Base).to respond_to(:interceptor_filters)
|
9
10
|
expect(ActionController::Base.is_interceptor).to be_false
|
10
11
|
expect(ActionController::Base.interceptor_filters).to be_a(Hash)
|
@@ -13,24 +14,29 @@ module ActionInterceptor
|
|
13
14
|
expect(ActionController::Base).to respond_to(:skip_interceptor)
|
14
15
|
expect(ActionController::Base).to respond_to(:acts_as_interceptor)
|
15
16
|
|
16
|
-
expect(ActionController::Base.new
|
17
|
-
|
18
|
-
expect(ActionController::Base.new
|
19
|
-
|
17
|
+
expect(ActionController::Base.new.respond_to?(
|
18
|
+
:current_page?, true)).to be_true
|
19
|
+
expect(ActionController::Base.new.respond_to?(
|
20
|
+
:current_url, true)).to be_true
|
21
|
+
expect(ActionController::Base.new.respond_to?(
|
22
|
+
:current_url_hash, true)).to be_true
|
23
|
+
expect(ActionController::Base.new.respond_to?(
|
24
|
+
:with_interceptor, true)).to be_true
|
25
|
+
expect(ActionController::Base.new.respond_to?(
|
26
|
+
:without_interceptor, true)).to be_true
|
20
27
|
end
|
21
28
|
|
22
29
|
it 'modifies classes that act_as_interceptor' do
|
23
30
|
expect(RegistrationsController.is_interceptor).to be_true
|
24
31
|
|
25
|
-
expect(RegistrationsController.new
|
26
|
-
|
27
|
-
expect(RegistrationsController.new
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
expect(RegistrationsController.new).to respond_to(:redirect_back)
|
32
|
+
expect(RegistrationsController.new.respond_to?(
|
33
|
+
:intercepted_url, true)).to be_true
|
34
|
+
expect(RegistrationsController.new.respond_to?(
|
35
|
+
:intercepted_url=, true)).to be_true
|
36
|
+
expect(RegistrationsController.new.respond_to?(
|
37
|
+
:intercepted_url_hash, true)).to be_true
|
38
|
+
expect(RegistrationsController.new.respond_to?(
|
39
|
+
:redirect_back, true)).to be_true
|
34
40
|
end
|
35
41
|
|
36
42
|
it 'registers and skips before_filters' do
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module ActionInterceptor
|
4
|
+
describe View do
|
5
|
+
|
6
|
+
it 'modifies ActionView::Base' do
|
7
|
+
expect(ActionView::Base.new).to respond_to(:with_interceptor)
|
8
|
+
expect(ActionView::Base.new).to respond_to(:without_interceptor)
|
9
|
+
end
|
10
|
+
|
11
|
+
end
|
12
|
+
end
|
metadata
CHANGED
@@ -1,55 +1,55 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: action_interceptor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dante Soares
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-06-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '3.1'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '3.1'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: sqlite3
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rspec-rails
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- -
|
52
|
+
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
55
|
description: Action Interceptor provides controllers that require users to perform
|
@@ -71,6 +71,7 @@ files:
|
|
71
71
|
- lib/action_interceptor/engine.rb
|
72
72
|
- lib/action_interceptor/undefined_interceptor.rb
|
73
73
|
- lib/action_interceptor/version.rb
|
74
|
+
- lib/action_interceptor/view.rb
|
74
75
|
- lib/tasks/action_interceptor_tasks.rake
|
75
76
|
- spec/dummy/README.md
|
76
77
|
- spec/dummy/Rakefile
|
@@ -110,6 +111,7 @@ files:
|
|
110
111
|
- spec/dummy/public/favicon.ico
|
111
112
|
- spec/lib/action_interceptor/controller_spec.rb
|
112
113
|
- spec/lib/action_interceptor/encryptor_spec.rb
|
114
|
+
- spec/lib/action_interceptor/view_spec.rb
|
113
115
|
- spec/lib/action_interceptor_spec.rb
|
114
116
|
- spec/spec_helper.rb
|
115
117
|
homepage: http://github.com/openstax/action_interceptor
|
@@ -122,12 +124,12 @@ require_paths:
|
|
122
124
|
- lib
|
123
125
|
required_ruby_version: !ruby/object:Gem::Requirement
|
124
126
|
requirements:
|
125
|
-
- -
|
127
|
+
- - ">="
|
126
128
|
- !ruby/object:Gem::Version
|
127
129
|
version: '0'
|
128
130
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
129
131
|
requirements:
|
130
|
-
- -
|
132
|
+
- - ">="
|
131
133
|
- !ruby/object:Gem::Version
|
132
134
|
version: '0'
|
133
135
|
requirements: []
|
@@ -175,5 +177,6 @@ test_files:
|
|
175
177
|
- spec/dummy/README.md
|
176
178
|
- spec/lib/action_interceptor/controller_spec.rb
|
177
179
|
- spec/lib/action_interceptor/encryptor_spec.rb
|
180
|
+
- spec/lib/action_interceptor/view_spec.rb
|
178
181
|
- spec/lib/action_interceptor_spec.rb
|
179
182
|
- spec/spec_helper.rb
|