Sha256: 84cb5ce1b5ad79c3f219006ce218e604e7a81f02cddb562cf6a4b3a9ffee2144

Contents?: true

Size: 1.41 KB

Versions: 49

Compression:

Stored size: 1.41 KB

Contents

module Editmode

  module AutoInclude

    module Method
      def editmode_auto_include
        Editmode::AutoInclude::Filter.filter(self)
      end
    end

    class Filter

      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, editmode_script_tag.output + '\\0')
      end

      def include_javascript?
        enabled_for_environment? &&
        html_content_type? &&
        response_has_closing_body_tag? &&
        editmode_script_tag.valid?
      end

      private
      def response
        controller.response
      end

      def html_content_type?
        response.content_type.try(:include?,'text/html')
      end

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

      def editmode_script_tag
        @script_tag ||= Editmode::ScriptTag.new()
      end

      def enabled_for_environment?
        enabled_environments = ["production","development","staging"]
        return true if enabled_environments.nil?
        enabled_environments.map(&:to_s).include?(Rails.env)
      end

    end

  end

end

Version data entries

49 entries across 49 versions & 1 rubygems

Version Path
editmode-1.8.1 lib/editmode/auto_include_filter.rb
editmode-1.8.0 lib/editmode/auto_include_filter.rb
editmode-1.7.0.pre.4 lib/editmode/auto_include_filter.rb
editmode-1.7.0.pre.3 lib/editmode/auto_include_filter.rb
editmode-1.7.0.pre.2 lib/editmode/auto_include_filter.rb
editmode-1.7.0.pre.1 lib/editmode/auto_include_filter.rb
editmode-1.6.2 lib/editmode/auto_include_filter.rb
editmode-1.6.1 lib/editmode/auto_include_filter.rb
editmode-1.6.0.pre.1 lib/editmode/auto_include_filter.rb
editmode-1.5.0 lib/editmode/auto_include_filter.rb
editmode-1.4.0 lib/editmode/auto_include_filter.rb
editmode-1.4.0.pre.1 lib/editmode/auto_include_filter.rb
editmode-1.3.7 lib/editmode/auto_include_filter.rb
editmode-1.3.6 lib/editmode/auto_include_filter.rb
editmode-1.3.5 lib/editmode/auto_include_filter.rb
editmode-1.3.4 lib/editmode/auto_include_filter.rb
editmode-1.3.3 lib/editmode/auto_include_filter.rb
editmode-1.3.2 lib/editmode/auto_include_filter.rb
editmode-1.3.1 lib/editmode/auto_include_filter.rb
editmode-1.3.0 lib/editmode/auto_include_filter.rb