return_hook 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6e9d96e1f37fc671eab1f5cd8266e202a29f8336
4
+ data.tar.gz: 55a24fadbaa7eba3a8573190cb2359cdf60aa4cd
5
+ SHA512:
6
+ metadata.gz: dc45b77024fca587e76715cef50d92fff547cda3ba85a771aea874cc87d79e73bf069a3b8c52b7377483ab1f967c534efdfa69c951a41342c99904b7dbdbba1f
7
+ data.tar.gz: caad7a4ffba9240e3a57fcd7756e9e70a6f394851d29e8864bbaa022719010652588bfdda4281d8395d0f69fe3abbb355af45ce528c424d8e61cb1178df37fbb
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2013 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.markdown ADDED
@@ -0,0 +1,45 @@
1
+ ReturnHook
2
+ ==========
3
+
4
+ ReturnHook gives you some conveniences for hooking returns from controller redirects. It works by interacting with a url parameter 'return' (`params[:return]`). By specifying this parameter, you are able to override the redirect in a controller action to return the user to the url of your choice.
5
+
6
+ Convenience Methods
7
+ -------------------
8
+
9
+ ### smart_redirect
10
+ This method is a replacement for redirect_to in your controllers. It takes the same parameters, but is aware of any pending return hooks. If it detects `params[:return]`, then it will redirect to the url specified under `params[:return]`, instead of the one given to it in it's first parameter.
11
+
12
+ ##### Example:
13
+ You have accessed a controller action with `?return=/your-url`. At the end of your action you write: `smart_redirect(users_path)`. Because `?return=/your-url` is present in your calling url, it will redirect to `/your-url` instead of `users_path`. Otherwise it will follow the same semantics of `redirect_to`.
14
+
15
+ ### url_for_return
16
+ This method is available to both controllers and views. It takes a url_for argument, and adds a return parameter equal to the present url.
17
+
18
+ ##### Example:
19
+ You want to have the user go to a certain action, but once they have completed that flow, they will be redirect back to this page.
20
+
21
+ ### forward_return_hook
22
+ This method is also available to both controllers and views. You feed it a url, and if `params[:return]` is present, then it will automatically add the return parameter to the url specified. It returns the url + the return hook.
23
+
24
+ #### Form Helpers:
25
+ The form helper is automatically overridden to forward the return hook to it's action parameter. This works with all form builders. (simple_form, formatastic, etc...). So if you use the return hook in the url, and that page has a form in it, then that form will submit to it's action, and include the return hook in that url. The controller action responsible for handling that action can then handle the form submission, and use `smart_redirect` to redirect to the return hook if specified.
26
+
27
+ Responders, respond_with
28
+ ------------------------
29
+ If you would like to have respond_with at the end of your controller methods, and use ReturnHook, then you can `include ReturnHook::Responder` in your own responder class. This will override the redirect_to inside your responder to use `smart_redirect` for catching any return hooks.
30
+
31
+ ##### Example:
32
+
33
+ ###### lib/app_responder.rb
34
+ ```ruby
35
+ class AppResponder < ActionController::Responder
36
+ include ReturnHook::Responder
37
+ end
38
+ ```
39
+ ###### app/controllers/application_controller.rb
40
+ ```ruby
41
+ require 'app_responder'
42
+ class ApplicationController < ActionController::Base
43
+ self.responder = AppResponder
44
+ ...
45
+ ```
data/Rakefile ADDED
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'ReturnHook'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+
24
+
25
+
26
+ Bundler::GemHelper.install_tasks
27
+
@@ -0,0 +1,43 @@
1
+ module ReturnHook
2
+ module Controller
3
+ def self.included(base)
4
+ base.send :helper_method, :url_for_return, :forward_return_hook
5
+ end
6
+ def smart_redirect(options = {}, response_status = {})
7
+ if !((response_status.include?(:override_return) ||
8
+ (options.is_a?(Hash) && options.include?(:override_return))))
9
+ uri = params[:return]
10
+ if uri
11
+ redirect_to CGI.unescape(uri), response_status
12
+ elsif (options == :back) && request.env['HTTP_REFERER'].blank?
13
+ redirect_to '/'
14
+ else
15
+ redirect_to options, response_status
16
+ end
17
+ elsif (options == :back) && request.env['HTTP_REFERER'].blank?
18
+ redirect_to '/'
19
+ else
20
+ redirect_to options, response_status
21
+ end
22
+ end
23
+ def url_for_return(params)
24
+ escaped_url = CGI.escape request.url
25
+ if params.is_a? String
26
+ params = params.include?('?') ? params + '&return=' + escaped_url : params + '?return=' + CGI.escape(escaped_url)
27
+ elsif params.is_a? Hash
28
+ params[:return] = escaped_url
29
+ end
30
+ url_for params
31
+ end
32
+ def forward_return_hook(url)
33
+ if params[:return]
34
+ if url.is_a? String
35
+ url = url.include?('?') ? url + '&return=' + params[:return] : url + '?return=' + params[:return]
36
+ elsif url.is_a? Hash
37
+ url[:return] = params[:return]
38
+ end
39
+ end
40
+ url_for url
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,32 @@
1
+ module ReturnHook
2
+ module FormTagHelper
3
+ private
4
+ # This method overrides the rails built in form helper's action setting code
5
+ # to inject a return path
6
+ def html_options_for_form(url_for_options, options)
7
+ options.stringify_keys.tap do |html_options|
8
+ html_options["enctype"] = "multipart/form-data" if html_options.delete("multipart")
9
+ # The following URL is unescaped, this is just a hash of options, and it is the
10
+ # responsibility of the caller to escape all the values.
11
+
12
+ ## OVERRIDDEN HERE:
13
+ html_options["action"] = forward_return_hook(url_for(url_for_options))
14
+
15
+ html_options["accept-charset"] = "UTF-8"
16
+
17
+ html_options["data-remote"] = true if html_options.delete("remote")
18
+
19
+ if html_options["data-remote"] &&
20
+ !embed_authenticity_token_in_remote_forms &&
21
+ html_options["authenticity_token"].blank?
22
+ # The authenticity token is taken from the meta tag in this case
23
+ html_options["authenticity_token"] = false
24
+ elsif html_options["authenticity_token"] == true
25
+ # Include the default authenticity_token, which is only generated when its set to nil,
26
+ # but we needed the true value to override the default of no authenticity_token on data-remote.
27
+ html_options["authenticity_token"] = nil
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,12 @@
1
+ module ReturnHook
2
+ module Responder
3
+ def redirect_to(options = {}, response_status = {})
4
+ if format == :html && !request.xhr?
5
+ # follow returns by using the smart method
6
+ controller.smart_redirect(options,response_status)
7
+ else
8
+ controller.redirect_to options,response_status
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ module ReturnHook
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,11 @@
1
+ require 'return_hook/version'
2
+ require 'return_hook/controller'
3
+ require 'return_hook/form_tag_helper'
4
+ require 'return_hook/responder'
5
+
6
+ module ReturnHook
7
+
8
+ end
9
+
10
+ ActionController::Base.send :include, ReturnHook::Controller
11
+ ActionController::Base.helper ReturnHook::FormTagHelper
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: return_hook
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Nik Petersen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-08-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: adds some convenience helpers and methods for hooking the redirect in
28
+ a controller
29
+ email:
30
+ - demersus@gmail.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - lib/return_hook/controller.rb
36
+ - lib/return_hook/responder.rb
37
+ - lib/return_hook/version.rb
38
+ - lib/return_hook/form_tag_helper.rb
39
+ - lib/return_hook.rb
40
+ - MIT-LICENSE
41
+ - Rakefile
42
+ - README.markdown
43
+ homepage: https://github.com/demersus/return_hook
44
+ licenses: []
45
+ metadata: {}
46
+ post_install_message:
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - '>='
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ requirements: []
61
+ rubyforge_project:
62
+ rubygems_version: 2.0.3
63
+ signing_key:
64
+ specification_version: 4
65
+ summary: adds some convenience helpers and methods for hooking the redirect in a controller
66
+ test_files: []