Skip to content

Add callback method in controller when auto include filter decides not to insert javascript #344

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,14 @@ By default Intercom will be automatically inserted in development and production
config.enabled_environments = ["production"]
```

### Controller callback

When using automatic insertion of Intercom Javascript, you can have a callback method in your controller should the criteria to automatically insert javascript is unmet. The callback is `intercom_javascript_excluded` by default, but you can configure to a more appropriate name for all controllers in your application.

```ruby
config.exclude_javascript_callback = :intercom_javascript_excluded
```

### Manually Inserting the Intercom Javascript

Some situations may require manually inserting the Intercom script tag. If you simply wish to place the Intercom javascript in a different place within the page or, on a page without a closing `</body>` tag:
Expand Down
18 changes: 16 additions & 2 deletions lib/intercom-rails/auto_include_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@ class Filter

def self.filter(controller)
return if BLOCKED_CONTROLLER_NAMES.include?(controller.class.name)

auto_include_filter = new(controller)
return unless auto_include_filter.include_javascript?

auto_include_filter.include_javascript!
if auto_include_filter.include_javascript?
auto_include_filter.include_javascript!
else
auto_include_filter.exclude_javascript
end

# User defined method to whitelist the script sha-256 when using CSP
if defined?(CoreExtensions::IntercomRails::AutoInclude.csp_sha256_hook) == 'method'
Expand All @@ -44,6 +48,11 @@ def include_javascript?
intercom_script_tag.valid?
end

def exclude_javascript
callback = exclude_javascript_callback
controller.send(callback) if controller.respond_to?(callback)
end

def csp_sha256
intercom_script_tag.csp_sha256
end
Expand Down Expand Up @@ -89,6 +98,11 @@ def show_everywhere?
IntercomRails.config.include_for_logged_out_users
end

def exclude_javascript_callback
IntercomRails.config.exclude_javascript_callback ||
:intercom_javascript_excluded
end

def enabled_for_environment?
enabled_environments = IntercomRails.config.enabled_environments
return true if enabled_environments.nil?
Expand Down
1 change: 1 addition & 0 deletions lib/intercom-rails/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ def self.reset!
config_accessor :hide_default_launcher
config_accessor :api_base
config_accessor :encrypted_mode
config_accessor :exclude_javascript_callback

def self.api_key=(*)
warn "Setting an Intercom API key is no longer supported; remove the `config.api_key = ...` line from config/initializers/intercom.rb"
Expand Down
7 changes: 7 additions & 0 deletions spec/auto_include_filter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -255,4 +255,11 @@ def current_user
expect(response.body).to include('nonce="aaaa"')
end
end

context 'when intercom script is not injected' do
it 'invokes callback on controller' do
expect(controller).to receive(:intercom_javascript_excluded)
get :without_user
end
end
end
5 changes: 5 additions & 0 deletions spec/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@
expect(IntercomRails.config.encrypted_mode).to eq(true)
end

it 'gets/sets controller callback' do
IntercomRails.config.exclude_javascript_callback = :callback
expect(IntercomRails.config.exclude_javascript_callback).to eq(:callback)
end

it 'raises error if current user not a proc' do
expect { IntercomRails.config.user.current = 1 }.to raise_error(ArgumentError)
end
Expand Down