Skip to content

Instantly share code, notes, and snippets.

@willcosgrove
Created May 14, 2015 21:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willcosgrove/e9d5c292554cd3a169b0 to your computer and use it in GitHub Desktop.
Save willcosgrove/e9d5c292554cd3a169b0 to your computer and use it in GitHub Desktop.
The ClientID concern is for facilitating a passthrough of client IDs, useful for reconciling created records with their non-ID'd client counterpart.
module ClientID
extend ActiveSupport::Concern
included do
attr_writer :cid
after_commit :_save_cid, on: :create, if: -> { @cid.present? }
end
def cid
Thread.current[_cid_key]
end
private
def _save_cid
Thread.current[_cid_key] = @cid
end
def _cid_key
"cid:#{self.class.to_s.tableize}:#{id}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment