Sha256: 1e5085d37cbfd6d2c6b1bfeb0e7bce22c88b75069942c40c358fb6dc1f5e24a9

Contents?: true

Size: 1.53 KB

Versions: 1

Compression:

Stored size: 1.53 KB

Contents

require 'angular-rails-templates/compact_javascript_escape'

module AngularRails4Templates
  class Template < ::Tilt::Template
    include CompactJavaScriptEscape
    AngularJsTemplateWrapper = Tilt::ERBTemplate.new "#{File.dirname __FILE__}/javascript_template.js.erb"
    @@compressor = nil

    def self.default_mime_type
      'application/javascript'
    end

    def prepare
      # we only want to process html assets inside those specified in configuration.inside_paths
      @asset_should_be_processed = configuration.inside_paths.any? { |folder| file.match(folder.to_s) }

      if configuration.htmlcompressor and @asset_should_be_processed
        @data = compress data
      end
    end

    def evaluate(scope, locals, &block)
      locals[:html] = escape_javascript data.chomp
      locals[:angular_template_name] = logical_template_path(scope)
      locals[:source_file] = "#{scope.pathname}".sub(/^#{Rails.root}\//,'')
      locals[:angular_module] = configuration.module_name

      if @asset_should_be_processed
        AngularJsTemplateWrapper.render(scope, locals)
      else
        data
      end
    end

    private

    def logical_template_path(scope)
      path = scope.logical_path.sub /^(#{configuration.ignore_prefix.join('|')})/, ''
      "#{path}.html"
    end

    def configuration
      ::Rails.configuration.angular_templates
    end

    def compress html
      unless @@compressor
        @@compressor = HtmlCompressor::Compressor.new configuration.htmlcompressor
      end
      @@compressor.compress(html)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
angular-rails4-templates-0.3.0 lib/angular-rails4-templates/template.rb