Sha256: 7495e62d09d4aa3270d2d40841fee013f5c4647bafae97cee767c5dd4e962dff

Contents?: true

Size: 1.6 KB

Versions: 1

Compression:

Stored size: 1.6 KB

Contents

require 'sprockets'

module Trackman
  module Assets
    module Components  
      module Rails32PathResolver
        include PathResolver
        
        def translate url, parent_url 
          root = working_dir.realpath
          
          path = url
          path.slice! /^\/assets/
          path = Pathname.new path
          
          if path.relative?          
            folder = (root + Pathname.new(parent_url)).parent.realpath
            path = (folder + path).to_s
            path.slice! sprockets.paths.select{|p| path.include? p }.first 
          end
          
          path = sprockets.resolve path
          path.relative_path_from(root).to_s
        end

        def sprockets 
          @@sprockets ||= init_env
        end

        def init_env
          env = ::Rails.application.class.assets

          # if defined?(::Rails) && ::Rails.application
          #   env = ::Rails.application.config.assets
          # else
          #   env = ::Sprockets::Environment.new
          #   paths = ['app', 'lib', 'vendor'].inject([]) do |array, f|
          #     array + ["images", "stylesheets", "javascripts"].map{|p| "#{working_dir}/#{f}/assets/#{p}" }
          #   end
          #   paths << "#{working_dir}/public"
          #   paths.each{|p| env.append_path p }
          # end

          env
        end
        def subfolder(file)
          if file.include?('.js')
            subfolder = "javascripts"
          elsif file.include?('.css')
            subfolder = "stylesheets"
          else 
            subfolder = "images"
          end
          subfolder
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
trackman-0.2.64 lib/trackman/assets/components/rails32_path_resolver.rb