2015-04-06 14:17:02 -04:00
|
|
|
module ActionCable
|
|
|
|
module Connection
|
2015-06-21 15:08:20 -04:00
|
|
|
module Identification
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
|
|
|
class_attribute :identifiers
|
|
|
|
self.identifiers = Set.new
|
|
|
|
end
|
|
|
|
|
|
|
|
class_methods do
|
2015-07-07 16:33:58 -04:00
|
|
|
# Mark a key as being a connection identifier index that can then used to find the specific connection again later.
|
|
|
|
# Common identifiers are current_user and current_account, but could be anything really.
|
2015-07-11 05:25:11 -04:00
|
|
|
#
|
|
|
|
# Note that anything marked as an identifier will automatically create a delegate by the same name on any
|
|
|
|
# channel instances created off the connection.
|
2015-06-21 15:08:20 -04:00
|
|
|
def identified_by(*identifiers)
|
2015-07-07 16:28:02 -04:00
|
|
|
Array(identifiers).each { |identifier| attr_accessor identifier }
|
2015-07-30 14:21:12 -04:00
|
|
|
self.identifiers += identifiers
|
2015-06-21 15:08:20 -04:00
|
|
|
end
|
2015-04-06 14:17:02 -04:00
|
|
|
end
|
|
|
|
|
2015-07-07 16:33:58 -04:00
|
|
|
# Return a single connection identifier that combines the value of all the registered identifiers into a single gid.
|
2015-04-06 14:17:02 -04:00
|
|
|
def connection_identifier
|
2015-06-21 13:39:43 -04:00
|
|
|
@connection_identifier ||= connection_gid identifiers.map { |id| instance_variable_get("@#{id}") }.compact
|
2015-04-06 14:17:02 -04:00
|
|
|
end
|
|
|
|
|
2015-07-07 16:33:58 -04:00
|
|
|
private
|
|
|
|
def connection_gid(ids)
|
2015-08-21 07:41:27 -04:00
|
|
|
ids.map { |o| (o.try(:to_global_id) || o).to_s }.sort.join(":")
|
2015-07-07 16:33:58 -04:00
|
|
|
end
|
2015-04-06 14:17:02 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|