2015-10-16 08:02:22 +00:00
|
|
|
require 'set'
|
|
|
|
|
2015-04-06 18:17:02 +00:00
|
|
|
module ActionCable
|
|
|
|
module Connection
|
2015-06-21 19:08:20 +00:00
|
|
|
module Identification
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
|
|
|
class_attribute :identifiers
|
|
|
|
self.identifiers = Set.new
|
|
|
|
end
|
|
|
|
|
|
|
|
class_methods do
|
2016-01-15 17:41:18 +00:00
|
|
|
# Mark a key as being a connection identifier index that can then be used to find the specific connection again later.
|
2016-02-14 02:05:04 +00:00
|
|
|
# Common identifiers are current_user and current_account, but could be anything, really.
|
2015-07-11 09:25:11 +00: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 19:08:20 +00:00
|
|
|
def identified_by(*identifiers)
|
2015-07-07 20:28:02 +00:00
|
|
|
Array(identifiers).each { |identifier| attr_accessor identifier }
|
2015-07-30 18:21:12 +00:00
|
|
|
self.identifiers += identifiers
|
2015-06-21 19:08:20 +00:00
|
|
|
end
|
2015-04-06 18:17:02 +00:00
|
|
|
end
|
|
|
|
|
2015-07-07 20:33:58 +00:00
|
|
|
# Return a single connection identifier that combines the value of all the registered identifiers into a single gid.
|
2015-04-06 18:17:02 +00:00
|
|
|
def connection_identifier
|
2015-10-16 07:32:46 +00:00
|
|
|
unless defined? @connection_identifier
|
2015-10-09 14:11:26 +00:00
|
|
|
@connection_identifier = connection_gid identifiers.map { |id| instance_variable_get("@#{id}") }.compact
|
|
|
|
end
|
|
|
|
|
|
|
|
@connection_identifier
|
2015-04-06 18:17:02 +00:00
|
|
|
end
|
|
|
|
|
2015-07-07 20:33:58 +00:00
|
|
|
private
|
|
|
|
def connection_gid(ids)
|
2015-10-16 08:02:22 +00:00
|
|
|
ids.map do |o|
|
2015-11-18 11:15:18 +00:00
|
|
|
if o.respond_to? :to_gid_param
|
|
|
|
o.to_gid_param
|
2015-10-16 08:02:22 +00:00
|
|
|
else
|
|
|
|
o.to_s
|
|
|
|
end
|
|
|
|
end.sort.join(":")
|
2015-07-07 20:33:58 +00:00
|
|
|
end
|
2015-04-06 18:17:02 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|