action_interceptor 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 04669ca4f3feb14833aa912c88dff4787e3c4c1e
4
- data.tar.gz: 63cb94be3b6dc259fb9ea5c9db942aa59549c436
3
+ metadata.gz: 164253286739cc3f66b62a86b6b8c8650ebf441b
4
+ data.tar.gz: 96fc1302173b587524f3ede0cf8bfdeaf5953f5f
5
5
  SHA512:
6
- metadata.gz: 175943387c1c16fa25aa2c5a827dd130a56eadb4706bdb72d3228cd48127e49e5c59ed3c10dc9ce1f87dd947c9cebefa7ebb034d3ceb3927bd8e95a905ab48b0
7
- data.tar.gz: f56f418f2136734120cd716df80d6a3d41a830a59723b3273d6e64839bd808fac43c21f648195ca2f10ce76750d6db5dadc922efe7f739a563b7c46fb88112df
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
- The `acts_as_interceptor` method will ensure the following:
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 calling `acts_as_interceptor`
95
- like this: `acts_as_interceptor override_url_options: false`. In that case,
96
- you are responsible for passing the `intercepted_url_hash` to any internal
97
- links and redirects.
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`, `intercepted_url=`,
101
- `intercepted_url_hash`, `without_interceptor(&block)`,
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 :current_page?, :current_url, :current_url_hash
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,4 +1,4 @@
1
1
  module ActionInterceptor
2
- VERSION = '0.2.0'
2
+ VERSION = '0.2.1'
3
3
  end
4
4
 
@@ -1,61 +1,9 @@
1
+ require 'action_interceptor/common'
2
+
1
3
  module ActionInterceptor
2
4
  module View
3
5
 
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
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).to respond_to(:with_interceptor)
8
- expect(ActionView::Base.new).to respond_to(:without_interceptor)
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.0
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