get_schwifty 0.1.1 → 0.2.0

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
  SHA1:
3
- metadata.gz: efeaf60c498883cadecc040b078c405a9490f86c
4
- data.tar.gz: ae6c6b127933885076ac0144a2a0566eaf394fe8
3
+ metadata.gz: 855d3f63a838d8f8c920bd22a27b92cbb6a2efe7
4
+ data.tar.gz: 5e6f2c371471b7c93b7fda2fb61f125bd9ca5e3d
5
5
  SHA512:
6
- metadata.gz: aeac7f9f8c7a71738f5b5bdc3d7045e10e984a2beb116071a92bd9669f8264e453fee18437c8f781f9f6136c143cd5eb1cf2297bbcb6ceb4a21dc7ec6f857237
7
- data.tar.gz: d231ecb37e5ee75fa47986c7ad9896dc08a9ba9a92d6c9fac943ca22ac9d3df54cf1a718aa1563db56af3400b22c87c324251ef87ae33f8f1bf98fb3e478a62e
6
+ metadata.gz: e02f65f4ad6989bbfe0f097cc2c79a56dfc8969db8f02a21b645d89bb0ec8b5ecb6d416c05f76b407712eb91ccb126926e52c56be3ee4341ade620acdb6168bf
7
+ data.tar.gz: 84338811e747a31467807fa278e59092c06bb2ebd3a502aface746d6e04cf62a7767adc2b24826fb525c3497777ba1f9ca8cc6b371958e1acd33b5075022e0cc
data/README.md CHANGED
@@ -1,15 +1,17 @@
1
- # GetSchwifty
1
+ # get_schwifty
2
2
  >Oh, yeah!
3
3
  You gotta get schwifty.
4
4
  You gotta get schwifty in here.
5
5
 
6
- GetSchwifty is a Rails plugin for rendering slow view partials in a background job and sending them to the client over ActionCable websockets.
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
+ [![CI](https://travis-ci.org/danielwestendorf/get_schwifty.svg?branch=master)](https://travis-ci.org/danielwestendorf/get_schwifty) [![Gem Version](https://badge.fury.io/rb/get_schwifty.svg)](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
- GetSchwifty is all about quick responses by utilizing background jobs to do the rendering for your and delivering it to the client with ActionCable.
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
- ![](https://travis-ci.org/danielwestendorf/get_schwifty.svg?branch=master)
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 GetSchwifty Cable
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 GetSchwifty cable.
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. GetSchwifty uses a similar routing syntax to Rails routes of `cablecontrollername#action`.
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. GetSchwifty will make any identefiers or params made available in the channel subscription within your cables. Let's take a look at an example.
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 'bundler/setup'
4
+ require "bundler/setup"
3
5
  rescue LoadError
4
- puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ puts "You must `gem install bundler` and `bundle install` to run rake tasks"
5
7
  end
6
8
 
7
- require 'rdoc/task'
9
+ require "rdoc/task"
8
10
 
9
11
  RDoc::Task.new(:rdoc) do |rdoc|
10
- rdoc.rdoc_dir = 'rdoc'
11
- rdoc.title = 'GetSchwifty'
12
- rdoc.options << '--line-numbers'
13
- rdoc.rdoc_files.include('README.md')
14
- rdoc.rdoc_files.include('lib/**/*.rb')
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 << 'test'
28
- t.pattern = 'test/**/*_test.rb'
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(html) {
27
- dispatchEvent('render:before', el, { schwiftyJobId: schwiftyJobId, html: html });
28
-
29
- var newContent = document.createRange().createContextualFragment(html);
30
- var newEl = newContent.firstChild;
31
- el.parentNode.replaceChild(newContent, el)
32
-
33
- dispatchEvent('render:after', newEl, { schwiftyJobId: schwiftyJobId, html: html });
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
- application %(config.autoload_paths << config.root.join("app", "cables"))
37
+ application %(config.autoload_paths << config.root.join("app", "cables"))
35
38
  end
36
39
 
37
40
  def copy_get_schwifty_channel_js
@@ -1,5 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Base cable class to inherit from when getting schwifty
2
4
  class BaseCable < GetSchwifty::Cable::Base
5
+ include Rails.application.routes.url_helpers
3
6
  # Access to pundit helper methods for authorization
4
7
  # include Pundit
5
8
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Channel for handling schwifty subscriptions
2
4
  class GetSchwiftyChannel < ApplicationCable::Channel
3
5
  include GetSchwifty::Channel
@@ -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(*%w(app views cables)).to_s
5
+ prepend_view_path Rails.root.join("app", "views", "cables").to_s
4
6
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # GetSchwifty configuration initializer
2
4
  # Use this file to configure GetSchwifty for your needs
3
5
  GetSchwifty.configure do |config|
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Job for running schwifty cables in ActiveJob
2
4
  class GetSchwiftyRunnerJob < ApplicationJob
3
5
  include GetSchwifty::Job
@@ -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
- config.assets.paths += [File.expand_path("../../app/assets/javascripts", __FILE__)] if config.respond_to?(:assets)
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 |app|
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
- GetSchwiftyController.renderer.new.render(*args).squish
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
@@ -1,4 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GetSchwifty
4
+ # :nodoc
2
5
  module Channel
3
6
  def subscribed
4
7
  reject if route.blank?
@@ -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, expires_in: 10.minutes)
6
+ Rails.cache.write("get_schwifty:#{id}", route)
6
7
 
7
8
  opts = {
8
9
  "data-get-schwifty" => id
@@ -1,4 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GetSchwifty
4
+ # :nodoc
2
5
  module Job
3
6
  def perform(schwifty_job_id, klass_name, method, params, *identifiers)
4
7
  klass_name.constantize.new(schwifty_job_id, params, Hash[*identifiers]).send(method)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GetSchwifty
2
- VERSION = "0.1.1"
4
+ VERSION = "0.2.0"
3
5
  end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  # desc "Explaining what the task does"
2
3
  # task :get_schwifty do
3
4
  # # Task goes here
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.1.1
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-04 00:00:00.000000000 Z
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
- description: "\n Offload the rendering of slow view partials with
98
- ActiveJob and ActionCable to reduce page load times.\n "
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.13
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.