turbolinks_render 0.9.10 → 0.9.11

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7c4babe3e6089e042ce6205c746540f8a6614bcd3676cbf14548517b06410af4
4
- data.tar.gz: 6f6560db061ba14396011db0acfb0765ce2e4c8a63cefd0268c84dfa5729442f
3
+ metadata.gz: 60f02af004bd119d0d38ee2678d8128b93f1e6756c641c60972f7707e755593d
4
+ data.tar.gz: cee50736d7790b5f9e795c4ad2421274cc3020c1b47e5ed56e48781ccb81784e
5
5
  SHA512:
6
- metadata.gz: c53e0d03adf0fc750c6f9184302bd1c583e0f1cf41f39c6eed11d3c0ee025c09098074cbd9c07c582a776edfe29395ce5d8a0507dcab2e166d4ce0b752440db9
7
- data.tar.gz: f3103e0d16097ca7a81a56e51ce94c7fe9290cd17546cc8bbd9cea10f061f4afc20adb201b114d41bf44819a2383065fc9ac7381f9ab3028ce2ea61604ee4aea
6
+ metadata.gz: 3dd95e4590be8d63d6c0b57ba1ee0199d5d222537a3fb623379774ff433b6bbfd2aeb56a5ad7a1e9d063431a4c3cd2450bc9630ea014b6e9274eb1f50250d771
7
+ data.tar.gz: f31224ef7f37349d795982627ad50bb9902eebef345c62afa3e0f8210f31b16e2ae8e313fef79a691e67a03dc0814a8a007a3b4ff700d98316ea160af7435d2e
data/README.md CHANGED
@@ -30,7 +30,7 @@ By default, `render` will be handled by Turbolinks if these conditions are met:
30
30
 
31
31
  When these conditions are met and `render` is used:
32
32
 
33
- - The body of the page is replaced with the rendered content with Javascript
33
+ - The page content is replaced with the rendered content with Javascript. It uses Turbolinks to do this replacement, so it is pretty smart about keeping the `<head>` when it's identical, executing `<script>` elements, etc.
34
34
  - An event `turbolinks:load` is dispatched
35
35
 
36
36
  You can disable turbolinks on a given request with:
@@ -1,55 +1,39 @@
1
1
  module TurbolinksRender
2
2
  class Middleware
3
- def initialize(app)
4
- @app = app
5
- end
6
-
7
- def call(env)
8
- @request = Rack::Request.new(env)
9
- @request.set_header('X-Turbolinks-Render-Candidate', turbolinks_response_candidate?)
10
-
11
- @status, @headers, @response = @app.call(env)
12
-
13
- return [@status, @headers, @response] if file? || (!@response.respond_to?(:body) && !@response.respond_to?(:[]))
14
-
15
- body = @response.respond_to?(:body) ? @response.body : @response[0]
16
- body = render_body_with_turbolinks(body) if render_with_turbolinks?
17
- [@status, @headers, [body]]
18
- end
19
-
20
- private
3
+ Request = Struct.new(:request) do
4
+ def candidate_for_turbolinks?
5
+ request.xhr? && !request.get?
6
+ end
21
7
 
22
- def render_body_with_turbolinks(body)
23
- @headers["Content-Type"] = 'text/javascript'
24
- build_turbolinks_response_to_render(body).tap do |turbolinks_body|
25
- @headers["Content-Length"] = turbolinks_body.bytesize
8
+ def turbolinks_render_option
9
+ @turbolinks_render_option ||= request.get_header('X-Turbolinks-Render-Option')
26
10
  end
27
11
  end
28
12
 
29
- def file?
30
- @headers["Content-Transfer-Encoding"] == "binary"
31
- end
13
+ Response = Struct.new(:status, :headers, :response) do
14
+ def candidate_for_turbolinks?
15
+ html_response?
16
+ end
32
17
 
