turbolinks_render 0.9.0

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: 3771ad622437e3a92f8529d069c82ac3fc6ea4c9
4
+ data.tar.gz: 5fd8bddd612bf72fde74e239c68fcbc1a2db10d1
5
+ SHA512:
6
+ metadata.gz: 88712b8d615cc039a63cc10386fca0d72e73ffb3977d2099c5c6a18cc72bfcfdf5fc80b323a687d17accc9ebc8e003e7ab3e5f6a2dd3ce5dffe98c5b6c22baf6
7
+ data.tar.gz: 43bd9209a484567f487e10d82de77be8d3aa2e50f583867881132542a6aa3a390c992f13210c16e398cda1221f0ba9808fadd3f093b9eb116f4babf5fb048b5a
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2018 Jorge Manrubia
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.md ADDED
@@ -0,0 +1,53 @@
1
+ # turbolinks_render
2
+
3
+ Use `render` in your Rails controllers and handle the response with Turbolinks.
4
+
5
+ Turbolinks supports [`redirect_to`](https://github.com/turbolinks/turbolinks/blob/master/README.md#redirecting-after-a-form-submission) out of the box. But `render` is not supported and you have to use [workarounds for common things like dealing with forms](https://github.com/turbolinks/turbolinks/issues/85). This gem aims to fix that.
6
+
7
+ ## Installation
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'turboilnks_render'
12
+ ```
13
+
14
+ And then execute:
15
+ ```bash
16
+ $ bundle
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ By default, when using `render`, the response is going to be handled by Turbolinks if these conditions are met:
22
+
23
+ - It's an ajax request
24
+ - It's not a `get` request
25
+ - It's not rendering json
26
+
27
+ You can disable turbolinks for a given response:
28
+
29
+ ```ruby
30
+ render turbolinks: false
31
+ ```
32
+
33
+ ### Configuration
34
+
35
+ You can invert the default behavior: never use turbolinks for rendering unless explicitly indicated. Create a file `config/initializers/turbolinks_render.rb` and toggle `turbolinks_render.render_with_turbolinks_by_default`:
36
+
37
+ ```ruby
38
+ Rails.application.config.turbolinks_render.render_with_turbolinks_by_default = false
39
+ ```
40
+
41
+ In this case, to use turbolinks in a given response you should write:
42
+
43
+ ```ruby
44
+ render turbolinks: true
45
+ ```
46
+
47
+ ## Credits
48
+
49
+ - [Implementation based on this idea by @nerdcave](https://github.com/turbolinks/turbolinks/issues/85#issuecomment-298347900).
50
+
51
+ ## License
52
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
53
+
data/Rakefile ADDED
@@ -0,0 +1,27 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Turbolinks::Rails::Render'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ require 'bundler/gem_tasks'
18
+
19
+ require 'rake/testtask'
20
+
21
+ Rake::TestTask.new(:test) do |t|
22
+ t.libs << 'test'
23
+ t.pattern = 'test/**/*_test.rb'
24
+ t.verbose = false
25
+ end
26
+
27
+ task default: :test
@@ -0,0 +1,16 @@
1
+ require 'turbolinks_render/version'
2
+ require 'turbolinks_render/rendering'
3
+
4
+ module TurbolinksRender
5
+ class Engine < ::Rails::Railtie
6
+ config.turbolinks_render = ActiveSupport::OrderedOptions.new
7
+ config.turbolinks_render.render_with_turbolinks_by_default = true
8
+
9
+ initializer :turbolinks_render do |app|
10
+ ActiveSupport.on_load(:action_controller) do
11
+ include Rendering
12
+ end
13
+ end
14
+ end
15
+ end
16
+
@@ -0,0 +1,56 @@
1
+ module TurbolinksRender
2
+ module Rendering
3
+ extend ActiveSupport::Concern
4
+
5
+ def render(*args, &block)
6
+ options = args.dup.extract_options!
7
+ if render_with_turbolinks?(options)
8
+ render_with_turbolinks(*args, &block)
9
+ else
10
+ super
11
+ end
12
+ end
13
+
14
+ private
15
+
16
+ def render_with_turbolinks?(options)
17
+ request_candidate_for_turbolinks? && !json_response?(options) &&
18
+ (options[:turbolinks] || (render_with_turbolinks_by_default? && options[:turbolinks] != false))
19
+ end
20
+
21
+ def request_candidate_for_turbolinks?
22
+ request.xhr? && !request.get?
23
+ end
24
+
25
+ def render_with_turbolinks_by_default?
26
+ Rails.application.config.turbolinks_render.render_with_turbolinks_by_default
27
+ end
28
+
29
+ def json_response?(options)
30
+ options[:json]
31
+ end
32
+
33
+ def render_with_turbolinks(*args, &block)
34
+ html = render_to_string(*args, &block)
35
+ self.response_body = build_turbolinks_response_to_render(html)
36
+ self.status = 200
37
+ response.content_type = 'text/javascript'
38
+ end
39
+
40
+ def build_turbolinks_response_to_render(html)
41
+ escaped_html = ActionController::Base.helpers.j(html)
42
+
43
+ <<-JS
44
+ (function(){
45
+ Turbolinks.clearCache();
46
+ var parser = new DOMParser();
47
+ var newDocument = parser.parseFromString("#{escaped_html}", "text/html");
48
+
49
+ document.documentElement.replaceChild(newDocument.body, document.body);
50
+ Turbolinks.dispatch('turbolinks:load');
51
+ window.scroll(0, 0);
52
+ })();
53
+ JS
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,3 @@
1
+ module TurbolinksRender
2
+ VERSION = '0.9.0'
3
+ end
metadata ADDED
@@ -0,0 +1,162 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: turbolinks_render
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.0
5
+ platform: ruby
6
+ authors:
7
+ - Jorge Manrubia
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-06-18 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: 5.2.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 5.2.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: turbolinks-source
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: sqlite3
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: capybara
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
+ - !ruby/object:Gem::Dependency
70
+ name: chromedriver-helper
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: selenium-webdriver
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: puma
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: turbolinks
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: Use Rails render with Turbolinks
126
+ email:
127
+ - jorge.manrubia@gmail.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - MIT-LICENSE
133
+ - README.md
134
+ - Rakefile
135
+ - lib/turbolinks_render.rb
136
+ - lib/turbolinks_render/rendering.rb
137
+ - lib/turbolinks_render/version.rb
138
+ homepage: https://github.com/jorgemanrubia/turboilnks_render
139
+ licenses:
140
+ - MIT
141
+ metadata: {}
142
+ post_install_message:
143
+ rdoc_options: []
144
+ require_paths:
145
+ - lib
146
+ required_ruby_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ required_rubygems_version: !ruby/object:Gem::Requirement
152
+ requirements:
153
+ - - ">="
154
+ - !ruby/object:Gem::Version
155
+ version: '0'
156
+ requirements: []
157
+ rubyforge_project:
158
+ rubygems_version: 2.6.13
159
+ signing_key:
160
+ specification_version: 4
161
+ summary: Use Rails render with Turbolinks
162
+ test_files: []