2015-10-16 04:02:22 -04:00
|
|
|
require 'set'
|
|
|
|
|
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
|
2016-01-15 12:41:18 -05:00
|
|
|
# Mark a key as being a connection identifier index that can then be used to find the specific connection again later.
|
2016-02-13 21:05:04 -05:00
|
|
|
# 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-10-16 03:32:46 -04:00
|
|
|
unless defined? @connection_identifier
|
2015-10-09 10:11:26 -04:00
|
|
|
@connection_identifier = connection_gid identifiers.map { |id| instance_variable_get("@#{id}") }.compact
|
|
|
|
end
|
|
|
|
|
|
|
|
@connection_identifier
|
2015-04-06 14:17:02 -04:00
|
|
|
end
|
|
|
|
|
2015-07-07 16:33:58 -04:00
|
|
|
private
|
|
|
|
def connection_gid(ids)
|
2015-10-16 04:02:22 -04:00
|
|
|
ids.map do |o|
|
2015-11-18 06:15:18 -05:00
|
|
|
if o.respond_to? :to_gid_param
|
|
|
|
o.to_gid_param
|
2015-10-16 04:02:22 -04:00
|
|
|
else
|
|
|
|
o.to_s
|
|
|
|
end
|
|
|
|
end.sort.join(":")
|
2015-07-07 16:33:58 -04:00
|
|
|
end
|
2015-04-06 14:17:02 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|