rails-ajax_redirect 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +62 -0
- data/lib/assets/javascripts/rails-ajax_redirect.js.erb +8 -0
- data/lib/rails/ajax_redirect.rb +39 -0
- data/lib/rails/ajax_redirect/version.rb +5 -0
- metadata +106 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5be9be975a242193fb0d4b6c29eb4633104cafb5
|
4
|
+
data.tar.gz: a5880efa46faa048db6e9b353fe2807ef2223a62
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b0b0cc1c5316de8a6b6ae2f32bb86a2897fcbf77838a560152b60750893a2eba9ff2ebffb3048ec75ba1c7fee2296e8cebfd9a5d268a307c1aec12f0f3c16ee4
|
7
|
+
data.tar.gz: 72d7d1595c1aa08396702429d6d609819c8e9c558084b42ed3833b70bb4e45099a3d4e53b3d1978f1e0994265dc1f955633d34a946d5c95e351a15e01f6a6b97
|
data/README.md
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
Rails::AjaxRedirect
|
2
|
+
=====================
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/rails-ajax_redirect.svg)](http://badge.fury.io/rb/rails-ajax_redirect)
|
4
|
+
[![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/ElMassimo/rails-ajax_redirect/blob/master/LICENSE.txt)
|
5
|
+
|
6
|
+
Simple `redirect_to` support for AJAX requests in non-Turbolinks applications.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
gem 'rails-ajax_redirect'
|
13
|
+
|
14
|
+
Or install it yourself as:
|
15
|
+
|
16
|
+
gem install rails-ajax_redirect
|
17
|
+
|
18
|
+
## Usage
|
19
|
+
|
20
|
+
Require `rails-ajax_redirect` in your `application.js`:
|
21
|
+
|
22
|
+
``` javascript
|
23
|
+
//= require rails-ajax_redirect
|
24
|
+
```
|
25
|
+
|
26
|
+
`AjaxRedirect` extends `redirect_to` to use a custom status code for ajax
|
27
|
+
requests, while still setting the `Location` header.
|
28
|
+
|
29
|
+
The script adds a handler for the `ajax:success` event, that navigates to the
|
30
|
+
location in the header for responses that have the custom status code.
|
31
|
+
|
32
|
+
Now you will be able to write controller actions in the usual way :smiley:
|
33
|
+
|
34
|
+
## Turbolinks
|
35
|
+
|
36
|
+
If your application uses Turbolinks, you should use the [`turbolinks-redirect`](https://github.com/remind101/turbolinks-redirect), on which this library is based on. The advantage is that
|
37
|
+
it will perform faster navigation by leveraging Turbolinks to visit the new
|
38
|
+
location.
|
39
|
+
|
40
|
+
License
|
41
|
+
--------
|
42
|
+
|
43
|
+
Copyright (c) 2015 Máximo Mussini
|
44
|
+
|
45
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
46
|
+
a copy of this software and associated documentation files (the
|
47
|
+
"Software"), to deal in the Software without restriction, including
|
48
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
49
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
50
|
+
permit persons to whom the Software is furnished to do so, subject to
|
51
|
+
the following conditions:
|
52
|
+
|
53
|
+
The above copyright notice and this permission notice shall be
|
54
|
+
included in all copies or substantial portions of the Software.
|
55
|
+
|
56
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
57
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
58
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
59
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
60
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
61
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
62
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
@@ -0,0 +1,8 @@
|
|
1
|
+
// require jquery_ujs
|
2
|
+
|
3
|
+
AJAX_REDIRECT_STATUS_CODE = <%= Rack::Utils::SYMBOL_TO_STATUS_CODE[:ajax_redirect] %>
|
4
|
+
$(document).on('ajax:success', function(event, data, status, xhr) {
|
5
|
+
if(xhr.status == AJAX_REDIRECT_STATUS_CODE) {
|
6
|
+
window.location = xhr.getResponseHeader('Location')
|
7
|
+
}
|
8
|
+
});
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'rails/ajax_redirect/version'
|
2
|
+
require 'jquery-rails'
|
3
|
+
|
4
|
+
module Rails
|
5
|
+
# Public: Provides client-side redirection for ajax requests. The client will
|
6
|
+
# check successful ajax requests for the custom HTTP status code 279, and will
|
7
|
+
# navigate to the new location.
|
8
|
+
#
|
9
|
+
# Status code 279 has no real significance, and is used as a workaround for
|
10
|
+
# the way browsers handle redirects: http://stackoverflow.com/a/304654
|
11
|
+
module AjaxRedirect
|
12
|
+
STATUS_CODE = 279
|
13
|
+
::Rack::Utils::HTTP_STATUS_CODES[STATUS_CODE] = 'Ajax Redirect'
|
14
|
+
::Rack::Utils::SYMBOL_TO_STATUS_CODE[:ajax_redirect] = STATUS_CODE
|
15
|
+
|
16
|
+
# Internal: Included in ActionController::Base to override `redirect_to`.
|
17
|
+
module Concern
|
18
|
+
|
19
|
+
# Override: Automatically respond with a status of :ajax_redirect if it's
|
20
|
+
# an ajax request.
|
21
|
+
def redirect_to(options = {}, response_status = {})
|
22
|
+
if request.xhr?
|
23
|
+
super options, response_status.merge(status: :ajax_redirect)
|
24
|
+
else
|
25
|
+
super
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# Internal: Include the module in all controllers.
|
31
|
+
class Engine < Rails::Engine
|
32
|
+
initializer :ajax_redirect do
|
33
|
+
ActionController::Base.class_eval do
|
34
|
+
include Rails::AjaxRedirect::Concern
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
metadata
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rails-ajax_redirect
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Máximo Mussini
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-12-29 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: railties
|
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
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: jquery-rails
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: Simple redirect_to support for ajax requests in Rails, for non-Turbolinks
|
70
|
+
applications.
|
71
|
+
email:
|
72
|
+
- maximomussini@gmail.com
|
73
|
+
executables: []
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files:
|
76
|
+
- README.md
|
77
|
+
files:
|
78
|
+
- README.md
|
79
|
+
- lib/assets/javascripts/rails-ajax_redirect.js.erb
|
80
|
+
- lib/rails/ajax_redirect.rb
|
81
|
+
- lib/rails/ajax_redirect/version.rb
|
82
|
+
homepage: https://github.com/ElMassimo/rails-ajax_redirect
|
83
|
+
licenses:
|
84
|
+
- MIT
|
85
|
+
metadata: {}
|
86
|
+
post_install_message:
|
87
|
+
rdoc_options: []
|
88
|
+
require_paths:
|
89
|
+
- lib
|
90
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: 1.9.3
|
95
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
96
|
+
requirements:
|
97
|
+
- - ">="
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
requirements: []
|
101
|
+
rubyforge_project:
|
102
|
+
rubygems_version: 2.4.5.1
|
103
|
+
signing_key:
|
104
|
+
specification_version: 4
|
105
|
+
summary: Simple redirect_to support for ajax requests in Rails.
|
106
|
+
test_files: []
|