turbolinks-redirect 0.0.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.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +65 -0
- data/Rakefile +1 -0
- data/lib/assets/javascripts/turbolinks.redirect.js.coffee.erb +10 -0
- data/lib/turbolinks-redirect.rb +1 -0
- data/lib/turbolinks/redirect.rb +44 -0
- data/lib/turbolinks/redirect/version.rb +5 -0
- data/turbolinks-redirect.gemspec +27 -0
- metadata +136 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Jeremy Ruppel
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
# Turbolinks::Redirect
|
2
|
+
|
3
|
+
Simple `redirect_to` support for [turbolinks][turbolinks] and
|
4
|
+
[jquery-rails][jquery-rails].
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
gem 'turbolinks-redirect'
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install turbolinks-redirect
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
This approach assumes that you are trying to get turbolinks to play nice
|
23
|
+
with rails_ujs and want your ajax forms to be able to "redirect" on success.
|
24
|
+
|
25
|
+
Simply require `turbolinks.redirect` in your javascripts:
|
26
|
+
|
27
|
+
``` javascript
|
28
|
+
//= require turbolinks
|
29
|
+
//= require turbolinks.redirect
|
30
|
+
```
|
31
|
+
|
32
|
+
Now, write your controller actions as you normally would:
|
33
|
+
|
34
|
+
``` ruby
|
35
|
+
class PostsController < ActionController::Base
|
36
|
+
|
37
|
+
def create
|
38
|
+
@post = Post.new params[ :post ]
|
39
|
+
if @post.save
|
40
|
+
redirect_to @post
|
41
|
+
else
|
42
|
+
render :new
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
```
|
47
|
+
|
48
|
+
Under the hood, `redirect_to` checks to see if the request is an XHR. If
|
49
|
+
it is, instead of responding with a 3xx status code, it responds with a
|
50
|
+
custom 2xx status code, still setting the Location header.
|
51
|
+
|
52
|
+
Then, a handler is attached to the rails_ujs "ajax:success" event. It
|
53
|
+
checks for this particular status code, and if it finds it, tells
|
54
|
+
turbolinks to visit the page located in the header.
|
55
|
+
|
56
|
+
## Contributing
|
57
|
+
|
58
|
+
1. Fork it
|
59
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
60
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
61
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
62
|
+
5. Create new Pull Request
|
63
|
+
|
64
|
+
[turbolinks]: https://github.com/rails/turbolinks/
|
65
|
+
[jquery-rails]: https://github.com/rails/jquery-rails
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,10 @@
|
|
1
|
+
#= require jquery_ujs
|
2
|
+
#= require turbolinks
|
3
|
+
|
4
|
+
##
|
5
|
+
# On form success, we check for a custom status code we use to denote a
|
6
|
+
# "Turbolinks Redirect". If we find it, we will have the redirect location
|
7
|
+
# in the Location header and manually direct turbolinks to visit that url.
|
8
|
+
$( @ ).on 'ajax:success', ( event, data, status, xhr ) ->
|
9
|
+
if xhr.status is <%= Rack::Utils::SYMBOL_TO_STATUS_CODE[ :turbolinks ] %>
|
10
|
+
Turbolinks.visit( xhr.getResponseHeader( 'Location' ) )
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'turbolinks/redirect'
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'turbolinks/redirect/version'
|
2
|
+
require 'turbolinks'
|
3
|
+
require 'jquery-rails'
|
4
|
+
|
5
|
+
module Turbolinks
|
6
|
+
module Redirect
|
7
|
+
##
|
8
|
+
# Allows client-side redirection for rails ujs ajax requests. The client
|
9
|
+
# will look for HTTP status code 278, and will use turbolinks to request
|
10
|
+
# the new page if found. Status code 278 has no real significance, except
|
11
|
+
# that it is mentioned for this purpose at the following stackoverflow
|
12
|
+
# conversation: http://stackoverflow.com/a/304654
|
13
|
+
STATUS_CODE = 278
|
14
|
+
::Rack::Utils::HTTP_STATUS_CODES[ STATUS_CODE ] = 'Turbolinks Redirect'
|
15
|
+
::Rack::Utils::SYMBOL_TO_STATUS_CODE[ :turbolinks ] = STATUS_CODE
|
16
|
+
|
17
|
+
##
|
18
|
+
# All methods that will be included in ActionController::Base.
|
19
|
+
module Concern
|
20
|
+
|
21
|
+
##
|
22
|
+
# Override #redirect_to to automatically respond with a status
|
23
|
+
# of :turbolinks if this request is AJAX.
|
24
|
+
def redirect_to( options={}, response_status={} )
|
25
|
+
if request.xhr?
|
26
|
+
super options, response_status.merge( :status => :turbolinks )
|
27
|
+
else
|
28
|
+
super
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
##
|
34
|
+
# Rails integration
|
35
|
+
class Engine < Rails::Engine
|
36
|
+
|
37
|
+
initializer :turbolinks_redirect do
|
38
|
+
ActionController::Base.class_eval do
|
39
|
+
include Turbolinks::Redirect::Concern
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'turbolinks/redirect/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "turbolinks-redirect"
|
8
|
+
spec.version = Turbolinks::Redirect::VERSION
|
9
|
+
spec.authors = ["Jeremy Ruppel"]
|
10
|
+
spec.email = ["jeremy.ruppel@gmail.com"]
|
11
|
+
spec.description = %q{Simple redirect_to support for Turbolinks.}
|
12
|
+
spec.summary = %q{Simple redirect_to support for Turbolinks.}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency 'railties'
|
22
|
+
spec.add_dependency 'turbolinks'
|
23
|
+
spec.add_dependency 'jquery-rails'
|
24
|
+
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
26
|
+
spec.add_development_dependency "rake"
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,136 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: turbolinks-redirect
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jeremy Ruppel
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-04-11 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: railties
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: turbolinks
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: jquery-rails
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: bundler
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ~>
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '1.3'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '1.3'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: rake
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
description: Simple redirect_to support for Turbolinks.
|
95
|
+
email:
|
96
|
+
- jeremy.ruppel@gmail.com
|
97
|
+
executables: []
|
98
|
+
extensions: []
|
99
|
+
extra_rdoc_files: []
|
100
|
+
files:
|
101
|
+
- .gitignore
|
102
|
+
- Gemfile
|
103
|
+
- LICENSE.txt
|
104
|
+
- README.md
|
105
|
+
- Rakefile
|
106
|
+
- lib/assets/javascripts/turbolinks.redirect.js.coffee.erb
|
107
|
+
- lib/turbolinks-redirect.rb
|
108
|
+
- lib/turbolinks/redirect.rb
|
109
|
+
- lib/turbolinks/redirect/version.rb
|
110
|
+
- turbolinks-redirect.gemspec
|
111
|
+
homepage: ''
|
112
|
+
licenses:
|
113
|
+
- MIT
|
114
|
+
post_install_message:
|
115
|
+
rdoc_options: []
|
116
|
+
require_paths:
|
117
|
+
- lib
|
118
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
119
|
+
none: false
|
120
|
+
requirements:
|
121
|
+
- - ! '>='
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '0'
|
124
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
125
|
+
none: false
|
126
|
+
requirements:
|
127
|
+
- - ! '>='
|
128
|
+
- !ruby/object:Gem::Version
|
129
|
+
version: '0'
|
130
|
+
requirements: []
|
131
|
+
rubyforge_project:
|
132
|
+
rubygems_version: 1.8.23
|
133
|
+
signing_key:
|
134
|
+
specification_version: 3
|
135
|
+
summary: Simple redirect_to support for Turbolinks.
|
136
|
+
test_files: []
|