Sha256: c977baf8555935a86e2f7607e96be5195773b6321a76804a41440f58b8dbc601

Contents?: true

Size: 1.22 KB

Versions: 9

Compression:

Stored size: 1.22 KB

Contents

module IntercomRails

  class AutoIncludeFilter

    CLOSING_BODY_TAG = %r{</body>}

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

      auto_include_filter.include_javascript!
    end

    attr_reader :controller

    def initialize(kontroller)
      @controller = kontroller 
    end

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

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

    private
    def response
      controller.response
    end

    def html_content_type?
      response.content_type == 'text/html'
    end

    def response_has_closing_body_tag?
      !!(response.body[CLOSING_BODY_TAG])
    end

    def intercom_script_tag_called_manually?
      controller.instance_variable_get(SCRIPT_TAG_HELPER_CALLED_INSTANCE_VARIABLE)
    end

    def intercom_script_tag
      @script_tag ||= ScriptTag.new(:find_current_user_details => true, :find_current_company_details => true, :controller => controller)
    end

  end

end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
intercom-rails-0.2.8 lib/intercom-rails/auto_include_filter.rb
intercom-rails-0.2.7 lib/intercom-rails/auto_include_filter.rb
intercom-rails-0.2.6 lib/intercom-rails/auto_include_filter.rb
intercom-rails-0.2.5 lib/intercom-rails/auto_include_filter.rb
intercom-rails-0.2.4 lib/intercom-rails/auto_include_filter.rb
intercom-rails-0.2.3 lib/intercom-rails/auto_include_filter.rb
intercom-rails-0.2.2 lib/intercom-rails/auto_include_filter.rb
intercom-rails-0.2.1 lib/intercom-rails/auto_include_filter.rb
intercom-rails-0.2.0 lib/intercom-rails/auto_include_filter.rb