adamh-asset_library 0.3.1 → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/asset_library/helpers.rb +55 -20
- data/lib/asset_library.rb +6 -0
- data/spec/asset_library/helpers_spec.rb +38 -2
- metadata +2 -2
@@ -3,44 +3,79 @@ class AssetLibrary
|
|
3
3
|
def asset_library_javascript_tags(module_key, format = nil)
|
4
4
|
m = AssetLibrary.asset_module(module_key)
|
5
5
|
if AssetLibrary.cache
|
6
|
-
|
7
|
-
|
6
|
+
AssetLibrary.cache_vars[:javascript_tags] ||= {}
|
7
|
+
AssetLibrary.cache_vars[:javascript_tags][module_key] ||= asset_library_priv.script_tag(m.cache_asset)
|
8
8
|
else
|
9
|
-
m.assets(format).collect{|a| script_tag(a
|
9
|
+
m.assets(format).collect{|a| asset_library_priv.script_tag(a)}.join("\n")
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
13
|
def asset_library_stylesheet_tags(module_key, format = nil)
|
14
14
|
m = AssetLibrary.asset_module(module_key)
|
15
15
|
if AssetLibrary.cache
|
16
|
-
|
17
|
-
|
16
|
+
AssetLibrary.cache_vars[:stylesheet_tags] ||= {}
|
17
|
+
AssetLibrary.cache_vars[:stylesheet_tags][[module_key, format]] ||= asset_library_priv.style_tag(m.cache_asset(format))
|
18
18
|
else
|
19
|
-
import_styles_tag(m.assets(format)
|
19
|
+
asset_library_priv.import_styles_tag(m.assets(format))
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
23
23
|
private
|
24
24
|
|
25
|
-
def
|
26
|
-
|
25
|
+
def asset_library_priv
|
26
|
+
@asset_library_priv ||= Priv.new(self)
|
27
27
|
end
|
28
28
|
|
29
|
-
|
30
|
-
|
31
|
-
|
29
|
+
class Priv
|
30
|
+
# Don't pollute helper's class's namespace with all our methods; put
|
31
|
+
# them here instead
|
32
|
+
|
33
|
+
attr_accessor :helper
|
32
34
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
35
|
+
def initialize(helper)
|
36
|
+
@helper = helper
|
37
|
+
end
|
38
|
+
|
39
|
+
def url(asset)
|
40
|
+
absolute_url(asset.relative_url)
|
41
|
+
end
|
42
|
+
|
43
|
+
def absolute_url(relative_url)
|
44
|
+
host = helper.__send__(:compute_asset_host, relative_url) if helper.respond_to?(:compute_asset_host, true)
|
45
|
+
|
46
|
+
if host && !(host =~ %r{^[-a-z]+://})
|
47
|
+
controller = helper.instance_variable_get(:@controller)
|
48
|
+
request = controller && controller.respond_to?(:request) && controller.request
|
49
|
+
host = request && "#{request.protocol}#{host}"
|
50
|
+
end
|
51
|
+
|
52
|
+
if host
|
53
|
+
"#{host}#{relative_url}"
|
54
|
+
else
|
55
|
+
relative_url
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def script_tag(asset)
|
60
|
+
"<script type=\"text/javascript\" src=\"#{url(asset)}\"></script>"
|
61
|
+
end
|
62
|
+
|
63
|
+
def style_tag(asset)
|
64
|
+
"<link rel=\"stylesheet\" type=\"text/css\" href=\"#{url(asset)}\" />"
|
65
|
+
end
|
66
|
+
|
67
|
+
def import_styles_tag(assets)
|
68
|
+
a = []
|
69
|
+
assets.each_slice(30) do |subset|
|
70
|
+
a << import_style_tag(subset)
|
71
|
+
end
|
72
|
+
a.join("\n")
|
37
73
|
end
|
38
|
-
a.join("\n")
|
39
|
-
end
|
40
74
|
|
41
|
-
|
42
|
-
|
43
|
-
|
75
|
+
def import_style_tag(assets)
|
76
|
+
imports = assets.collect{ |a| "@import \"#{url(a)}\";" }
|
77
|
+
"<style type=\"text/css\">\n#{imports.join("\n")}\n</style>"
|
78
|
+
end
|
44
79
|
end
|
45
80
|
end
|
46
81
|
end
|
data/lib/asset_library.rb
CHANGED
@@ -33,9 +33,15 @@ class AssetLibrary
|
|
33
33
|
|
34
34
|
def cache=(cache)
|
35
35
|
@config = nil
|
36
|
+
@cache_vars = nil
|
36
37
|
@cache = cache
|
37
38
|
end
|
38
39
|
|
40
|
+
def cache_vars
|
41
|
+
# We store cache_vars even if not caching--this is our "globals"
|
42
|
+
@cache_vars ||= {}
|
43
|
+
end
|
44
|
+
|
39
45
|
def config
|
40
46
|
return @config if cache && @config
|
41
47
|
ret = if File.exist?(config_path)
|
@@ -4,8 +4,20 @@ require File.dirname(__FILE__) + '/../../lib/asset_library/helpers'
|
|
4
4
|
|
5
5
|
describe(AssetLibrary::Helpers) do
|
6
6
|
before(:each) do
|
7
|
+
@h = nil
|
7
8
|
AssetLibrary.stub!(:root).and_return('/')
|
8
9
|
end
|
10
|
+
before(:each) do
|
11
|
+
@old_cache = AssetLibrary.cache # Empty globals
|
12
|
+
end
|
13
|
+
|
14
|
+
after(:each) do
|
15
|
+
@h = nil
|
16
|
+
end
|
17
|
+
after(:each) do
|
18
|
+
AssetLibrary.cache = @old_cache
|
19
|
+
@old_cache = nil
|
20
|
+
end
|
9
21
|
|
10
22
|
describe('#asset_library_javascript_tags') do
|
11
23
|
describe('when not caching') do
|
@@ -36,11 +48,26 @@ describe(AssetLibrary::Helpers) do
|
|
36
48
|
AssetLibrary.stub!(:asset_module).and_return(m)
|
37
49
|
h.asset_library_javascript_tags(:m).should == '<script type="text/javascript" src="/f.js?123"></script>' + "\n" + '<script type="text/javascript" src="/f2.js?123"></script>'
|
38
50
|
end
|
51
|
+
|
52
|
+
it('should use compute_asset_host if available') do
|
53
|
+
m = mock(:assets => [a('/f.js')])
|
54
|
+
AssetLibrary.stub!(:asset_module).and_return(m)
|
55
|
+
h.should_receive(:compute_asset_host).with('/f.js?123').and_return('http://assets.test')
|
56
|
+
h.asset_library_javascript_tags(:m).should =~ %r{"http://assets.test/f.js\?123"}
|
57
|
+
end
|
58
|
+
|
59
|
+
it('should add request protocol to compute_asset_host output if applicable') do
|
60
|
+
m = mock(:assets => [a('/f.js')])
|
61
|
+
AssetLibrary.stub!(:asset_module).and_return(m)
|
62
|
+
h.stub!(:compute_asset_host).and_return('assets.test')
|
63
|
+
h.instance_variable_set(:@controller, mock(:request => mock(:protocol => 'http://')))
|
64
|
+
h.asset_library_javascript_tags(:m).should =~ %r{"http://assets.test/f.js\?123"}
|
65
|
+
end
|
39
66
|
end
|
40
67
|
|
41
68
|
describe('when caching') do
|
42
69
|
before(:each) do
|
43
|
-
AssetLibrary.
|
70
|
+
AssetLibrary.cache = true
|
44
71
|
end
|
45
72
|
|
46
73
|
it('should output a single <script> tag with the cache filename') do
|
@@ -48,6 +75,14 @@ describe(AssetLibrary::Helpers) do
|
|
48
75
|
AssetLibrary.stub!(:asset_module).and_return(m)
|
49
76
|
h.asset_library_javascript_tags(:m).should == '<script type="text/javascript" src="/cache.js?123"></script>'
|
50
77
|
end
|
78
|
+
|
79
|
+
it('should use compute_asset_host if available') do
|
80
|
+
m = mock(:cache_asset => a('/cache.js'))
|
81
|
+
AssetLibrary.stub!(:asset_module).and_return(m)
|
82
|
+
h.should_receive(:compute_asset_host).with('/cache.js?123').and_return('http://assets.test')
|
83
|
+
h.asset_library_javascript_tags(:m)
|
84
|
+
#h.asset_library_javascript_tags(:m).should =~ %r{"http://assets.test/cache.js\?123"}
|
85
|
+
end
|
51
86
|
end
|
52
87
|
end
|
53
88
|
|
@@ -123,9 +158,10 @@ describe(AssetLibrary::Helpers) do
|
|
123
158
|
end
|
124
159
|
|
125
160
|
def h
|
161
|
+
return @h if @h
|
126
162
|
c = Class.new do
|
127
163
|
include AssetLibrary::Helpers
|
128
164
|
end
|
129
|
-
|
165
|
+
@h = c.new
|
130
166
|
end
|
131
167
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: adamh-asset_library
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- adamh
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-08-
|
12
|
+
date: 2009-08-24 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|