get_schwifty 0.1.1 → 0.2.0
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.
- checksums.yaml +4 -4
- data/README.md +10 -9
- data/Rakefile +14 -18
- data/app/assets/javascripts/get_schwifty.js +26 -9
- data/lib/generators/get_schwifty/cable_generator.rb +3 -0
- data/lib/generators/get_schwifty/install_generator.rb +4 -1
- data/lib/generators/templates/cables/base_cable.rb +3 -0
- data/lib/generators/templates/channels/get_schwifty_channel.rb +2 -0
- data/lib/generators/templates/controllers/get_schwifty_controller.rb +3 -1
- data/lib/generators/templates/get_schwifty.rb +2 -0
- data/lib/generators/templates/jobs/get_schwifty_runner_job.rb +2 -0
- data/lib/get_schwifty.rb +7 -2
- data/lib/get_schwifty/cable/base.rb +13 -1
- data/lib/get_schwifty/channel.rb +3 -0
- data/lib/get_schwifty/helper.rb +2 -1
- data/lib/get_schwifty/job.rb +3 -0
- data/lib/get_schwifty/version.rb +3 -1
- data/lib/tasks/get_schwifty_tasks.rake +1 -0
- metadata +19 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 855d3f63a838d8f8c920bd22a27b92cbb6a2efe7
|
4
|
+
data.tar.gz: 5e6f2c371471b7c93b7fda2fb61f125bd9ca5e3d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e02f65f4ad6989bbfe0f097cc2c79a56dfc8969db8f02a21b645d89bb0ec8b5ecb6d416c05f76b407712eb91ccb126926e52c56be3ee4341ade620acdb6168bf
|
7
|
+
data.tar.gz: 84338811e747a31467807fa278e59092c06bb2ebd3a502aface746d6e04cf62a7767adc2b24826fb525c3497777ba1f9ca8cc6b371958e1acd33b5075022e0cc
|
data/README.md
CHANGED
@@ -1,15 +1,17 @@
|
|
1
|
-
#
|
1
|
+
# get_schwifty
|
2
2
|
>Oh, yeah!
|
3
3
|
You gotta get schwifty.
|
4
4
|
You gotta get schwifty in here.
|
5
5
|
|
6
|
-
|
6
|
+
get_schwifty is a Rails plugin for rendering slow view partials in a background job and sending them to the client over ActionCable websockets.
|
7
|
+
|
8
|
+
[](https://travis-ci.org/danielwestendorf/get_schwifty) [](https://badge.fury.io/rb/get_schwifty)
|
7
9
|
|
8
10
|
## Justification
|
9
11
|
|
10
12
|
Slow-to-render HTML elements can be expensive (hosting) and unavoidable (technical debt, slow libs, expensive db queries, etc). Rendering in-line in your web server consumes a connection which could be serving other clients. Sometimes it's better to just return a minimal page quickly, and let the data backfill as it's generated.
|
11
13
|
|
12
|
-
|
14
|
+
get_schwifty is all about quick responses by utilizing background jobs to do the rendering for your and delivering it to the client with ActionCable.
|
13
15
|
|
14
16
|
## Caveats
|
15
17
|
|
@@ -21,7 +23,6 @@ get_schwifty was extracted from [HireLoop.io](https://www.hireloop.io), a platfo
|
|
21
23
|
|
22
24
|
Make hiring delightful by closing the loop between hiring managers and every applicant. Automate the trival tasks associated with screening job applicants, lowering the barrier for easy and clear communication.
|
23
25
|
|
24
|
-

|
25
26
|
|
26
27
|
## Installation
|
27
28
|
Add this line to your application's Gemfile:
|
@@ -49,12 +50,12 @@ Follow the instructions printed.
|
|
49
50
|
|
50
51
|
## Usage
|
51
52
|
|
52
|
-
Generate your first
|
53
|
+
Generate your first get_schwifty Cable
|
53
54
|
```bash
|
54
55
|
$ rails generate get_schwifty:cable Calculator fibonacci
|
55
56
|
```
|
56
57
|
|
57
|
-
Cables are a place to put your data access logic. Think Controllers for each
|
58
|
+
Cables are a place to put your data access logic. Think Controllers for each get_schwifty cable.
|
58
59
|
```ruby
|
59
60
|
# app/cables/calculator_cable.rb
|
60
61
|
|
@@ -74,7 +75,7 @@ class CalculatorCable < BaseCable
|
|
74
75
|
end
|
75
76
|
```
|
76
77
|
|
77
|
-
When the data has been queried/generated, the partial is rendered. `stream` is a wrapper around the normal Rails `render`.
|
78
|
+
When the data has been queried/generated, the partial is rendered. `stream` is a wrapper around the normal Rails `render`. Want to redirect to another location? use the pass the path or URL to the `redirect` method.
|
78
79
|
|
79
80
|
```erb
|
80
81
|
# app/views/cables/calculator/_fibonacci.html.erb
|
@@ -84,7 +85,7 @@ When the data has been queried/generated, the partial is rendered. `stream` is a
|
|
84
85
|
|
85
86
|
```
|
86
87
|
|
87
|
-
Lastly, we need to tell the app where to render this chunk of HTML.
|
88
|
+
Lastly, we need to tell the app where to render this chunk of HTML. get_schwifty uses a similar routing syntax to Rails routes of `cablecontrollername#action`.
|
88
89
|
|
89
90
|
```erb
|
90
91
|
# app/views/yourpage.html.erb
|
@@ -102,7 +103,7 @@ Load the page, and you should get the whole page back quickly, while the Fibonac
|
|
102
103
|
|
103
104
|
### Identifiers & Params
|
104
105
|
|
105
|
-
Random data isn't very useful. Unauthenticated data isn't very cool either. Usually in Rails data scoping belongs starts at a current_user method.
|
106
|
+
Random data isn't very useful. Unauthenticated data isn't very cool either. Usually in Rails data scoping belongs starts at a current_user method. get_schwifty will make any identefiers or params made available in the channel subscription within your cables. Let's take a look at an example.
|
106
107
|
|
107
108
|
#### Identifiers
|
108
109
|
Here we'll find the current user. This is copied form the [Rails Guides](http://guides.rubyonrails.org/action_cable_overview.html#connection-setup).
|
data/Rakefile
CHANGED
@@ -1,33 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
begin
|
2
|
-
require
|
4
|
+
require "bundler/setup"
|
3
5
|
rescue LoadError
|
4
|
-
puts
|
6
|
+
puts "You must `gem install bundler` and `bundle install` to run rake tasks"
|
5
7
|
end
|
6
8
|
|
7
|
-
require
|
9
|
+
require "rdoc/task"
|
8
10
|
|
9
11
|
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
-
rdoc.rdoc_dir =
|
11
|
-
rdoc.title =
|
12
|
-
rdoc.options <<
|
13
|
-
rdoc.rdoc_files.include(
|
14
|
-
rdoc.rdoc_files.include(
|
12
|
+
rdoc.rdoc_dir = "rdoc"
|
13
|
+
rdoc.title = "GetSchwifty"
|
14
|
+
rdoc.options << "--line-numbers"
|
15
|
+
rdoc.rdoc_files.include("README.md")
|
16
|
+
rdoc.rdoc_files.include("lib/**/*.rb")
|
15
17
|
end
|
16
18
|
|
19
|
+
require "bundler/gem_tasks"
|
17
20
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
require 'bundler/gem_tasks'
|
23
|
-
|
24
|
-
require 'rake/testtask'
|
21
|
+
require "rake/testtask"
|
25
22
|
|
26
23
|
Rake::TestTask.new(:test) do |t|
|
27
|
-
t.libs <<
|
28
|
-
t.pattern =
|
24
|
+
t.libs << "test"
|
25
|
+
t.pattern = "test/**/*_test.rb"
|
29
26
|
t.verbose = false
|
30
27
|
end
|
31
28
|
|
32
|
-
|
33
29
|
task default: :test
|
@@ -8,7 +8,23 @@ GetSchwifty = function(app) {
|
|
8
8
|
bubbles: true
|
9
9
|
});
|
10
10
|
el.dispatchEvent(event)
|
11
|
-
}
|
11
|
+
}
|
12
|
+
|
13
|
+
function replaceContent(schwiftyJobId, oldEl, response) {
|
14
|
+
dispatchEvent('render:before', oldEl, { schwiftyJobId: schwiftyJobId, response: response });
|
15
|
+
|
16
|
+
var newContent = document.createRange().createContextualFragment(response.body);
|
17
|
+
var newEl = newContent.firstChild;
|
18
|
+
oldEl.parentNode.replaceChild(newContent, oldEl);
|
19
|
+
|
20
|
+
dispatchEvent('render:after', newEl, { schwiftyJobId: schwiftyJobId, html: response });
|
21
|
+
}
|
22
|
+
|
23
|
+
function redirectTo(schwiftyJobId, oldEl, response) {
|
24
|
+
dispatchEvent('redirect:before', oldEl, { schwiftyJobId: schwiftyJobId, response: response });
|
25
|
+
|
26
|
+
window.location = response.body;
|
27
|
+
}
|
12
28
|
|
13
29
|
return {
|
14
30
|
showMeWhatYouGot: function(selector) {
|
@@ -23,14 +39,15 @@ GetSchwifty = function(app) {
|
|
23
39
|
var subscription = Object.assign({ channel: "GetSchwiftyChannel", id: schwiftyJobId }, schwiftyParams);
|
24
40
|
|
25
41
|
var cable = _App.cable.subscriptions.create(subscription, {
|
26
|
-
received: function(
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
42
|
+
received: function(response) {
|
43
|
+
|
44
|
+
switch (response.status) {
|
45
|
+
case 302:
|
46
|
+
redirectTo(schwiftyJobId, el, response);
|
47
|
+
break;
|
48
|
+
default:
|
49
|
+
replaceContent(schwiftyJobId, el, response);
|
50
|
+
}
|
34
51
|
|
35
52
|
cable.perform('rendered');
|
36
53
|
cable.unsubscribe();
|
@@ -1,5 +1,8 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module GetSchwifty
|
2
4
|
module Generators
|
5
|
+
# :nodoc
|
3
6
|
class CableGenerator < Rails::Generators::NamedBase
|
4
7
|
source_root File.expand_path("../../templates", __FILE__)
|
5
8
|
argument :actions, type: :array, default: [], banner: "action action"
|
@@ -1,5 +1,8 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module GetSchwifty
|
2
4
|
module Generators
|
5
|
+
# :nodoc
|
3
6
|
class InstallGenerator < Rails::Generators::Base
|
4
7
|
source_root File.expand_path("../../templates", __FILE__)
|
5
8
|
|
@@ -31,7 +34,7 @@ module GetSchwifty
|
|
31
34
|
end
|
32
35
|
|
33
36
|
def autoload_cables_path
|
34
|
-
|
37
|
+
application %(config.autoload_paths << config.root.join("app", "cables"))
|
35
38
|
end
|
36
39
|
|
37
40
|
def copy_get_schwifty_channel_js
|
@@ -1,4 +1,6 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# Used for rendering partials in schwifty background jobs
|
2
4
|
class GetSchwiftyController < ApplicationController
|
3
|
-
prepend_view_path Rails.root.join(
|
5
|
+
prepend_view_path Rails.root.join("app", "views", "cables").to_s
|
4
6
|
end
|
data/lib/get_schwifty.rb
CHANGED
@@ -1,8 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "get_schwifty/helper"
|
2
4
|
require "get_schwifty/channel"
|
3
5
|
require "get_schwifty/job"
|
4
6
|
require "get_schwifty/cable/base"
|
5
7
|
|
8
|
+
# :nodoc
|
6
9
|
module GetSchwifty
|
7
10
|
mattr_accessor :allow_rerender
|
8
11
|
@@allow_rerender = true
|
@@ -11,10 +14,12 @@ module GetSchwifty
|
|
11
14
|
yield self
|
12
15
|
end
|
13
16
|
|
17
|
+
# :nodoc
|
14
18
|
class Engine < ::Rails::Engine
|
15
|
-
|
19
|
+
asset_path = File.expand_path("../../app/assets/javascripts", __FILE__)
|
20
|
+
config.assets.paths += [asset_path] if config.respond_to?(:assets)
|
16
21
|
|
17
|
-
initializer :get_schwifty do |
|
22
|
+
initializer :get_schwifty do |_app|
|
18
23
|
ActiveSupport.on_load(:action_view) do
|
19
24
|
include GetSchwifty::Helper
|
20
25
|
end
|
@@ -1,5 +1,8 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module GetSchwifty
|
2
4
|
module Cable
|
5
|
+
# :nodoc
|
3
6
|
class Base
|
4
7
|
attr_reader :schwifty_job_id, :identifiers, :params
|
5
8
|
|
@@ -14,7 +17,16 @@ module GetSchwifty
|
|
14
17
|
def stream(*args)
|
15
18
|
ActionCable.server.broadcast(
|
16
19
|
schwifty_job_id,
|
17
|
-
|
20
|
+
status: 200,
|
21
|
+
body: GetSchwiftyController.renderer.new.render(*args).squish
|
22
|
+
)
|
23
|
+
end
|
24
|
+
|
25
|
+
def redirect(url)
|
26
|
+
ActionCable.server.broadcast(
|
27
|
+
schwifty_job_id,
|
28
|
+
status: 302,
|
29
|
+
body: url
|
18
30
|
)
|
19
31
|
end
|
20
32
|
end
|
data/lib/get_schwifty/channel.rb
CHANGED
data/lib/get_schwifty/helper.rb
CHANGED
@@ -1,8 +1,9 @@
|
|
1
1
|
module GetSchwifty
|
2
|
+
# :nodoc
|
2
3
|
module Helper
|
3
4
|
def get_schwifty(route, params = nil, &blk)
|
4
5
|
id = SecureRandom.hex
|
5
|
-
Rails.cache.write("get_schwifty:#{id}", route
|
6
|
+
Rails.cache.write("get_schwifty:#{id}", route)
|
6
7
|
|
7
8
|
opts = {
|
8
9
|
"data-get-schwifty" => id
|
data/lib/get_schwifty/job.rb
CHANGED
data/lib/get_schwifty/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: get_schwifty
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel Westendorf
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-10-
|
11
|
+
date: 2017-10-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -94,8 +94,22 @@ dependencies:
|
|
94
94
|
- - ">="
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '0'
|
97
|
-
|
98
|
-
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rubocop
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - '='
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0.49'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - '='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0.49'
|
111
|
+
description: Offload the rendering of slow view partials with ActiveJob and ActionCable
|
112
|
+
to reduce page load times.
|
99
113
|
email:
|
100
114
|
- daniel@prowestech.com
|
101
115
|
executables: []
|
@@ -145,7 +159,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
145
159
|
version: '0'
|
146
160
|
requirements: []
|
147
161
|
rubyforge_project:
|
148
|
-
rubygems_version: 2.6.
|
162
|
+
rubygems_version: 2.6.14
|
149
163
|
signing_key:
|
150
164
|
specification_version: 4
|
151
165
|
summary: Render view partials with ActiveJob and ActionCable.
|