• This repository has been archived on 16/Nov/2018
  • Stars
    star
    190
  • Rank 203,739 (Top 5 %)
  • Language
    Ruby
  • License
    MIT License
  • Created about 7 years ago
  • Updated over 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Render portions of your Rails view in a background job queue. ActiveJob + ActionCable.

get_schwifty

Oh, yeah! You gotta get schwifty. You gotta get schwifty in here.

get_schwifty is a Rails plugin for rendering slow view partials in a background job and sending them to the client over ActionCable websockets.

CI Gem Version

Justification

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.

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.

Caveats

  • There is no gurantee that the content will load. If your background job queue get's backed up, you'll be not showing content anytime soon.

Extracted from HireLoop

get_schwifty was extracted from HireLoop.io, a platform for a more human hiring experience.

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.

Installation

Add this line to your application's Gemfile:

gem "get_schwifty"

And then execute:

$ bundle

Or install it yourself as:

$ gem install get_schwifty

Install the plugin

$ rails generate get_schwifty:install

Follow the instructions printed.

Usage

Generate your first get_schwifty Cable

$ rails generate get_schwifty:cable Calculator fibonacci

Cables are a place to put your data access logic. Think Controllers for each get_schwifty cable.

# app/cables/calculator_cable.rb

class CalculatorCable < BaseCable
  def fibonacci
    n = SecureRandom.rand(30..40)
    calculated = calculate_fibonacci(n)
    stream partial: "calculator/fibonacci", locals: { calculated: calculated, n: n }
  end

  private

  def calculate_fibonacci(n)
    return n if n <= 1
    calculate_fibonacci( n - 1 ) + calculate_fibonacci( n - 2 )
  end
end

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.

# app/views/cables/calculator/_fibonacci.html.erb
<p class="take-off-your-pants">
  Fibonacci of <%= n %> is <%= calculated %>
</p>

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.

# app/views/yourpage.html.erb
... Other HTML ...
<%= get_schwifty "calculator#fibonacci" do %>
  <p>
    This will get displayed while the cable is waiting to get schwifty.
    Maybe markup for a loading animation?
  </p>
<%- end %>
... Other HTML ...

Load the page, and you should get the whole page back quickly, while the Fibonacci comes later.

Identifiers & Params

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.

Identifiers

Here we'll find the current user. This is copied form the Rails Guides.

# app/channels/connection.rb
module ApplicationCable
  class Connection < ActionCable::Connection::Base
    identified_by :current_user

    def connect
      self.current_user = find_verified_user
    end

    private

    def find_verified_user
      if current_user = User.find_by(id: cookies.signed[:user_id])
        current_user
      else
        reject_unauthorized_connection
      end
    end
  end
end

Let's assume the User#fibonacci returns and integer to use in the CalculatorCable#fibonacci.

# app/cables/calculator_cable.rb

class CalculatorCable < BaseCable
  def fibonacci
    n = identifiers[:current_user]
    ... same code as before ..
  end
end

Similarly, any other identifiers created in the connection will follow suite and be made available in your cable. Additionally, you may want to add getter methods to BaseCable make accessing identifiers easier.

Params

Code reusability is the Rails way, so maybe we need to tweak how the cable's action is invoked dynamically, based on where the HTML is to be rendered. This is easy as pie, simply pass a hash of parameters to the get_schwifty view helper.

# app/views/yourpage.html.erb
... Other HTML ...
<%= get_schwifty "calculator#fibonacci", min_start: 3 do %>
  ... same as before ...
<%- end %>
... Other HTML ...

These params will be available in your cable action.

# app/cables/calculator_cable.rb

class CalculatorCable < BaseCable
  def fibonacci
    n = SecureRandom.rand(params[:min_start]..40)
    ... same code as before ..
  end
end

License

The gem is available as open source under the terms of the MIT License.

More Repositories

1

breezy-pdf-lite

HTML/CSS/JS in, PDF out, via Chrome
JavaScript
394
star
2

breezy_pdf_lite-ruby

Ruby middleware/client for breezy-pdf-lite, an HTML -> PDF service powered by Google Chrome
Ruby
56
star
3

Thumper

An OSX client of Subsonic
Ruby
48
star
4

HTML-Slide-Presenter

MacRuby application to display your HTML slideshows fullscreen w/Apple Remote Support
Objective-C
20
star
5

sinatra-subroutes

Subdomain based route matching
Ruby
12
star
6

WorkoutBook

Track you gym workouts with this localstorage Backbone app
JavaScript
11
star
7

devise-async-stretch

Move password stretching into a background job for fast user creation but while maintaining difficult to crack storage.
Ruby
8
star
8

multiple_ajax_upload

Ruby on Rails demo of uploading multiple files on one page using ajax. Displays errors and successful saves. Added support for mutliple file uploads.
JavaScript
8
star
9

engineering-app

Form for collecting web engineer job applications while providing an easy coding challenge to filter out the resume flingers
Ruby
6
star
10

breezy_pdf-ruby

Eazy HTML to PDF conversion without the headache
Ruby
5
star
11

Visify.js

A jQuery plugin for graphing byte data
JavaScript
4
star
12

robologger

Simple Web application to scan robocopy logs for errors, show them in a web page, and send notification emails. All written in Ruby
JavaScript
2
star
13

Houdini

Launch your rdesktop sessions from the OS X Menu bar
Ruby
2
star
14

QTKitBroken

A repo showing that Apple's own API's don't follow it's sandboxing guidelines.
Objective-C
2
star
15

breezy-pdf-lite-client-js

Javascript client for breezy-pdf-lite, an HTML -> PDF service powered by Google Chrome
JavaScript
2
star
16

ruby-PACgen

Ruby script to create multiple PAC files from an ERB Template
Ruby
1
star
17

breezy_pdf-ruby-demo

Demo app for BreezyPDF
CSS
1
star
18

get_schwifty_demo

Demo app for get_schwifty
Ruby
1
star
19

Triforce

Ruby
1
star
20

bartender

Ruby
1
star
21

Snake

Snake! An HTML5 clone of the classic game found on early mobile phones
CoffeeScript
1
star
22

raggio

A simple Ruby based web interface to manage a FreeRADIUS server
JavaScript
1
star
23

pdf-printer

JavaScript
1
star
24

nested_attributes_demo

JavaScript
1
star
25

jquery.greyscaleImage

Create a greyscale image rollover effect clientside
JavaScript
1
star
26

dnsimpler

A Simple DNSimple API wrapper which always provides the response you're looking for
Ruby
1
star