diff --git a/Gemfile.lock b/Gemfile.lock index 83eb32d..0767a45 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - get_schwifty (0.1.1) + get_schwifty (0.2.0) rails (> 5) GEM @@ -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) @@ -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) @@ -155,7 +155,7 @@ DEPENDENCIES get_schwifty! puma redis - rubocop (~> 0.49) + rubocop (= 0.49) selenium-webdriver sqlite3 diff --git a/README.md b/README.md index 8e50322..135114c 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/app/assets/javascripts/get_schwifty.js b/app/assets/javascripts/get_schwifty.js index 06f9196..099c665 100644 --- a/app/assets/javascripts/get_schwifty.js +++ b/app/assets/javascripts/get_schwifty.js @@ -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) { @@ -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(); diff --git a/get_schwifty.gemspec b/get_schwifty.gemspec index 6fc09d2..7a89fe7 100644 --- a/get_schwifty.gemspec +++ b/get_schwifty.gemspec @@ -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 diff --git a/lib/generators/templates/cables/base_cable.rb b/lib/generators/templates/cables/base_cable.rb index 0b5d4cb..25f9db3 100644 --- a/lib/generators/templates/cables/base_cable.rb +++ b/lib/generators/templates/cables/base_cable.rb @@ -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 diff --git a/lib/get_schwifty/cable/base.rb b/lib/get_schwifty/cable/base.rb index c05d7cd..0407da6 100644 --- a/lib/get_schwifty/cable/base.rb +++ b/lib/get_schwifty/cable/base.rb @@ -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 diff --git a/lib/get_schwifty/version.rb b/lib/get_schwifty/version.rb index 4eab9c4..24b0ec4 100644 --- a/lib/get_schwifty/version.rb +++ b/lib/get_schwifty/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module GetSchwifty - VERSION = "0.1.1" + VERSION = "0.2.0" end diff --git a/test/dummy/app/cables/base_cable.rb b/test/dummy/app/cables/base_cable.rb index 019806b..7723400 100644 --- a/test/dummy/app/cables/base_cable.rb +++ b/test/dummy/app/cables/base_cable.rb @@ -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 diff --git a/test/dummy/app/cables/demo_cable.rb b/test/dummy/app/cables/demo_cable.rb new file mode 100644 index 0000000..35e957d --- /dev/null +++ b/test/dummy/app/cables/demo_cable.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +class DemoCable < BaseCable + def build + sleep 5 + + redirect demo_path(id: DateTime.current) + end +end diff --git a/test/dummy/app/channels/application_cable/connection.rb b/test/dummy/app/channels/application_cable/connection.rb index ebcec5d..2d565fb 100644 --- a/test/dummy/app/channels/application_cable/connection.rb +++ b/test/dummy/app/channels/application_cable/connection.rb @@ -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 diff --git a/test/dummy/app/controllers/demos_controller.rb b/test/dummy/app/controllers/demos_controller.rb new file mode 100644 index 0000000..cded2cc --- /dev/null +++ b/test/dummy/app/controllers/demos_controller.rb @@ -0,0 +1,5 @@ +class DemosController < ApplicationController + def index; end + + def show; end +end diff --git a/test/dummy/app/views/demos/index.html.erb b/test/dummy/app/views/demos/index.html.erb new file mode 100644 index 0000000..2cd9de0 --- /dev/null +++ b/test/dummy/app/views/demos/index.html.erb @@ -0,0 +1,3 @@ +<%= get_schwifty "demo#build" do %> +

Inserting demo data...

+<% end %> diff --git a/test/dummy/app/views/demos/show.html.erb b/test/dummy/app/views/demos/show.html.erb new file mode 100644 index 0000000..1102c6b --- /dev/null +++ b/test/dummy/app/views/demos/show.html.erb @@ -0,0 +1 @@ +

Demo data was inserted

diff --git a/test/dummy/config/routes.rb b/test/dummy/config/routes.rb index 2d39ef9..bca98ff 100644 --- a/test/dummy/config/routes.rb +++ b/test/dummy/config/routes.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true Rails.application.routes.draw do + resources :demos, only: %i[index show] + root "calculators#index" end diff --git a/test/system_test.rb b/test/system_test.rb index 471a1dc..d18be21 100644 --- a/test/system_test.rb +++ b/test/system_test.rb @@ -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