Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Timestamp suffixes to images #18

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
source 'http://rubygems.org'

gem 'rails', '2.3.8'
gem 'rack'
gem 'mocha'
32 changes: 32 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
GEM
remote: http://rubygems.org/
specs:
actionmailer (2.3.8)
actionpack (= 2.3.8)
actionpack (2.3.8)
activesupport (= 2.3.8)
rack (~> 1.1.0)
activerecord (2.3.8)
activesupport (= 2.3.8)
activeresource (2.3.8)
activesupport (= 2.3.8)
activesupport (2.3.8)
mocha (0.9.8)
rake
rack (1.1.0)
rails (2.3.8)
actionmailer (= 2.3.8)
actionpack (= 2.3.8)
activerecord (= 2.3.8)
activeresource (= 2.3.8)
activesupport (= 2.3.8)
rake (>= 0.8.3)
rake (0.8.7)

PLATFORMS
ruby

DEPENDENCIES
mocha
rack
rails (= 2.3.8)
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ Rake::RDocTask.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'AssetPackager'
rdoc.options << '--line-numbers' << '--inline-source'
rdoc.rdoc_files.include('README')
rdoc.rdoc_files.include('Readme.rdoc')
rdoc.rdoc_files.include('lib/**/*.rb')
end
File renamed without changes.
21 changes: 19 additions & 2 deletions lib/synthesis/asset_package.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,14 @@ def compressed_file
end

def compress_js(source)
jsmin_path = "#{Rails.root}/vendor/plugins/asset_packager/lib"
jsmin_path = File.join(File.dirname(__FILE__), 'jsmin.rb')
tmp_path = "#{Rails.root}/tmp/#{@target}_packaged"

# write out to a temp file
File.open("#{tmp_path}_uncompressed.js", "w") {|f| f.write(source) }

# compress file with JSMin library
`ruby #{jsmin_path}/jsmin.rb <#{tmp_path}_uncompressed.js >#{tmp_path}_compressed.js \n`
`ruby #{jsmin_path} <#{tmp_path}_uncompressed.js >#{tmp_path}_compressed.js \n`

# read it back in and trim it
result = ""
Expand All @@ -180,6 +180,23 @@ def compress_css(source)
source.gsub!(/\n$/, "") # remove last break
source.gsub!(/ \{ /, " {") # trim inside brackets
source.gsub!(/; \}/, "}") # trim inside brackets

# add timestamps to images in css
source.gsub!(/url\(['"]?([^'"\)]+?(?:gif|png|jpe?g))['"]?\)/i) do |match|

file = $1
path = File.join(Rails.root, 'public')

if file.starts_with?('/')
path = File.join(path, file)
else
path = File.join(path, 'stylesheets', file)
end


match.gsub(file, "#{file}?#{File.new(path).mtime.to_i}")
end

source
end

Expand Down
4 changes: 2 additions & 2 deletions lib/synthesis/asset_package_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def javascript_include_merged(*sources)
AssetPackage.targets_from_sources("javascripts", sources) :
AssetPackage.sources_from_targets("javascripts", sources))

sources.collect {|source| javascript_include_tag(source, options) }.join("\n")
sources.collect {|source| javascript_include_tag(source, options) }.join("\n").html_safe
end

def stylesheet_link_merged(*sources)
Expand All @@ -32,7 +32,7 @@ def stylesheet_link_merged(*sources)
AssetPackage.targets_from_sources("stylesheets", sources) :
AssetPackage.sources_from_targets("stylesheets", sources))

sources.collect { |source| stylesheet_link_tag(source, options) }.join("\n")
sources.collect { |source| stylesheet_link_tag(source, options) }.join("\n").html_safe
end

end
Expand Down
File renamed without changes.
15 changes: 4 additions & 11 deletions test/asset_package_helper_development_test.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
$:.unshift(File.dirname(__FILE__) + '/../lib')

ENV['RAILS_ENV'] = "development"
require File.dirname(__FILE__) + '/../../../../config/environment'
require 'test/unit'
require 'rubygems'
require 'mocha'

require 'action_controller/test_process'
require 'test/test_helper'

Rails.env = 'development'
ActionController::Base.logger = nil
ActionController::Routing::Routes.reload rescue nil

Expand All @@ -19,8 +12,8 @@ class AssetPackageHelperDevelopmentTest < Test::Unit::TestCase
include Synthesis::AssetPackageHelper

def setup
Synthesis::AssetPackage.asset_base_path = "#{Rails.root}/vendor/plugins/asset_packager/test/assets"
Synthesis::AssetPackage.asset_packages_yml = YAML.load_file("#{Rails.root}/vendor/plugins/asset_packager/test/asset_packages.yml")
Synthesis::AssetPackage.asset_base_path = "test/assets"
Synthesis::AssetPackage.asset_packages_yml = YAML.load_file("test/asset_packages.yml")

Synthesis::AssetPackage.any_instance.stubs(:log)

Expand Down
17 changes: 4 additions & 13 deletions test/asset_package_helper_production_test.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
$:.unshift(File.dirname(__FILE__) + '/../lib')

require File.dirname(__FILE__) + '/../../../../config/environment'
require 'test/unit'
require 'rubygems'
require 'mocha'

require 'action_controller/test_process'

ActionController::Base.logger = nil
ActionController::Routing::Routes.reload rescue nil
require 'test/test_helper'
Rails.env = 'development'

class AssetPackageHelperProductionTest < Test::Unit::TestCase
include ActionController::Assertions::DomAssertions
Expand All @@ -20,8 +11,8 @@ class AssetPackageHelperProductionTest < Test::Unit::TestCase
cattr_accessor :packages_built

def setup
Synthesis::AssetPackage.asset_base_path = "#{Rails.root}/vendor/plugins/asset_packager/test/assets"
Synthesis::AssetPackage.asset_packages_yml = YAML.load_file("#{Rails.root}/vendor/plugins/asset_packager/test/asset_packages.yml")
Synthesis::AssetPackage.asset_base_path = "test/assets"
Synthesis::AssetPackage.asset_packages_yml = YAML.load_file("test/asset_packages.yml")

Synthesis::AssetPackage.any_instance.stubs(:log)
self.stubs(:should_merge?).returns(true)
Expand Down
8 changes: 3 additions & 5 deletions test/asset_packager_test.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
require File.dirname(__FILE__) + '/../../../../config/environment'
require 'test/unit'
require 'mocha'
require 'test/test_helper'

class AssetPackagerTest < Test::Unit::TestCase
include Synthesis

def setup
Synthesis::AssetPackage.asset_base_path = "#{Rails.root}/vendor/plugins/asset_packager/test/assets"
Synthesis::AssetPackage.asset_packages_yml = YAML.load_file("#{Rails.root}/vendor/plugins/asset_packager/test/asset_packages.yml")
Synthesis::AssetPackage.asset_base_path = "test/assets"
Synthesis::AssetPackage.asset_packages_yml = YAML.load_file("test/asset_packages.yml")

Synthesis::AssetPackage.any_instance.stubs(:log)
Synthesis::AssetPackage.build_all
Expand Down
37 changes: 37 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require 'rubygems'

`rm -rf test/fake_root`
`mkdir -p test/fake_root/tmp`

module Rails
def self.root
File.expand_path("test/fake_root")
end

def self.backtrace_cleaner
ActiveSupport::BacktraceCleaner.new
end

def self.env=(x)
@env = x
end

def self.env
@env
end
end

require 'rack'
require 'action_view'
require 'action_controller'
require 'rails/backtrace_cleaner'

$LOAD_PATH << 'lib'
require 'init'

require 'test/unit'
require 'action_controller/test_process'
require 'mocha'

ActionController::Base.logger = nil
ActionController::Routing::Routes.reload rescue nil