33
- def render_with_turbolinks?
34
- turbolinks_response_candidate? && html_response? && (turbolinks_render_option ||
35
- (render_with_turbolinks_by_default? && turbolinks_render_option != false))
36
- end
18
+ def turbolinks_body
19
+ @turbolinks_body ||= js_code_to_render_html(body)
20
+ end
37
21
 
38
- def turbolinks_render_option
39
- @request.get_header('X-Turbolinks-Render-Option')
40
- end
22
+ private
41
23
 
42
- def turbolinks_response_candidate?
43
- @request.xhr? && !@request.get?
44
- end
24
+ def html_response?
25
+ headers['Content-Type'] =~ /text\/html/
26
+ end
45
27
 
46
- def html_response?
47
- @headers['Content-Type'] =~ /text\/html/
48
- end
28
+ def body
29
+ body = ''
30
+ response.each {|part| body << part}
31
+ body
32
+ end
49
33
 
50
- def build_turbolinks_response_to_render(html)
51
- escaped_html = ActionController::Base.helpers.j(html)
52
- <<-JS
34
+ def js_code_to_render_html(html)
35
+ escaped_html = ActionController::Base.helpers.j(html)
36
+ <<-JS
53
37
  (function(){
54
38
  function renderWithTurbolinks(htmlContent){
55
39
  var currentSnapshot = Turbolinks.Snapshot.fromHTMLElement(document.documentElement);
@@ -58,27 +42,50 @@ module TurbolinksRender
58
42
  var nullDelegate = {viewInvalidated: nullCallback, viewWillRender: nullCallback, viewRendered: nullCallback};
59
43
 
60
44
  var renderer = new Turbolinks.SnapshotRenderer(currentSnapshot, newSpanshot, false);
61
- renderer.delegate = nullDelegate;
62
- if(renderer.shouldRender()){
63
- renderer.render(nullCallback);
64
- }
65
- else{
45
+ if(!renderer.shouldRender()){
66
46
  renderer = new Turbolinks.ErrorRenderer(htmlContent);
67
- renderer.delegate = nullDelegate;
68
- renderer.render(nullCallback);
69
47
  }
48
+ renderer.delegate = nullDelegate;
49
+ renderer.render(nullCallback);
70
50
  }
71
51
  Turbolinks.clearCache();
72
52
  renderWithTurbolinks("#{escaped_html}");
73
53
  Turbolinks.dispatch('turbolinks:load');
74
54
  window.scroll(0, 0);
75
55
  })();
76
- JS
56
+ JS
57
+ end
58
+ end
59
+
60
+ def initialize(app)
61
+ @app = app
62
+ end
63
+
64
+ def call(env)
65
+ rack_request = Rack::Request.new(env)
66
+ request = Request.new(rack_request)
67
+ rack_request.set_header('X-Turbolinks-Render-Candidate', request.candidate_for_turbolinks?)
68
+
69
+ rack_status, rack_headers, rack_response = @app.call(env)
70
+ response = Response.new(rack_status, rack_headers, rack_response)
71
+
72
+ return [rack_status, rack_headers, rack_response] unless render_with_turbolinks?(request, response)
73
+
74
+ rack_headers["Content-Type"] = 'text/javascript'
75
+ rack_headers["Content-Length"] = response.turbolinks_body.bytesize
76
+
77
+ [rack_status, rack_headers, [response.turbolinks_body]]
78
+ end
79
+
80
+ private
81
+
82
+ def render_with_turbolinks?(request, response)
83
+ request.candidate_for_turbolinks? && response.candidate_for_turbolinks? &&
84
+ (request.turbolinks_render_option || (render_with_turbolinks_by_default? && request.turbolinks_render_option != false))
77
85
  end
78
86
 
79
87
  def render_with_turbolinks_by_default?
80
88
  Rails.application.config.turbolinks_render.render_with_turbolinks_by_default
81
89
  end
82
-
83
90
  end
84
91
  end
@@ -1,3 +1,3 @@
1
1
  module TurbolinksRender
2
- VERSION = '0.9.10'
2
+ VERSION = '0.9.11'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: turbolinks_render
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.10
4
+ version: 0.9.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jorge Manrubia