rack-pjax 0.5.3 → 0.5.4
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/README.md +20 -15
- data/lib/rack/pjax/version.rb +1 -1
- data/lib/rack/pjax.rb +9 -6
- data/rack-pjax.gemspec +2 -2
- data/spec/rack/pjax_spec.rb +52 -18
- metadata +13 -13
data/README.md
CHANGED
@@ -3,16 +3,26 @@ Rack-pjax [](ht
|
|
3
3
|
|
4
4
|
Rack-pjax is middleware that lets you serve 'chrome-less' pages in respond to [pjax-requests](https://github.com/defunkt/jquery-pjax).
|
5
5
|
|
6
|
-
|
7
|
-
For example when you have:
|
6
|
+
It does this by simply filtering the generated page; only the title and inner-html of the pjax-container are sent to the client.
|
8
7
|
|
9
|
-
|
10
|
-
-
|
8
|
+
While this won't save you any time rendering the page, it gives you more flexibility where or how to define the pjax-container.
|
9
|
+
Ryan Bates featured [rack-pjax on Railscasts](http://railscasts.com/episodes/294-playing-with-pjax) and explains how this gem compares to [pjax_rails](https://github.com/rails/pjax_rails).
|
11
10
|
|
12
|
-
|
11
|
+
Installation
|
13
12
|
------------
|
14
13
|
|
15
|
-
|
14
|
+
Check out the [Railscast notes](http://railscasts.com/episodes/294-playing-with-pjax) how to integrate rack-pjax in your Rails 3.1 application.
|
15
|
+
|
16
|
+
The more generic installation comes down to:
|
17
|
+
|
18
|
+
I. Add the gem to your Gemfile
|
19
|
+
|
20
|
+
```ruby
|
21
|
+
# Gemfile
|
22
|
+
gem "rack-pjax"
|
23
|
+
```
|
24
|
+
|
25
|
+
II. Include **rack-pjax** as middleware to your application(-stack)
|
16
26
|
|
17
27
|
```ruby
|
18
28
|
# config.ru
|
@@ -20,7 +30,8 @@ I. Include **rack-pjax** as middleware to your application(-stack)
|
|
20
30
|
use Rack::Pjax
|
21
31
|
run RackApp::Application
|
22
32
|
```
|
23
|
-
|
33
|
+
|
34
|
+
III. Install [jquery-pjax](https://github.com/defunkt/jquery-pjax). Make sure to add the 'data-pjax-container'-attribute to the container.
|
24
35
|
|
25
36
|
```html
|
26
37
|
<head>
|
@@ -41,19 +52,13 @@ II. Install [jquery-pjax](https://github.com/defunkt/jquery-pjax). Make sure to
|
|
41
52
|
</body>
|
42
53
|
```
|
43
54
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
Installation
|
48
|
-
------------
|
49
|
-
|
50
|
-
$ gem install rack-pjax
|
55
|
+
IV. Fire up your [pushState-enabled browser](http://caniuse.com/#search=pushstate) and enjoy!
|
51
56
|
|
52
57
|
|
53
58
|
Requirements
|
54
59
|
------------
|
55
60
|
|
56
|
-
-
|
61
|
+
- Hpricot
|
57
62
|
|
58
63
|
|
59
64
|
Author
|
data/lib/rack/pjax/version.rb
CHANGED
data/lib/rack/pjax.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require '
|
1
|
+
require 'hpricot'
|
2
2
|
|
3
3
|
module Rack
|
4
4
|
class Pjax
|
@@ -14,13 +14,16 @@ module Rack
|
|
14
14
|
|
15
15
|
if pjax?(env)
|
16
16
|
new_body = ""
|
17
|
-
body.each do |
|
18
|
-
|
17
|
+
body.each do |b|
|
18
|
+
parsed_body = Hpricot(b)
|
19
|
+
container = parsed_body.at("[@data-pjax-container]")
|
19
20
|
if container
|
20
|
-
title =
|
21
|
-
|
21
|
+
title = parsed_body.at("title")
|
22
|
+
|
23
|
+
new_body << title.to_s if title
|
24
|
+
new_body << container.inner_html
|
22
25
|
else
|
23
|
-
new_body <<
|
26
|
+
new_body << b
|
24
27
|
end
|
25
28
|
end
|
26
29
|
|
data/rack-pjax.gemspec
CHANGED
@@ -18,6 +18,6 @@ Gem::Specification.new do |s|
|
|
18
18
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
19
|
s.require_paths = ["lib"]
|
20
20
|
|
21
|
-
s.add_dependency('rack', '
|
22
|
-
s.add_dependency('
|
21
|
+
s.add_dependency('rack', '~>1.3')
|
22
|
+
s.add_dependency('hpricot', '~>0.8.4')
|
23
23
|
end
|
data/spec/rack/pjax_spec.rb
CHANGED
@@ -3,41 +3,75 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
|
3
3
|
describe Rack::Pjax do
|
4
4
|
include Rack::Test::Methods # can be moved to config
|
5
5
|
|
6
|
-
def
|
6
|
+
def generate_app(options={})
|
7
|
+
body = options[:body]
|
8
|
+
|
7
9
|
Rack::Lint.new(
|
8
|
-
Rack::
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
}
|
18
|
-
end
|
10
|
+
Rack::Pjax.new(
|
11
|
+
lambda do |env|
|
12
|
+
[
|
13
|
+
200,
|
14
|
+
{'Content-Type' => 'text/plain', 'Content-Length' => Rack::Utils.bytesize(body).to_s},
|
15
|
+
[body]
|
16
|
+
]
|
17
|
+
end
|
18
|
+
)
|
19
19
|
)
|
20
20
|
end
|
21
21
|
|
22
|
-
context "
|
23
|
-
|
22
|
+
context "a pjaxified app, upon receiving a pjax-request" do
|
23
|
+
before do
|
24
|
+
self.class.app = generate_app(:body => '<html><title>Hello</title><body><div data-pjax-container>World!</div></body></html>')
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should return the title-tag in the body" do
|
24
28
|
get "/", {}, {"HTTP_X_PJAX" => "true"}
|
25
29
|
body.should == "<title>Hello</title>World!"
|
26
30
|
end
|
27
31
|
|
28
|
-
it "should
|
32
|
+
it "should return the inner-html of the pjax-container in the body" do
|
33
|
+
self.class.app = generate_app(:body => '<html><body><div data-pjax-container>World!</div></body></html>')
|
34
|
+
|
35
|
+
get "/", {}, {"HTTP_X_PJAX" => "true"}
|
36
|
+
body.should == "World!"
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should return the correct Content Length" do
|
29
40
|
get "/", {}, {"HTTP_X_PJAX" => "true"}
|
30
41
|
headers['Content-Length'].should == Rack::Utils.bytesize(body).to_s
|
31
42
|
end
|
43
|
+
|
44
|
+
it "should return the original body when there's no pjax-container" do
|
45
|
+
self.class.app = generate_app(:body => '<html><body>Has no pjax-container</body></html>')
|
46
|
+
|
47
|
+
get "/", {}, {"HTTP_X_PJAX" => "true"}
|
48
|
+
body.should == "<html><body>Has no pjax-container</body></html>"
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should preserve whitespaces of the original body" do
|
52
|
+
container = "\n <p>\nfirst paragraph</p> <p>Second paragraph</p>\n"
|
53
|
+
self.class.app = generate_app(:body =><<-BODY)
|
54
|
+
<html>
|
55
|
+
<div data-pjax-container>#{container}</div>
|
56
|
+
</html>
|
57
|
+
BODY
|
58
|
+
|
59
|
+
get "/", {}, {"HTTP_X_PJAX" => "true"}
|
60
|
+
body.should == container
|
61
|
+
end
|
32
62
|
end
|
33
63
|
|
34
|
-
context "
|
35
|
-
|
64
|
+
context "a pjaxified app, upon receiving a non-pjax request" do
|
65
|
+
before do
|
66
|
+
self.class.app = generate_app(:body => '<html><title>Hello</title><body><div data-pjax-container>World!</div></body></html>')
|
67
|
+
end
|
68
|
+
|
69
|
+
it "should return the original body" do
|
36
70
|
get "/"
|
37
71
|
body.should == '<html><title>Hello</title><body><div data-pjax-container>World!</div></body></html>'
|
38
72
|
end
|
39
73
|
|
40
|
-
it "should
|
74
|
+
it "should return the correct Content Length" do
|
41
75
|
get "/"
|
42
76
|
headers['Content-Length'].should == Rack::Utils.bytesize(body).to_s
|
43
77
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-pjax
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 3
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 5
|
9
|
-
-
|
10
|
-
version: 0.5.
|
9
|
+
- 4
|
10
|
+
version: 0.5.4
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Gert Goet
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-11-
|
18
|
+
date: 2011-11-06 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: rack
|
@@ -23,29 +23,29 @@ dependencies:
|
|
23
23
|
requirement: &id001 !ruby/object:Gem::Requirement
|
24
24
|
none: false
|
25
25
|
requirements:
|
26
|
-
- -
|
26
|
+
- - ~>
|
27
27
|
- !ruby/object:Gem::Version
|
28
|
-
hash:
|
28
|
+
hash: 9
|
29
29
|
segments:
|
30
30
|
- 1
|
31
|
-
-
|
32
|
-
version: "1.
|
31
|
+
- 3
|
32
|
+
version: "1.3"
|
33
33
|
type: :runtime
|
34
34
|
version_requirements: *id001
|
35
35
|
- !ruby/object:Gem::Dependency
|
36
|
-
name:
|
36
|
+
name: hpricot
|
37
37
|
prerelease: false
|
38
38
|
requirement: &id002 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ~>
|
42
42
|
- !ruby/object:Gem::Version
|
43
|
-
hash:
|
43
|
+
hash: 55
|
44
44
|
segments:
|
45
|
-
- 1
|
46
|
-
- 5
|
47
45
|
- 0
|
48
|
-
|
46
|
+
- 8
|
47
|
+
- 4
|
48
|
+
version: 0.8.4
|
49
49
|
type: :runtime
|
50
50
|
version_requirements: *id002
|
51
51
|
description: Serve pjax responses through rack middleware
|