Skip to content

Instantly share code, notes, and snippets.

@khoan
Last active August 3, 2023 14:35
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 khoan/10db3971c19c50ffb4889c8b1d5d9b98 to your computer and use it in GitHub Desktop.
Save khoan/10db3971c19c50ffb4889c8b1d5d9b98 to your computer and use it in GitHub Desktop.
# When intercom javascript is excluded from HTML, any attempt to invoke `window.Intercom(...)` would
# result in runtime error. This patch introduces a callback into the controller, so that a shim can
# be inserted, when intercom javascript is excluded.
#
# Remove this patch and update your code if alternative at https://github.com/intercom/intercom-rails/pull/344
# is accepted.
spec = Bundler.definition.specs.find{|s| s.name == 'intercom-rails'}
if Gem::Dependency.new(spec.name, '> 0.4.2').match?(spec.name, spec.version)
raise Gem::DependencyError, "Check that patch is still applicable for intercom-rails gem version #{spec.version}"
else
module IntercomRails
module AutoInclude
module MethodPatch
def intercom_rails_auto_include
super
# Callback to controller#after_intercom_rails_auto_include. Within callback,
# instance variable @intercom_rails_include_javascript can be queried to know
# if intercom javascript has been included or not.
callback = :after_intercom_rails_auto_include
send(callback) if respond_to?(callback, true)
end
end
module Method
prepend MethodPatch
end
module FilterPatch
def include_javascript?
controller.instance_variable_set(:@intercom_rails_include_javascript, super)
end
end
class Filter
prepend FilterPatch
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment