merb-assets 1.0.15 → 1.1.0.pre
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +55 -64
- data/lib/merb-assets/assets.rb +11 -6
- data/lib/merb-assets/assets_mixin.rb +166 -46
- data/lib/merb-assets/version.rb +5 -0
- data/lib/merb-assets.rb +13 -1
- data/spec/merb-assets_spec.rb +196 -7
- data/spec/spec.opts +2 -0
- data/spec/spec_helper.rb +11 -6
- metadata +25 -13
data/Rakefile
CHANGED
@@ -1,73 +1,64 @@
|
|
1
|
-
require
|
2
|
-
|
3
|
-
##############################################################################
|
4
|
-
# Package && release
|
5
|
-
##############################################################################
|
6
|
-
RUBY_FORGE_PROJECT = "merb"
|
7
|
-
PROJECT_URL = "http://merbivore.com"
|
8
|
-
PROJECT_SUMMARY = "Merb plugin that provides the helpers for assets and asset bundling"
|
9
|
-
PROJECT_DESCRIPTION = PROJECT_SUMMARY
|
10
|
-
|
11
|
-
GEM_AUTHOR = "Ezra Zygmuntowicz"
|
12
|
-
GEM_EMAIL = "ez@engineyard.com"
|
13
|
-
|
14
|
-
GEM_NAME = "merb-assets"
|
15
|
-
PKG_BUILD = ENV['PKG_BUILD'] ? '.' + ENV['PKG_BUILD'] : ''
|
16
|
-
GEM_VERSION = Merb::VERSION + PKG_BUILD
|
17
|
-
|
18
|
-
RELEASE_NAME = "REL #{GEM_VERSION}"
|
19
|
-
|
20
|
-
require "extlib/tasks/release"
|
21
|
-
|
22
|
-
spec = Gem::Specification.new do |s|
|
23
|
-
s.rubyforge_project = RUBY_FORGE_PROJECT
|
24
|
-
s.name = GEM_NAME
|
25
|
-
s.version = GEM_VERSION
|
26
|
-
s.platform = Gem::Platform::RUBY
|
27
|
-
s.has_rdoc = true
|
28
|
-
s.extra_rdoc_files = ["README", "LICENSE", 'TODO']
|
29
|
-
s.summary = PROJECT_SUMMARY
|
30
|
-
s.description = PROJECT_DESCRIPTION
|
31
|
-
s.author = GEM_AUTHOR
|
32
|
-
s.email = GEM_EMAIL
|
33
|
-
s.homepage = PROJECT_URL
|
34
|
-
s.add_dependency('merb-core', "~> #{Merb::VERSION}")
|
35
|
-
s.require_path = 'lib'
|
36
|
-
s.files = %w(LICENSE README Rakefile TODO) + Dir.glob("{lib,spec}/**/*")
|
37
|
-
end
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
38
3
|
|
39
|
-
|
40
|
-
|
41
|
-
end
|
4
|
+
# Assume a typical dev checkout to fetch the current merb-core version
|
5
|
+
require File.expand_path('../../merb-core/lib/merb-core/version', __FILE__)
|
42
6
|
|
43
|
-
|
44
|
-
|
45
|
-
Merb::RakeHelper.install(GEM_NAME, :version => GEM_VERSION)
|
46
|
-
end
|
7
|
+
# Load this library's version information
|
8
|
+
require File.expand_path('../lib/merb-assets/version', __FILE__)
|
47
9
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
10
|
+
begin
|
11
|
+
|
12
|
+
gem 'jeweler', '~> 1.4'
|
13
|
+
require 'jeweler'
|
14
|
+
|
15
|
+
Jeweler::Tasks.new do |gemspec|
|
16
|
+
|
17
|
+
gemspec.version = Merb::Assets::VERSION
|
18
|
+
|
19
|
+
gemspec.name = "merb-assets"
|
20
|
+
gemspec.description = "Merb plugin for supporting assets"
|
21
|
+
gemspec.summary = "Merb plugin that provides helpers for assets and asset bundling"
|
22
|
+
|
23
|
+
gemspec.authors = [ "Ezra Zygmuntowicz" ]
|
24
|
+
gemspec.email = "ez@engineyard.com"
|
25
|
+
gemspec.homepage = "http://merbivore.com/"
|
26
|
+
|
27
|
+
gemspec.files = %w(LICENSE Rakefile README TODO) + Dir['{lib,spec}/**/*']
|
28
|
+
|
29
|
+
# Runtime dependencies
|
30
|
+
gemspec.add_dependency 'merb-core', "~> #{Merb::VERSION}"
|
31
|
+
|
32
|
+
# Development dependencies
|
33
|
+
gemspec.add_development_dependency 'rspec', '>= 1.2.9'
|
52
34
|
|
53
|
-
desc "Create a gemspec file"
|
54
|
-
task :gemspec do
|
55
|
-
File.open("#{GEM_NAME}.gemspec", "w") do |file|
|
56
|
-
file.puts spec.to_ruby
|
57
35
|
end
|
36
|
+
|
37
|
+
Jeweler::GemcutterTasks.new
|
38
|
+
|
39
|
+
rescue LoadError
|
40
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
58
41
|
end
|
59
42
|
|
60
|
-
|
61
|
-
Spec::Rake::SpecTask.new(
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
ENV["TASK"].split(',').map { |task| "spec/**/#{task}_spec.rb" }
|
66
|
-
else
|
67
|
-
FileList['spec/**/*_spec.rb']
|
68
|
-
end
|
69
|
-
end
|
43
|
+
require 'spec/rake/spectask'
|
44
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
45
|
+
spec.spec_opts << '--options' << 'spec/spec.opts' if File.exists?('spec/spec.opts')
|
46
|
+
spec.libs << 'lib' << 'spec'
|
47
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
70
48
|
end
|
71
49
|
|
72
|
-
|
73
|
-
|
50
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
51
|
+
spec.libs << 'lib' << 'spec'
|
52
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
53
|
+
spec.rcov = true
|
54
|
+
end
|
55
|
+
|
56
|
+
task :default => :spec
|
57
|
+
|
58
|
+
require 'rake/rdoctask'
|
59
|
+
Rake::RDocTask.new do |rdoc|
|
60
|
+
rdoc.rdoc_dir = 'rdoc'
|
61
|
+
rdoc.title = "test_gem #{Merb::Assets::VERSION}"
|
62
|
+
rdoc.rdoc_files.include('README*')
|
63
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
64
|
+
end
|
data/lib/merb-assets/assets.rb
CHANGED
@@ -41,12 +41,16 @@ module Merb
|
|
41
41
|
# # => "public/javascripts/dingo.js"
|
42
42
|
def asset_path(asset_type, filename, local_path = false)
|
43
43
|
filename = filename.to_s
|
44
|
+
return filename if filename =~ %r{^https?://} #leave absolte paths alone
|
45
|
+
|
46
|
+
# add extension if none given
|
44
47
|
if filename !~ /#{'\\' + ASSET_FILE_EXTENSIONS[asset_type]}\Z/ && filename.index('?').nil?
|
45
48
|
filename = "#{filename}#{ASSET_FILE_EXTENSIONS[asset_type]}" # don't modify receiver
|
46
49
|
end
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
+
|
51
|
+
# prepend asset type's folder path
|
52
|
+
filename = "/#{asset_type}s/#{filename}" unless filename.index("/") == 0
|
53
|
+
|
50
54
|
if local_path
|
51
55
|
return "public#{filename}"
|
52
56
|
else
|
@@ -56,7 +60,7 @@ module Merb
|
|
56
60
|
end
|
57
61
|
|
58
62
|
# Helper for creating unique paths to a file name
|
59
|
-
# Can increase
|
63
|
+
# Can increase speed for browsers that are limited to a certain number of connections per host
|
60
64
|
# for downloading static files (css, js, images...)
|
61
65
|
class UniqueAssetPath
|
62
66
|
class << self
|
@@ -76,7 +80,8 @@ module Merb
|
|
76
80
|
config = Merb::Plugins.config[:asset_helpers]
|
77
81
|
#%{#{(USE_SSL ? 'https' : 'http')}://#{sprintf(config[:asset_domain],self.calculate_host_id(file))}.#{config[:domain]}/#{filename}}
|
78
82
|
path = config[:use_ssl] ? 'https://' : 'http://'
|
79
|
-
path << sprintf(config[:asset_domain],self.calculate_host_id(filename)) << "
|
83
|
+
path << sprintf(config[:asset_domain],self.calculate_host_id(filename)) << "." if config[:asset_domain]
|
84
|
+
path << config[:domain]
|
80
85
|
path << "/" if filename.index('/') != 0
|
81
86
|
path << filename
|
82
87
|
end
|
@@ -239,4 +244,4 @@ module Merb
|
|
239
244
|
end
|
240
245
|
|
241
246
|
end
|
242
|
-
end
|
247
|
+
end
|
@@ -8,6 +8,25 @@ module Merb
|
|
8
8
|
# assets.
|
9
9
|
|
10
10
|
|
11
|
+
# This tests whether a random query string shall be appended to a url.
|
12
|
+
# Basically, you tell it your intention and if it's ok to use default
|
13
|
+
# config values, and it will either use your intention or the value
|
14
|
+
# set in Merb::Config[:reload_templates]
|
15
|
+
#
|
16
|
+
# ==== Parameters
|
17
|
+
# intention<Boolean>: true if a random string shall be appended
|
18
|
+
# allow_default<Boolean>: true if it's ok to use Merb::Config[:reload_templates]
|
19
|
+
#
|
20
|
+
# ==== Returns
|
21
|
+
# <Boolean> true if a random query string shall be appended
|
22
|
+
#
|
23
|
+
# ==== Examples
|
24
|
+
# Merb::AssetsMixin.append_random_query_string?(options[:reload])
|
25
|
+
# Merb::AssetsMixin.append_random_query_string?(options[:reload], !absolute)
|
26
|
+
def self.append_random_query_string?(intention, allow_default = true)
|
27
|
+
intention.nil? && allow_default ? Merb::Config[:reload_templates] : intention
|
28
|
+
end
|
29
|
+
|
11
30
|
# ==== Parameters
|
12
31
|
# none
|
13
32
|
#
|
@@ -24,29 +43,72 @@ module Merb
|
|
24
43
|
# namespace/controller/action.(css|js)
|
25
44
|
#
|
26
45
|
def auto_link
|
27
|
-
|
28
|
-
|
29
|
-
(controller_name / action_name).split("/").each do |path|
|
30
|
-
path = prefix + path
|
31
|
-
|
32
|
-
css_path = path + ".css"
|
33
|
-
if File.exists? Merb.root / "public" / "stylesheets" / css_path
|
34
|
-
html << %{<link rel="stylesheet" type="text/css" href="/stylesheets/#{css_path}" /> }
|
35
|
-
end
|
46
|
+
[auto_link_css, auto_link_js].join("\n")
|
47
|
+
end
|
36
48
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
49
|
+
# ==== Parameters
|
50
|
+
# none
|
51
|
+
#
|
52
|
+
# ==== Returns
|
53
|
+
# html<String>
|
54
|
+
#
|
55
|
+
# ==== Examples
|
56
|
+
# We want all possible matches in the file system upto the action name
|
57
|
+
# for CSS. The reason for separating auto_link for CSS and JS is
|
58
|
+
# performance concerns with page loading. See Yahoo performance rules
|
59
|
+
# (http://developer.yahoo.com/performance/rules.html)
|
60
|
+
def auto_link_css
|
61
|
+
auto_link_paths.map do |path|
|
62
|
+
asset_exists?(:stylesheet, path) ? css_include_tag(path) : nil
|
63
|
+
end.compact.join("\n")
|
64
|
+
end
|
41
65
|
|
42
|
-
|
43
|
-
|
44
|
-
|
66
|
+
# ==== Parameters
|
67
|
+
# none
|
68
|
+
#
|
69
|
+
# ==== Returns
|
70
|
+
# html<String>
|
71
|
+
#
|
72
|
+
# ==== Examples
|
73
|
+
# We want all possible matches in the file system upto the action name
|
74
|
+
# for JS. The reason for separating auto_link for CSS and JS is
|
75
|
+
# performance concerns with page loading. See Yahoo performance rules
|
76
|
+
# (http://developer.yahoo.com/performance/rules.html)
|
77
|
+
def auto_link_js
|
78
|
+
auto_link_paths.map do |path|
|
79
|
+
asset_exists?(:javascript, path) ? js_include_tag(path) : nil
|
80
|
+
end.compact.join("\n")
|
81
|
+
end
|
45
82
|
|
46
|
-
|
47
|
-
|
83
|
+
# ==== Parameters
|
84
|
+
# asset_type<Symbol>: A symbol representing the type of the asset.
|
85
|
+
# asset_path<String>: The path to the asset
|
86
|
+
#
|
87
|
+
# ==== Returns
|
88
|
+
# exists?<Boolean>
|
89
|
+
#
|
90
|
+
# ==== Examples
|
91
|
+
# This tests whether a give asset exists in the file system.
|
92
|
+
def asset_exists?(asset_type, asset_path)
|
93
|
+
File.exists?(Merb.root / asset_path(asset_type, asset_path, true))
|
48
94
|
end
|
49
95
|
|
96
|
+
# ==== Parameters
|
97
|
+
# none
|
98
|
+
#
|
99
|
+
# ==== Returns
|
100
|
+
# paths<Array>
|
101
|
+
#
|
102
|
+
# ==== Examples
|
103
|
+
# This is an auxiliary method which returns an array of all possible asset
|
104
|
+
# paths for the current controller/action.
|
105
|
+
def auto_link_paths
|
106
|
+
paths = (controller_name / action_name).split('/')
|
107
|
+
first = paths.shift
|
108
|
+
paths.inject( [first] ) do |memo, val|
|
109
|
+
memo.push [memo.last, val].join('/')
|
110
|
+
end
|
111
|
+
end
|
50
112
|
|
51
113
|
# ==== Parameters
|
52
114
|
# name<~to_s>:: The text of the link.
|
@@ -77,6 +139,9 @@ module Merb
|
|
77
139
|
# Sets the path prefix for the image. Defaults to "/images/" or whatever
|
78
140
|
# is specified in Merb::Config. This is ignored if img is an absolute
|
79
141
|
# path or full URL.
|
142
|
+
# :reload<Boolean>::
|
143
|
+
# Override the Merb::Config[:reload_templates] value. If true, a random query param will be appended
|
144
|
+
# to the image url
|
80
145
|
#
|
81
146
|
# All other options set HTML attributes on the tag.
|
82
147
|
#
|
@@ -98,23 +163,24 @@ module Merb
|
|
98
163
|
# image_tag('/dynamic/charts')
|
99
164
|
# # => <img src="/dynamic/charts">
|
100
165
|
def image_tag(img, opts={})
|
166
|
+
return "" if img.blank?
|
101
167
|
if img[0].chr == '/'
|
102
|
-
opts[:src] = img
|
168
|
+
opts[:src] = "#{Merb::Config[:path_prefix]}#{img}"
|
103
169
|
else
|
104
170
|
opts[:path] ||=
|
105
171
|
if img =~ %r{^https?://}
|
172
|
+
absolute = true
|
106
173
|
''
|
107
174
|
else
|
108
|
-
|
109
|
-
Merb::Config[:path_prefix] + '/images/'
|
110
|
-
else
|
111
|
-
'/images/'
|
112
|
-
end
|
175
|
+
"#{Merb::Config[:path_prefix]}/images/"
|
113
176
|
end
|
114
177
|
opts[:src] ||= opts.delete(:path) + img
|
115
178
|
end
|
116
|
-
|
117
|
-
|
179
|
+
|
180
|
+
if AssetsMixin.append_random_query_string?(opts.delete(:reload), !absolute)
|
181
|
+
opts[:src] += opts[:src].include?('?') ? "&#{random_query_string}" : "?#{random_query_string}"
|
182
|
+
end
|
183
|
+
|
118
184
|
%{<img #{ opts.to_xml_attributes } />}
|
119
185
|
end
|
120
186
|
|
@@ -405,33 +471,53 @@ module Merb
|
|
405
471
|
# will be added to them.
|
406
472
|
#
|
407
473
|
# ==== Options
|
474
|
+
# :charset<~to_s>::
|
475
|
+
# Charset which will be used as value for charset attribute
|
408
476
|
# :bundle<~to_s>::
|
409
477
|
# The name of the bundle the scripts should be combined into.
|
478
|
+
# :prefix<~to_s>::
|
479
|
+
# prefix to add to include tag, overrides any set in Merb::Plugins.config[:asset_helpers][:js_prefix]
|
480
|
+
# :suffix<~to_s>::
|
481
|
+
# suffix to add to include tag, overrides any set in Merb::Plugins.config[:asset_helpers][:js_suffix]
|
482
|
+
# :reload<Boolean>::
|
483
|
+
# Override the Merb::Config[:reload_templates] value. If true, a random query param will be appended
|
484
|
+
# to the js url
|
410
485
|
#
|
411
486
|
# ==== Returns
|
412
487
|
# String:: The JavaScript include tag(s).
|
413
488
|
#
|
414
489
|
# ==== Examples
|
415
490
|
# js_include_tag 'jquery'
|
416
|
-
# # => <script src="/javascripts/jquery.js" type="text/javascript"></script>
|
491
|
+
# # => <script src="/javascripts/jquery.js" type="text/javascript" charset="utf-8"></script>
|
492
|
+
#
|
493
|
+
# js_include_tag 'jquery', :charset => 'iso-8859-1'
|
494
|
+
# # => <script src="/javascripts/jquery.js" type="text/javascript" charset="iso-8859-1"></script>
|
417
495
|
#
|
418
496
|
# js_include_tag 'moofx.js', 'upload'
|
419
|
-
# # => <script src="/javascripts/moofx.js" type="text/javascript"></script>
|
420
|
-
# # <script src="/javascripts/upload.js" type="text/javascript"></script>
|
497
|
+
# # => <script src="/javascripts/moofx.js" type="text/javascript" charset="utf-8"></script>
|
498
|
+
# # <script src="/javascripts/upload.js" type="text/javascript" charset="utf-8"></script>
|
421
499
|
#
|
422
500
|
# js_include_tag :effects
|
423
|
-
# # => <script src="/javascripts/effects.js" type="text/javascript"></script>
|
501
|
+
# # => <script src="/javascripts/effects.js" type="text/javascript" charset="utf-8"></script>
|
424
502
|
#
|
425
503
|
# js_include_tag :jquery, :validation
|
426
|
-
# # => <script src="/javascripts/jquery.js" type="text/javascript"></script>
|
427
|
-
# # <script src="/javascripts/validation.js" type="text/javascript"></script>
|
504
|
+
# # => <script src="/javascripts/jquery.js" type="text/javascript" charset="utf-8"></script>
|
505
|
+
# # <script src="/javascripts/validation.js" type="text/javascript" charset="utf-8"></script>
|
428
506
|
#
|
507
|
+
# js_include_tag :application, :validation, :prefix => "http://cdn.example.com"
|
508
|
+
# # => <script src="http://cdn.example.com/javascripts/application.js" type="text/javascript" charset="utf-8"></script>
|
509
|
+
# # <script src="http://cdn.example.com/javascripts/validation.js" type="text/javascript" charset="utf-8"></script>
|
510
|
+
#
|
511
|
+
# js_include_tag :application, :validation, :suffix => ".#{MyApp.version}"
|
512
|
+
# # => <script src="/javascripts/application.1.0.3.js" type="text/javascript" charset="utf-8"></script>
|
513
|
+
# # <script src="/javascripts/validation.1.0.3.js" type="text/javascript" charset="utf-8"></script>
|
429
514
|
def js_include_tag(*scripts)
|
430
515
|
options = scripts.last.is_a?(Hash) ? scripts.pop : {}
|
431
516
|
return nil if scripts.empty?
|
432
|
-
|
433
|
-
reload = options[:reload] || Merb::Config[:reload_templates]
|
434
517
|
|
518
|
+
js_prefix = options[:prefix] || Merb::Plugins.config[:asset_helpers][:js_prefix]
|
519
|
+
js_suffix = options[:suffix] || Merb::Plugins.config[:asset_helpers][:js_suffix]
|
520
|
+
|
435
521
|
if (bundle_name = options[:bundle]) && Merb::Assets.bundle? && scripts.size > 1
|
436
522
|
bundler = Merb::Assets::JavascriptAssetBundler.new(bundle_name, *scripts)
|
437
523
|
bundled_asset = bundler.bundle!
|
@@ -441,11 +527,21 @@ module Merb
|
|
441
527
|
tags = ""
|
442
528
|
|
443
529
|
for script in scripts
|
444
|
-
src = asset_path(:javascript, script)
|
445
|
-
|
530
|
+
src = js_prefix.to_s + asset_path(:javascript, script)
|
531
|
+
|
532
|
+
if js_suffix
|
533
|
+
ext_length = ASSET_FILE_EXTENSIONS[:javascript].length + 1
|
534
|
+
src.insert(-ext_length,js_suffix)
|
535
|
+
end
|
536
|
+
|
537
|
+
if AssetsMixin.append_random_query_string?(options[:reload])
|
538
|
+
src += src.include?('?') ? "&#{random_query_string}" : "?#{random_query_string}"
|
539
|
+
end
|
540
|
+
|
446
541
|
attrs = {
|
447
542
|
:src => src,
|
448
|
-
:type => "text/javascript"
|
543
|
+
:type => "text/javascript",
|
544
|
+
:charset => options[:charset] || "utf-8"
|
449
545
|
}
|
450
546
|
tags << %Q{<script #{attrs.to_xml_attributes}></script>}
|
451
547
|
end
|
@@ -460,10 +556,19 @@ module Merb
|
|
460
556
|
# names, it will be added to them.
|
461
557
|
#
|
462
558
|
# ==== Options
|
559
|
+
# :charset<~to_s>::
|
560
|
+
# Charset which will be used as value for charset attribute
|
463
561
|
# :bundle<~to_s>::
|
464
562
|
# The name of the bundle the stylesheets should be combined into.
|
465
563
|
# :media<~to_s>::
|
466
564
|
# The media attribute for the generated link element. Defaults to :all.
|
565
|
+
# :prefix<~to_s>::
|
566
|
+
# prefix to add to include tag, overrides any set in Merb::Plugins.config[:asset_helpers][:css_prefix]
|
567
|
+
# :suffix<~to_s>::
|
568
|
+
# suffix to add to include tag, overrides any set in Merb::Plugins.config[:asset_helpers][:css_suffix]
|
569
|
+
# :reload<Boolean>::
|
570
|
+
# Override the Merb::Config[:reload_templates] value. If true, a random query param will be appended
|
571
|
+
# to the css url
|
467
572
|
#
|
468
573
|
# ==== Returns
|
469
574
|
# String:: The CSS include tag(s).
|
@@ -488,11 +593,18 @@ module Merb
|
|
488
593
|
#
|
489
594
|
# css_include_tag :style, :charset => 'iso-8859-1'
|
490
595
|
# # => <link href="/stylesheets/style.css" media="print" rel="Stylesheet" type="text/css" charset="iso-8859-1" />
|
596
|
+
#
|
597
|
+
# css_include_tag :style, :prefix => "http://static.example.com"
|
598
|
+
# # => <link href="http://static.example.com/stylesheets/style.css" media="print" rel="Stylesheet" type="text/css" />
|
599
|
+
#
|
600
|
+
# css_include_tag :style, :suffix => ".#{MyApp.version}"
|
601
|
+
# # => <link href="/stylesheets/style.1.0.0.css" media="print" rel="Stylesheet" type="text/css" />
|
491
602
|
def css_include_tag(*stylesheets)
|
492
603
|
options = stylesheets.last.is_a?(Hash) ? stylesheets.pop : {}
|
493
604
|
return nil if stylesheets.empty?
|
494
605
|
|
495
|
-
|
606
|
+
css_prefix = options[:prefix] || Merb::Plugins.config[:asset_helpers][:css_prefix]
|
607
|
+
css_suffix = options[:suffix] || Merb::Plugins.config[:asset_helpers][:css_suffix]
|
496
608
|
|
497
609
|
if (bundle_name = options[:bundle]) && Merb::Assets.bundle? && stylesheets.size > 1
|
498
610
|
bundler = Merb::Assets::StylesheetAssetBundler.new(bundle_name, *stylesheets)
|
@@ -503,13 +615,22 @@ module Merb
|
|
503
615
|
tags = ""
|
504
616
|
|
505
617
|
for stylesheet in stylesheets
|
506
|
-
href = asset_path(:stylesheet, stylesheet)
|
507
|
-
|
618
|
+
href = css_prefix.to_s + asset_path(:stylesheet, stylesheet)
|
619
|
+
|
620
|
+
if css_suffix
|
621
|
+
ext_length = ASSET_FILE_EXTENSIONS[:stylesheet].length + 1
|
622
|
+
href.insert(-ext_length,css_suffix)
|
623
|
+
end
|
624
|
+
|
625
|
+
if AssetsMixin.append_random_query_string?(options[:reload])
|
626
|
+
href += href.include?('?') ? "&#{random_query_string}" : "?#{random_query_string}"
|
627
|
+
end
|
628
|
+
|
508
629
|
attrs = {
|
509
630
|
:href => href,
|
510
631
|
:type => "text/css",
|
511
632
|
:rel => "Stylesheet",
|
512
|
-
:charset => options[:charset] ||
|
633
|
+
:charset => options[:charset] || "utf-8",
|
513
634
|
:media => options[:media] || :all
|
514
635
|
}
|
515
636
|
tags << %Q{<link #{attrs.to_xml_attributes} />}
|
@@ -542,7 +663,7 @@ module Merb
|
|
542
663
|
# #=>"http://assets1.my-awesome-domain.com/images/hostsexypicture.jpg"
|
543
664
|
def uniq_path(*assets)
|
544
665
|
paths = []
|
545
|
-
assets.
|
666
|
+
assets.flatten.each do |filename|
|
546
667
|
paths.push(Merb::Assets::UniqueAssetPath.build(filename))
|
547
668
|
end
|
548
669
|
paths.length > 1 ? paths : paths.first
|
@@ -564,7 +685,7 @@ module Merb
|
|
564
685
|
# "http://assets1.my-awesome-domain.com/javascripts/home/signup.js"]
|
565
686
|
def uniq_js_path(*assets)
|
566
687
|
paths = []
|
567
|
-
assets.
|
688
|
+
assets.flatten.each do |filename|
|
568
689
|
paths.push(Merb::Assets::UniqueAssetPath.build(asset_path(:javascript,filename)))
|
569
690
|
end
|
570
691
|
paths.length > 1 ? paths : paths.first
|
@@ -586,7 +707,7 @@ module Merb
|
|
586
707
|
# "http://assets1.my-awesome-domain.com/stylesheets/home/signup.css"]
|
587
708
|
def uniq_css_path(*assets)
|
588
709
|
paths = []
|
589
|
-
assets.
|
710
|
+
assets.flatten.each do |filename|
|
590
711
|
paths.push(Merb::Assets::UniqueAssetPath.build(asset_path(:stylesheet,filename)))
|
591
712
|
end
|
592
713
|
paths.length > 1 ? paths : paths.first
|
@@ -644,6 +765,5 @@ module Merb
|
|
644
765
|
def random_query_string
|
645
766
|
Time.now.strftime("%m%d%H%M%S#{rand(99)}")
|
646
767
|
end
|
647
|
-
|
648
768
|
end
|
649
|
-
end
|
769
|
+
end
|
data/lib/merb-assets.rb
CHANGED
@@ -10,5 +10,17 @@ Merb::Plugins.config[:asset_helpers] = {
|
|
10
10
|
:max_hosts => 4,
|
11
11
|
:asset_domain => "assets%s",
|
12
12
|
:domain => "my-awesome-domain.com",
|
13
|
-
:use_ssl => false
|
13
|
+
:use_ssl => false,
|
14
|
+
|
15
|
+
# Global prefix/suffix for css/js include tags, overridable in js_include_tag and css_include_tag
|
16
|
+
#
|
17
|
+
# :js_prefix => "http://cdn.example.com"
|
18
|
+
# require_js :application # => "http://cdn.example.com/javascripts/application.js"
|
19
|
+
#
|
20
|
+
# :js_suffix => "_#{MyApp.version}"
|
21
|
+
# require_js :application # => "/javascripts/application_0.2.2.js"
|
22
|
+
:js_prefix => nil,
|
23
|
+
:js_suffix => nil,
|
24
|
+
:css_prefix => nil,
|
25
|
+
:css_suffix => nil
|
14
26
|
} if Merb::Plugins.config[:asset_helpers].nil?
|
data/spec/merb-assets_spec.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
|
-
require
|
1
|
+
require 'spec_helper'
|
2
|
+
|
2
3
|
include Merb::AssetsMixin
|
3
4
|
|
4
5
|
describe "Accessing Assets" do
|
@@ -58,7 +59,127 @@ describe "Accessing Assets" do
|
|
58
59
|
result = image_tag('foo.gif', :reload => true)
|
59
60
|
result.should match(%r{<img src="/images/foo.gif\?\d+" />})
|
60
61
|
end
|
62
|
+
|
63
|
+
it "should not create image tag with nil image" do
|
64
|
+
image_tag(nil).should == ""
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should not create image tag with empty string" do
|
68
|
+
image_tag('').should == ""
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
describe "With Merb::Config[:path_prefix] set," do
|
73
|
+
before(:all) do
|
74
|
+
Merb::Config[:path_prefix] = "/myapp"
|
75
|
+
end
|
76
|
+
after(:all) do
|
77
|
+
Merb::Config[:path_prefix] = nil
|
78
|
+
end
|
79
|
+
|
80
|
+
describe "create an image tag" do
|
81
|
+
it "with a relative url" do
|
82
|
+
image_tag('foo.gif').should ==
|
83
|
+
"<img src=\"/myapp/images/foo.gif\" />"
|
84
|
+
end
|
85
|
+
|
86
|
+
it "with an absolute url" do
|
87
|
+
image_tag('/foo.gif').should ==
|
88
|
+
"<img src=\"/myapp/foo.gif\" />"
|
89
|
+
end
|
90
|
+
|
91
|
+
it "with an external url" do
|
92
|
+
image_tag('http://example.com/foo.gif').should ==
|
93
|
+
"<img src=\"http://example.com/foo.gif\" />"
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
describe "create a stylesheet include tag" do
|
98
|
+
it "with a relative url" do
|
99
|
+
result = css_include_tag('bar.css')
|
100
|
+
result.should match(%r{^<link})
|
101
|
+
result.should match(%r{media="all"})
|
102
|
+
result.should match(%r{type="text/css"})
|
103
|
+
result.should match(%r{href="/myapp/stylesheets/bar.css"})
|
104
|
+
result.should match(%r{charset="utf-8"})
|
105
|
+
result.should match(%r{rel="Stylesheet"})
|
106
|
+
end
|
107
|
+
|
108
|
+
it "with an absolute url" do
|
109
|
+
result = css_include_tag('/bar.css')
|
110
|
+
result.should match(%r{^<link})
|
111
|
+
result.should match(%r{media="all"})
|
112
|
+
result.should match(%r{type="text/css"})
|
113
|
+
result.should match(%r{href="/myapp/bar.css"})
|
114
|
+
result.should match(%r{charset="utf-8"})
|
115
|
+
result.should match(%r{rel="Stylesheet"})
|
116
|
+
end
|
117
|
+
|
118
|
+
it "with an external url" do
|
119
|
+
result = css_include_tag('http://example.com/bar.css')
|
120
|
+
result.should match(%r{^<link})
|
121
|
+
result.should match(%r{media="all"})
|
122
|
+
result.should match(%r{type="text/css"})
|
123
|
+
result.should match(%r{href="http://example.com/bar.css"})
|
124
|
+
result.should match(%r{charset="utf-8"})
|
125
|
+
result.should match(%r{rel="Stylesheet"})
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
describe "create a javascript include tag" do
|
130
|
+
it "with a relative url" do
|
131
|
+
result = js_include_tag("bar.js")
|
132
|
+
result.should match(%r{^<script}); result.should match(%r{</script>$})
|
133
|
+
result.should match(%r{type="text/javascript"})
|
134
|
+
result.should match(%r{src="/myapp/javascripts/bar.js"})
|
135
|
+
result.should match(%r{charset="utf-8"})
|
136
|
+
end
|
137
|
+
|
138
|
+
it "with an absolute url" do
|
139
|
+
result = js_include_tag("/bar.js")
|
140
|
+
result.should match(%r{^<script}); result.should match(%r{</script>$})
|
141
|
+
result.should match(%r{type="text/javascript"})
|
142
|
+
result.should match(%r{src="/myapp/bar.js"})
|
143
|
+
end
|
144
|
+
|
145
|
+
it "with an external url" do
|
146
|
+
result = js_include_tag("http://example.com/bar.js")
|
147
|
+
result.should match(%r{^<script}); result.should match(%r{</script>$})
|
148
|
+
result.should match(%r{type="text/javascript"})
|
149
|
+
result.should match(%r{src="http://example.com/bar.js"})
|
150
|
+
end
|
151
|
+
|
152
|
+
it "with a specific charset option" do
|
153
|
+
result = js_include_tag("bar.js", :charset => 'iso-8859-1')
|
154
|
+
result.should match(%r{^<script}); result.should match(%r{</script>$})
|
155
|
+
result.should match(%r{type="text/javascript"})
|
156
|
+
result.should match(%r{src="/myapp/javascripts/bar.js"})
|
157
|
+
result.should match(%r{charset="iso-8859-1"})
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
describe "With Merb::Config[:reload_templates] set," do
|
163
|
+
before(:all) do
|
164
|
+
Merb::Config[:reload_templates] = true
|
165
|
+
end
|
166
|
+
after(:all) do
|
167
|
+
Merb::Config[:reload_templates] = false
|
168
|
+
end
|
61
169
|
|
170
|
+
it "should create image tag with absolute url" do
|
171
|
+
image_tag('http://example.com/foo.gif').should ==
|
172
|
+
"<img src=\"http://example.com/foo.gif\" />"
|
173
|
+
end
|
174
|
+
|
175
|
+
it "should create image tag with absolute url and random string if :reload option is set" do
|
176
|
+
image_tag('http://example.com/foo.gif', :reload => true).should =~
|
177
|
+
%r{<img src="http://example.com/foo.gif\?\d+" />}
|
178
|
+
end
|
179
|
+
|
180
|
+
it "should create image tag with relative url" do
|
181
|
+
image_tag('foo.gif').should =~ %r{<img src="/images/foo.gif\?\d+" />}
|
182
|
+
end
|
62
183
|
end
|
63
184
|
|
64
185
|
describe "JavaScript related functions" do
|
@@ -145,13 +266,17 @@ describe "External JavaScript and Stylesheets" do
|
|
145
266
|
end
|
146
267
|
|
147
268
|
it "should create a js include tag with the extension specified" do
|
148
|
-
js_include_tag('jquery.js')
|
149
|
-
|
269
|
+
result = js_include_tag('jquery.js')
|
270
|
+
result.scan(/<script/).should have(1).things
|
271
|
+
result.should match(%r{src="/javascripts/jquery.js"})
|
272
|
+
result.should match(%r{type="text/javascript"})
|
150
273
|
end
|
151
274
|
|
152
275
|
it "should create a js include tag and and the extension" do
|
153
|
-
js_include_tag('jquery')
|
154
|
-
|
276
|
+
result =js_include_tag('jquery')
|
277
|
+
result.scan(/<script/).should have(1).things
|
278
|
+
result.should match(%r{src="/javascripts/jquery.js"})
|
279
|
+
result.should match(%r{type="text/javascript"})
|
155
280
|
end
|
156
281
|
|
157
282
|
it "should create a js include tag for multiple includes" do
|
@@ -244,8 +369,10 @@ describe "External JavaScript and Stylesheets" do
|
|
244
369
|
end
|
245
370
|
|
246
371
|
it "should create a uniq js tag for a single js file" do
|
247
|
-
uniq_js_tag("my")
|
248
|
-
|
372
|
+
result = uniq_js_tag("my")
|
373
|
+
result.scan(/<script/).should have(1).things
|
374
|
+
result.should match(%r{src="http://assets2.my-awesome-domain.com/javascripts/my.js"})
|
375
|
+
result.should match(%r{type="text/javascript"})
|
249
376
|
end
|
250
377
|
|
251
378
|
it "should create a uniq js tag for each js file specified" do
|
@@ -271,4 +398,66 @@ describe "External JavaScript and Stylesheets" do
|
|
271
398
|
result.should match(%r{/stylesheets/style.css})
|
272
399
|
result.should match(%r{/stylesheets/layout.css})
|
273
400
|
end
|
401
|
+
|
402
|
+
it 'should create a js include tag with a prefix' do
|
403
|
+
result = js_include_tag('jquery.js', :effects, :prefix => "http://cdn.example.com")
|
404
|
+
result.scan(/<script/).should have(2).things
|
405
|
+
result.should match(%r{http://cdn.example.com/javascripts/jquery.js})
|
406
|
+
result.should match(%r{http://cdn.example.com/javascripts/effects.js})
|
407
|
+
end
|
408
|
+
|
409
|
+
it 'should create a js include tag with a suffix' do
|
410
|
+
@js_version = "3.0.1"
|
411
|
+
result = js_include_tag('jquery.js', :effects, :suffix => ".#{@js_version}")
|
412
|
+
result.scan(/<script/).should have(2).things
|
413
|
+
result.should match(%r{/javascripts/jquery.3.0.1.js})
|
414
|
+
result.should match(%r{/javascripts/effects.3.0.1.js})
|
415
|
+
end
|
416
|
+
|
417
|
+
it 'should create a css include tag with a prefix' do
|
418
|
+
result = css_include_tag('style.css', :layout, :prefix => "http://cdn.example.com")
|
419
|
+
result.scan(/<link/).should have(2).things
|
420
|
+
result.should match(%r{http://cdn.example.com/stylesheets/style.css})
|
421
|
+
result.should match(%r{http://cdn.example.com/stylesheets/layout.css})
|
422
|
+
end
|
423
|
+
|
424
|
+
it 'should create a css include tag with a suffix' do
|
425
|
+
@css_version = "0.1.3"
|
426
|
+
result = css_include_tag('style.css', :layout, :suffix => ".#{@css_version}")
|
427
|
+
result.scan(/<link/).should have(2).things
|
428
|
+
result.should match(%r{/stylesheets/style.0.1.3.css})
|
429
|
+
result.should match(%r{/stylesheets/layout.0.1.3.css})
|
430
|
+
end
|
431
|
+
end
|
432
|
+
|
433
|
+
describe "using assets unique assets helper in localhost environment" do
|
434
|
+
before(:all) do
|
435
|
+
Merb::Plugins.config[:asset_helpers][:asset_domain] = nil
|
436
|
+
Merb::Plugins.config[:asset_helpers][:domain] = "localhost:4000"
|
437
|
+
end
|
438
|
+
|
439
|
+
after(:all) do
|
440
|
+
Merb::Plugins.config[:asset_helpers][:asset_domain] = "assets%d"
|
441
|
+
Merb::Plugins.config[:asset_helpers][:domain] = "my-awesome-domain.com"
|
442
|
+
end
|
443
|
+
|
444
|
+
it "should return a js path for a single js file" do
|
445
|
+
uniq_js_path("my").should ==
|
446
|
+
"http://localhost:4000/javascripts/my.js"
|
447
|
+
end
|
448
|
+
|
449
|
+
it "should return a js path for multiple js files" do
|
450
|
+
uniq_js_path(["admin/secrets","home/signup"]).should ==
|
451
|
+
["http://localhost:4000/javascripts/admin/secrets.js", "http://localhost:4000/javascripts/home/signup.js"]
|
452
|
+
end
|
453
|
+
|
454
|
+
it "should return a css path for a single css file" do
|
455
|
+
uniq_css_path("my").should ==
|
456
|
+
"http://localhost:4000/stylesheets/my.css"
|
457
|
+
end
|
458
|
+
|
459
|
+
it "should return a css path for multiple css files" do
|
460
|
+
uniq_css_path(["admin/secrets","home/signup"]).should ==
|
461
|
+
["http://localhost:4000/stylesheets/admin/secrets.css", "http://localhost:4000/stylesheets/home/signup.css"]
|
462
|
+
end
|
274
463
|
end
|
data/spec/spec.opts
ADDED
data/spec/spec_helper.rb
CHANGED
@@ -1,9 +1,15 @@
|
|
1
|
-
$:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
|
2
1
|
require "rubygems"
|
3
|
-
|
2
|
+
|
3
|
+
# Use current merb-core sources if running from a typical dev checkout.
|
4
|
+
lib = File.expand_path('../../../merb-core/lib', __FILE__)
|
5
|
+
$LOAD_PATH.unshift(lib) if File.directory?(lib)
|
6
|
+
require 'merb-core'
|
7
|
+
|
8
|
+
# The lib under test
|
4
9
|
require "merb-assets"
|
5
|
-
|
6
|
-
|
10
|
+
|
11
|
+
# Satisfies Autotest and anyone else not using the Rake tasks
|
12
|
+
require 'spec'
|
7
13
|
|
8
14
|
Merb.start :environment => 'test'
|
9
15
|
|
@@ -11,7 +17,6 @@ Merb::Plugins.config[:asset_helpers][:max_hosts] = 4
|
|
11
17
|
Merb::Plugins.config[:asset_helpers][:asset_domain] = "assets%d"
|
12
18
|
Merb::Plugins.config[:asset_helpers][:domain] = "my-awesome-domain.com"
|
13
19
|
|
14
|
-
|
15
20
|
Spec::Runner.configure do |config|
|
16
21
|
config.include Merb::Test::RequestHelper
|
17
|
-
end
|
22
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: merb-assets
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.1.0.pre
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ezra Zygmuntowicz
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date:
|
12
|
+
date: 2010-02-20 00:00:00 +00:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -20,35 +20,47 @@ dependencies:
|
|
20
20
|
requirements:
|
21
21
|
- - ~>
|
22
22
|
- !ruby/object:Gem::Version
|
23
|
-
version: 1.0.
|
23
|
+
version: 1.1.0.pre
|
24
24
|
version:
|
25
|
-
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rspec
|
27
|
+
type: :development
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.2.9
|
34
|
+
version:
|
35
|
+
description: Merb plugin for supporting assets
|
26
36
|
email: ez@engineyard.com
|
27
37
|
executables: []
|
28
38
|
|
29
39
|
extensions: []
|
30
40
|
|
31
41
|
extra_rdoc_files:
|
32
|
-
- README
|
33
42
|
- LICENSE
|
43
|
+
- README
|
34
44
|
- TODO
|
35
45
|
files:
|
36
46
|
- LICENSE
|
37
47
|
- README
|
38
48
|
- Rakefile
|
39
49
|
- TODO
|
50
|
+
- lib/merb-assets.rb
|
40
51
|
- lib/merb-assets/assets.rb
|
41
52
|
- lib/merb-assets/assets_mixin.rb
|
42
|
-
- lib/merb-assets.rb
|
53
|
+
- lib/merb-assets/version.rb
|
43
54
|
- spec/merb-assets_spec.rb
|
55
|
+
- spec/spec.opts
|
44
56
|
- spec/spec_helper.rb
|
45
57
|
has_rdoc: true
|
46
|
-
homepage: http://merbivore.com
|
58
|
+
homepage: http://merbivore.com/
|
47
59
|
licenses: []
|
48
60
|
|
49
61
|
post_install_message:
|
50
|
-
rdoc_options:
|
51
|
-
|
62
|
+
rdoc_options:
|
63
|
+
- --charset=UTF-8
|
52
64
|
require_paths:
|
53
65
|
- lib
|
54
66
|
required_ruby_version: !ruby/object:Gem::Requirement
|
@@ -59,16 +71,16 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
59
71
|
version:
|
60
72
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
73
|
requirements:
|
62
|
-
- - "
|
74
|
+
- - ">"
|
63
75
|
- !ruby/object:Gem::Version
|
64
|
-
version:
|
76
|
+
version: 1.3.1
|
65
77
|
version:
|
66
78
|
requirements: []
|
67
79
|
|
68
|
-
rubyforge_project:
|
80
|
+
rubyforge_project:
|
69
81
|
rubygems_version: 1.3.5
|
70
82
|
signing_key:
|
71
83
|
specification_version: 3
|
72
|
-
summary: Merb plugin that provides
|
84
|
+
summary: Merb plugin that provides helpers for assets and asset bundling
|
73
85
|
test_files: []
|
74
86
|
|