Skip to content
This repository has been archived by the owner on Nov 16, 2018. It is now read-only.

Commit

Permalink
Merge pull request #4 from danielwestendorf/status-codes
Browse files Browse the repository at this point in the history
Add status code support
  • Loading branch information
danielwestendorf authored Oct 13, 2017
2 parents 145b5b6 + d869dc3 commit 96b86e3
Show file tree
Hide file tree
Showing 15 changed files with 74 additions and 25 deletions.
10 changes: 5 additions & 5 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
get_schwifty (0.1.1)
get_schwifty (0.2.0)
rails (> 5)

GEM
Expand Down Expand Up @@ -60,7 +60,7 @@ GEM
ffi (~> 1.0, >= 1.0.11)
concurrent-ruby (1.0.5)
crass (1.0.2)
erubi (1.6.1)
erubi (1.7.0)
ffi (1.9.18)
globalid (0.4.0)
activesupport (>= 4.2.0)
Expand Down Expand Up @@ -116,11 +116,11 @@ GEM
rake
rake (12.1.0)
redis (3.3.3)
rubocop (0.50.0)
rubocop (0.49.0)
parallel (~> 1.10)
parser (>= 2.3.3.1, < 3.0)
powerpack (~> 0.1)
rainbow (>= 2.2.2, < 3.0)
rainbow (>= 1.99.1, < 3.0)
ruby-progressbar (~> 1.7)
unicode-display_width (~> 1.0, >= 1.0.1)
ruby-progressbar (1.9.0)
Expand Down Expand Up @@ -155,7 +155,7 @@ DEPENDENCIES
get_schwifty!
puma
redis
rubocop (~> 0.49)
rubocop (= 0.49)
selenium-webdriver
sqlite3

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class CalculatorCable < BaseCable
end
```

When the data has been queried/generated, the partial is rendered. `stream` is a wrapper around the normal Rails `render`.
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.

```erb
# app/views/cables/calculator/_fibonacci.html.erb
Expand Down
35 changes: 26 additions & 9 deletions app/assets/javascripts/get_schwifty.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,23 @@ GetSchwifty = function(app) {
bubbles: true
});
el.dispatchEvent(event)
};
}

function replaceContent(schwiftyJobId, oldEl, response) {
dispatchEvent('render:before', oldEl, { schwiftyJobId: schwiftyJobId, response: response });

var newContent = document.createRange().createContextualFragment(response.body);
var newEl = newContent.firstChild;
oldEl.parentNode.replaceChild(newContent, oldEl);

dispatchEvent('render:after', newEl, { schwiftyJobId: schwiftyJobId, html: response });
}

function redirectTo(schwiftyJobId, oldEl, response) {
dispatchEvent('redirect:before', oldEl, { schwiftyJobId: schwiftyJobId, response: response });

window.location = response.body;
}

return {
showMeWhatYouGot: function(selector) {
Expand All @@ -23,14 +39,15 @@ GetSchwifty = function(app) {
var subscription = Object.assign({ channel: "GetSchwiftyChannel", id: schwiftyJobId }, schwiftyParams);

var cable = _App.cable.subscriptions.create(subscription, {
received: function(html) {
dispatchEvent('render:before', el, { schwiftyJobId: schwiftyJobId, html: html });

var newContent = document.createRange().createContextualFragment(html);
var newEl = newContent.firstChild;
el.parentNode.replaceChild(newContent, el)

dispatchEvent('render:after', newEl, { schwiftyJobId: schwiftyJobId, html: html });
received: function(response) {

switch (response.status) {
case 302:
redirectTo(schwiftyJobId, el, response);
break;
default:
replaceContent(schwiftyJobId, el, response);
}

cable.perform('rendered');
cable.unsubscribe();
Expand Down
2 changes: 1 addition & 1 deletion get_schwifty.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ Gem::Specification.new do |s|
s.add_development_dependency "selenium-webdriver"
s.add_development_dependency "sqlite3"
s.add_development_dependency "redis"
s.add_development_dependency "rubocop", "~> 0.49"
s.add_development_dependency "rubocop", "0.49"
end
1 change: 1 addition & 0 deletions lib/generators/templates/cables/base_cable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# Base cable class to inherit from when getting schwifty
class BaseCable < GetSchwifty::Cable::Base
include Rails.application.routes.url_helpers
# Access to pundit helper methods for authorization
# include Pundit

Expand Down
11 changes: 10 additions & 1 deletion lib/get_schwifty/cable/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,16 @@ def initialize(schwifty_job_id, params, identifiers)
def stream(*args)
ActionCable.server.broadcast(
schwifty_job_id,
GetSchwiftyController.renderer.new.render(*args).squish
status: 200,
body: GetSchwiftyController.renderer.new.render(*args).squish
)
end

def redirect(url)
ActionCable.server.broadcast(
schwifty_job_id,
status: 302,
body: url
)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/get_schwifty/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module GetSchwifty
VERSION = "0.1.1"
VERSION = "0.2.0"
end
1 change: 1 addition & 0 deletions test/dummy/app/cables/base_cable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# Base cable class to inherit from when getting schwifty
class BaseCable < GetSchwifty::Cable::Base
include Rails.application.routes.url_helpers
# Access to pundit helper methods for authorization
# include Pundit

Expand Down
9 changes: 9 additions & 0 deletions test/dummy/app/cables/demo_cable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

class DemoCable < BaseCable
def build
sleep 5

redirect demo_path(id: DateTime.current)
end
end
8 changes: 1 addition & 7 deletions test/dummy/app/channels/application_cable/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,7 @@ def connect
private

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

if current_user
current_user
else
reject_unauthorized_connection
end
User.find_by(id: cookies.signed[:user_id])
end
end
end
5 changes: 5 additions & 0 deletions test/dummy/app/controllers/demos_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class DemosController < ApplicationController
def index; end

def show; end
end
3 changes: 3 additions & 0 deletions test/dummy/app/views/demos/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<%= get_schwifty "demo#build" do %>
<h1>Inserting demo data...</h1>
<% end %>
1 change: 1 addition & 0 deletions test/dummy/app/views/demos/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1 class="rendered">Demo data was inserted</h1>
2 changes: 2 additions & 0 deletions test/dummy/config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

Rails.application.routes.draw do
resources :demos, only: %i[index show]

root "calculators#index"
end
7 changes: 7 additions & 0 deletions test/system_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,11 @@ class SystemTest < ApplicationSystemTestCase
assert_selector ".double-x-schwifty-201"
end
end

test "redirection" do
visit demos_path

# find ".rendered", wait: 10
assert_selector ".rendered", wait: 10
end
end

0 comments on commit 96b86e3

Please sign in to comment.