Skip to content

Commit 8142c50

Browse files
committed
refactor: Move Sprocket JS files to Webpack
wip
1 parent 82a05a6 commit 8142c50

35 files changed

+51
-25
lines changed

app/assets/javascripts/application.js

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,6 @@
1313
//= require rails-timeago
1414
//= require locales/jquery.timeago.de.js
1515
//
16-
// app/assets
17-
// --> Include some assets first, as they are used by other assets.
18-
// --> Hence, the order specified here is important.
19-
//
20-
// 1. Some common base functions and monkey patches
21-
//= require base
22-
// 2. Programming groups are required by "channels/synchronized_editor_channel.js"
23-
//= require programming_groups
24-
// 3. Some channels are required by "editor/editor.js.erb"
25-
//= require_tree ./channels
26-
//
27-
// All remaining assets are loaded in alphabetical order
28-
//= require_tree .
29-
//
3016
// Finally, we dispatch a custom event to signal that all assets are loaded.
3117
// This is used by our custom migration for Turbo to trigger the `turbo-migration:load` event
3218
const sprocketsLoad = new Event('sprockets:load');

app/javascript/application.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ window.ace = ace; // Publish ace in global namespace
8989
import '@hotwired/turbo-rails';
9090
import './turbo-migration';
9191

92+
// ActionCable
93+
import "@rails/actioncable"
94+
import "./channels"
95+
9296
// Import of Rails sprocket assets with minimal changes. After the initial migration these files should
9397
// be moved to an appropriate place after some adjustments to fit modern Rails JS.
9498
import 'sprocket_asset_import';

app/javascript/channels/consumer.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Action Cable provides the framework to deal with WebSockets in Rails.
2+
// You can generate new channels where WebSocket features live using the `bin/rails generate channel` command.
3+
4+
import { createConsumer } from "@rails/actioncable"
5+
6+
export default createConsumer()

app/javascript/channels/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Import all the channels to be used by Action Cable
2+
const channels = require.context(".", true, /_channel\.js$/)
3+
channels.keys().forEach(channels)

app/assets/javascripts/channels/la_exercises.js renamed to app/javascript/channels/la_exercises_channel.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import consumer from "./consumer"
2+
13
$(document).on('turbo-migration:load', function() {
24
if ($.isController('exercises') && $('.teacher_dashboard').isPresent()) {
35

@@ -18,7 +20,7 @@ $(document).on('turbo-migration:load', function() {
1820
const specific_channel = { channel: "LaExercisesChannel", exercise_id: exercise_id, study_group_id: study_group_id };
1921

2022

21-
App.la_exercise = App.cable.subscriptions.create(specific_channel, {
23+
consumer.subscriptions.create(specific_channel, {
2224
connected: function () {
2325
// Called when the subscription is ready for use on the server
2426
},

app/assets/javascripts/channels/pg_matching_channel.js renamed to app/javascript/channels/pg_matching_channel.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
import consumer from "./consumer";
2+
13
$(document).on('turbo-migration:load', function () {
24

35
if ($.isController('programming_groups') && window.location.pathname.includes('programming_groups/new')) {
46
const matching_page = $('#matching');
57
const exercise_id = matching_page.data('exercise-id');
68
const specific_channel = { channel: "PgMatchingChannel", exercise_id: exercise_id};
79

8-
App.pg_matching = App.cable.subscriptions.create(specific_channel, {
10+
consumer.subscriptions.create(specific_channel, {
911
connected() {
1012
// Called when the subscription is ready for use on the server
1113
},

app/assets/javascripts/channels/synchronized_editor_channel.js renamed to app/javascript/channels/synchronized_editor_channel.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import consumer from "./consumer"
2+
import ProgrammingGroups from '../sprocket_asset_import/programming_groups';
3+
import CodeOceanEditor from '../sprocket_asset_import/editor/evaluation';
4+
15
$(document).on('turbo-migration:load', function () {
26

37
if (window.location.pathname.includes('/implement')) {
@@ -6,7 +10,7 @@ $(document).on('turbo-migration:load', function () {
610

711
if ($.isController('exercises') && ProgrammingGroups.is_other_user(current_contributor)) {
812

9-
App.synchronized_editor = App.cable.subscriptions.create({
13+
consumer.subscriptions.create({
1014
channel: "SynchronizedEditorChannel", exercise_id: exercise_id
1115
}, {
1216
connected() {
@@ -70,7 +74,7 @@ $(document).on('turbo-migration:load', function () {
7074
},
7175

7276
is_connected() {
73-
return App.cable.subscriptions.findAll(App.synchronized_editor.identifier).length > 0
77+
return consumer.subscriptions.findAll(App.synchronized_editor.identifier).length > 0
7478
},
7579

7680
disconnect() {

0 commit comments

Comments
 (0)