Add live dashboard for teachers in the context of an exercise
This commit also adds the fundamentals for ActionCable
This commit is contained in:
4
app/channels/application_cable/channel.rb
Normal file
4
app/channels/application_cable/channel.rb
Normal file
@ -0,0 +1,4 @@
|
||||
module ApplicationCable
|
||||
class Channel < ActionCable::Channel::Base
|
||||
end
|
||||
end
|
30
app/channels/application_cable/connection.rb
Normal file
30
app/channels/application_cable/connection.rb
Normal file
@ -0,0 +1,30 @@
|
||||
module ApplicationCable
|
||||
class Connection < ActionCable::Connection::Base
|
||||
identified_by :current_user
|
||||
|
||||
def connect
|
||||
self.current_user = find_verified_user
|
||||
end
|
||||
|
||||
def disconnect
|
||||
# Any cleanup work needed when the cable connection is cut.
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def session
|
||||
# `session` is not available here, so that we need to use `cookies.encrypted` instead
|
||||
cookies.encrypted[Rails.application.config.session_options[:key]].symbolize_keys
|
||||
end
|
||||
|
||||
def find_verified_user
|
||||
# Finding the current_user is similar to the code used in application_controller.rb#current_user
|
||||
current_user = ExternalUser.find_by(id: session[:external_user_id]) || InternalUser.find_by(id: session[:user_id])
|
||||
if current_user
|
||||
current_user
|
||||
else
|
||||
reject_unauthorized_connection
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
16
app/channels/la_exercises_channel.rb
Normal file
16
app/channels/la_exercises_channel.rb
Normal file
@ -0,0 +1,16 @@
|
||||
class LaExercisesChannel < ApplicationCable::Channel
|
||||
|
||||
def subscribed
|
||||
stream_from specific_channel
|
||||
end
|
||||
|
||||
def unsubscribed
|
||||
stop_all_streams
|
||||
end
|
||||
|
||||
private
|
||||
def specific_channel
|
||||
reject unless StudyGroupPolicy.new(current_user, StudyGroup.find_by(id: params[:study_group_id])).stream_la?
|
||||
"la_exercises_#{params[:exercise_id]}_channel_study_group_#{params[:study_group_id]}"
|
||||
end
|
||||
end
|
Reference in New Issue
Block a user