Module: Padrino::Contrib::Helpers::AssetsCompressor::Helpers
- Defined in:
- lib/padrino-contrib/helpers/assets_compressor.rb
Class Method Summary collapse
Instance Method Summary collapse
- #asset_path_with_compression(kind, source) ⇒ Object
- #assets_compressor(kind, sources) ⇒ Object
- #cache_asset(file, options = {}, &block) ⇒ Object
- #javascript_include_tag_with_compression(*sources) ⇒ Object
- #stylesheet_link_tag_with_compression(*sources) ⇒ Object
Class Method Details
.included(base) ⇒ Object
34 35 36 37 38 |
# File 'lib/padrino-contrib/helpers/assets_compressor.rb', line 34 def self.included(base) base.alias_method_chain :javascript_include_tag, :compression base.alias_method_chain :stylesheet_link_tag, :compression base.alias_method_chain :asset_path, :compression end |
Instance Method Details
#asset_path_with_compression(kind, source) ⇒ Object
133 134 135 136 |
# File 'lib/padrino-contrib/helpers/assets_compressor.rb', line 133 def asset_path_with_compression(kind, source) file = asset_path_without_compression(kind, source) find_last_modified(file) end |
#assets_compressor(kind, sources) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/padrino-contrib/helpers/assets_compressor.rb', line 71 def assets_compressor(kind, sources) = sources..symbolize_keys bundle = .delete(:cache) return sources if bundle.nil? began_at = Time.now asset_folder = case kind when :css then 'stylesheets' when :js then 'javascripts' else raise "YUI Compressor didn't support yet #{kind} compression" end bundle = settings.app_name.downcase if bundle.is_a?(TrueClass) path = Padrino.root("public", uri_root_path(asset_folder, bundle.to_s)) # Detect changes stamps = sources.inject(0) do |memo, source| asset_path_without_compression(kind, source) =~ /\?(\d{10})$/ memo += $1.to_i memo end bundle = "#{bundle}.#{stamps}" if stamps > 0 path = Padrino.root("public", uri_root_path(asset_folder, bundle.to_s)) path += ".#{kind}" unless path =~ /\.#{kind}/ # Back if no changes happens return bundle if File.exist?(path) # Clean old cached files Dir[path.gsub(/\.\d{10}\.#{kind}/, "*")].each { |file| FileUtils.rm_f(file) } # Get source code errors = [] code = sources.map do |source| source = asset_path(kind, source).sub(/\?\d{10}$/, '') # Removes Timestamp begin source = source =~ /^http/ ? open(source) : File.read(Padrino.root("public", source)) rescue Exception => e logger.error e. errors << source next end # Removes extra comments if cs = source =~ /\/\*\!/ cr = source.slice(cs, source.length) ce = cr =~ /\*\// cr = source.slice(cs, ce+2) source.sub!(cr,'') end source.each_line.reject { |l| l.strip == "" }.join end # Write the new bundled file Dir.mkdir(File.dirname(path)) unless File.exist?(File.dirname(path)) File.open(path, "w") { |f| f.write(settings.compressor[kind].compress(code.join("\n"))) } logger.debug "Compressed (%0.2fms) %s" % [Time.now-began_at, path] if defined?(logger) # Return the updated bundle errors.unshift bundle end |
#cache_asset(file, options = {}, &block) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/padrino-contrib/helpers/assets_compressor.rb', line 48 def cache_asset(file, ={}, &block) began_at = Time.now kind = File.extname(file).sub(/\./, '').to_sym original = Dir[File.join(settings.views, file).sub(/#{kind}$/, "*")][0] mtime = File.mtime(original).to_i file = file.sub(/#{kind}$/, "#{mtime}.#{kind}") path = Padrino.root("public", uri_root_path(file)) if !File.exist?(path) source = block.call if [:compress] source = settings.compressor[kind].compress(source) end Dir.mkdir(File.dirname(path)) unless File.exist?(File.dirname(path)) File.open(path, "w") { |f| f.write(source) } logger.debug "Compressed (%0.2fms) %s" % [Time.now-began_at, path] if defined?(logger) end redirect file end |
#javascript_include_tag_with_compression(*sources) ⇒ Object
40 41 42 |
# File 'lib/padrino-contrib/helpers/assets_compressor.rb', line 40 def javascript_include_tag_with_compression(*sources) javascript_include_tag_without_compression(*assets_compressor(:js, sources)) end |
#stylesheet_link_tag_with_compression(*sources) ⇒ Object
44 45 46 |
# File 'lib/padrino-contrib/helpers/assets_compressor.rb', line 44 def stylesheet_link_tag_with_compression(*sources) stylesheet_link_tag_without_compression(*assets_compressor(:css, sources)) end |