Class: IntercomRails::AutoInclude::Filter

Inherits:
Object
  • Object
show all
Defined in:
lib/intercom-rails/auto_include_filter.rb

Constant Summary collapse

CLOSING_BODY_TAG =
%r{</body>}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(kontroller) ⇒ Filter

Returns a new instance of Filter.


23
24
25
# File 'lib/intercom-rails/auto_include_filter.rb', line 23

def initialize(kontroller)
  @controller = kontroller
end

Instance Attribute Details

#controllerObject (readonly)

Returns the value of attribute controller


21
22
23
# File 'lib/intercom-rails/auto_include_filter.rb', line 21

def controller
  @controller
end

Class Method Details

.filter(controller) ⇒ Object


14
15
16
17
18
19
# File 'lib/intercom-rails/auto_include_filter.rb', line 14

def self.filter(controller)
  auto_include_filter = new(controller)
  return unless auto_include_filter.include_javascript?

  auto_include_filter.include_javascript!
end

Instance Method Details

#include_javascript!Object


27
28
29
# File 'lib/intercom-rails/auto_include_filter.rb', line 27

def include_javascript!
  response.body = response.body.gsub(CLOSING_BODY_TAG, intercom_script_tag.output + '\\0')
end

#include_javascript?Boolean

Returns:

  • (Boolean)

31
32
33
34
35
36
37
# File 'lib/intercom-rails/auto_include_filter.rb', line 31

def include_javascript?
  enabled_for_environment? &&
  !intercom_script_tag_called_manually? &&
  html_content_type? &&
  response_has_closing_body_tag? &&
  intercom_script_tag.valid?
end