public
Description: Merb More: The Full Stack. Take what you need; leave what you don't.
Homepage: http://www.merbivore.com
Clone URL: git://github.com/wycats/merb-more.git
Search Repo:
Fixes Merb::AssetsMixin errors from #103, also added specs
daikini (author)
Fri Apr 11 13:41:32 -0700 2008
commit  0f419067401aa0ee3b6c9eede46da3cf7bce753d
tree    9b320d8219b7533eaf9510c19c010e4549db7818
parent  f2fdc8a2ae7a9f2d8abaaffe9ad0d10833dc5b71
...
45
46
47
48
 
 
 
49
50
51
...
59
60
61
62
63
64
65
66
...
74
75
76
77
78
79
 
 
 
 
80
81
82
...
89
90
91
92
 
93
94
95
...
45
46
47
 
48
49
50
51
52
53
...
61
62
63
 
 
64
65
66
...
74
75
76
 
 
 
77
78
79
80
81
82
83
...
90
91
92
 
93
94
95
96
0
@@ -45,7 +45,9 @@
0
         if filename !~ /#{'\\' + ASSET_FILE_EXTENSIONS[asset_type]}\Z/
0
           filename << ASSET_FILE_EXTENSIONS[asset_type]
0
         end
0
- filename = "/#{asset_type}s/#{filename}"
0
+ if filename !~ %r{^https?://}
0
+ filename = "/#{asset_type}s/#{filename}"
0
+ end
0
         if local_path
0
           return "public#{filename}"
0
         else
0
@@ -59,8 +61,6 @@
0
     # for downloading static files (css, js, images...)
0
     class UniqueAssetPath
0
       class << self
0
- @@config = Merb::Plugins.config[:asset_helpers]
0
-
0
         # Builds the path to the file based on the name
0
         #
0
         # ==== Parameters
0
@@ -74,9 +74,10 @@
0
         # # => "https://assets5.my-awesome-domain.com/javascripts/my_fancy_script.js"
0
         #
0
         def build(filename)
0
- #%{#{(USE_SSL ? 'https' : 'http')}://#{sprintf(@@config[:asset_domain],self.calculate_host_id(file))}.#{@@config[:domain]}/#{filename}}
0
- path = @@config[:use_ssl] ? 'https://' : 'http://'
0
- path << sprintf(@@config[:asset_domain],self.calculate_host_id(filename)) << ".#{@@config[:domain]}"
0
+ config = Merb::Plugins.config[:asset_helpers]
0
+ #%{#{(USE_SSL ? 'https' : 'http')}://#{sprintf(config[:asset_domain],self.calculate_host_id(file))}.#{config[:domain]}/#{filename}}
0
+ path = config[:use_ssl] ? 'https://' : 'http://'
0
+ path << sprintf(config[:asset_domain],self.calculate_host_id(filename)) << ".#{config[:domain]}"
0
           path << "/" if filename.index('/') != 0
0
           path << filename
0
         end
0
@@ -89,7 +90,7 @@
0
           filename.each_byte {|byte|
0
             ascii_total += byte
0
           }
0
- (ascii_total % @@config[:max_hosts] + 1)
0
+ (ascii_total % Merb::Plugins.config[:asset_helpers][:max_hosts] + 1)
0
         end
0
       end
0
     end
...
20
21
22
23
 
24
25
26
...
102
103
104
105
 
106
107
108
...
334
335
336
337
 
338
339
340
...
466
467
468
469
 
470
471
472
...
482
483
484
485
 
486
487
488
...
504
505
506
507
 
508
509
510
...
526
527
528
529
 
530
531
532
...
542
543
544
545
 
546
547
548
...
556
557
558
559
 
560
561
562
...
20
21
22
 
23
24
25
26
...
102
103
104
 
105
106
107
108
...
334
335
336
 
337
338
339
340
...
466
467
468
 
469
470
471
472
...
482
483
484
 
485
486
487
488
...
504
505
506
 
507
508
509
510
...
526
527
528
 
529
530
531
532
...
542
543
544
 
545
546
547
548
...
556
557
558
 
559
560
561
562
0
@@ -20,7 +20,7 @@
0
     # # => <a href="http://www.ruby-lang.org" class="special" target="blank">The Ruby home page</a>
0
     #
0
     # link_to p.title, "/blog/show/#{p.id}"
0
- # # => <a href="blog/show/13">The Entry Title</a>
0
+ # # => <a href="/blog/show/13">The Entry Title</a>
0
     #
0
     def link_to(name, url='', opts={})
0
       opts[:href] ||= url
0
@@ -102,7 +102,7 @@
0
     # link_to_function('Add to cart', "item_total += 1; alert('Item added!');")
0
     # # => <a href="#" onclick="item_total += 1; alert('Item added!'); return false;">Add to cart</a>
0
     def link_to_function(name, function)
0
- %{<a href="#" onclick="#{function}; return false;">#{name}</a>}
0
+ %{<a href="#" onclick="#{function.chomp(";")}; return false;">#{name}</a>}
0
     end
0
     
0
     # ==== Parameters
0
@@ -334,7 +334,7 @@
0
     # include_required_css
0
     # # => <link href="/stylesheets/style.css" media="all" rel="Stylesheet" type="text/css"/>
0
     #
0
- # # my_action.herb has a call to require_js 'style', 'ie-specific'
0
+ # # my_action.herb has a call to require_css 'style', 'ie-specific'
0
     # # File: layout/application.html.erb
0
     # include_required_css
0
     # # => <link href="/stylesheets/style.css" media="all" rel="Stylesheet" type="text/css"/>
0
@@ -466,7 +466,7 @@
0
     # String:: if only a single path is requested
0
     # ==== Examples
0
     # uniq_path("/javascripts/my.js","/javascripts/my.css")
0
- # #=> ["http://assets2.my-awesome-domain.com/javascripts/my.js", "http://assets2.my-awesome-domain.com/javascripts/my.css"]
0
+ # #=> ["http://assets2.my-awesome-domain.com/javascripts/my.js", "http://assets1.my-awesome-domain.com/javascripts/my.css"]
0
     #
0
     # uniq_path(["/javascripts/my.js","/stylesheets/my.css"])
0
     # #=> ["http://assets2.my-awesome-domain.com/javascripts/my.js", "http://assets1.my-awesome-domain.com/stylesheets/my.css"]
0
@@ -482,7 +482,7 @@
0
     def uniq_path(*assets)
0
       paths = []
0
       assets.collect.flatten.each do |filename|
0
- paths.push(UniqueAssetPath.build(filename))
0
+ paths.push(Merb::Assets::UniqueAssetPath.build(filename))
0
       end
0
       paths.length > 1 ? paths : paths.first
0
     end
0
@@ -504,7 +504,7 @@
0
     def uniq_js_path(*assets)
0
       paths = []
0
       assets.collect.flatten.each do |filename|
0
- paths.push(UniqueAssetPath.build(asset_path(:javascript,filename,true)))
0
+ paths.push(Merb::Assets::UniqueAssetPath.build(asset_path(:javascript,filename)))
0
       end
0
       paths.length > 1 ? paths : paths.first
0
     end
0
@@ -526,7 +526,7 @@
0
     def uniq_css_path(*assets)
0
       paths = []
0
       assets.collect.flatten.each do |filename|
0
- paths.push(UniqueAssetPath.build(asset_path(:stylesheet,filename,true)))
0
+ paths.push(Merb::Assets::UniqueAssetPath.build(asset_path(:stylesheet,filename)))
0
       end
0
       paths.length > 1 ? paths : paths.first
0
     end
0
@@ -542,7 +542,7 @@
0
     # uniq_js_tag("my")
0
     # #=> <script type="text/javascript" src="http://assets2.my-awesome-domain.com/javascripts/my.js"></script>
0
     def uniq_js_tag(*assets)
0
- js_include_tag(uniq_js_path(assets))
0
+ js_include_tag(*uniq_js_path(assets))
0
     end
0
     
0
     # ==== Parameters
0
@@ -556,7 +556,7 @@
0
     # uniq_css_tag("my")
0
     # #=> <link href="http://assets2.my-awesome-domain.com/stylesheets/my.css" type="text/css" />
0
     def uniq_css_tag(*assets)
0
- css_include_tag(uniq_css_path(assets))
0
+ css_include_tag(*uniq_css_path(assets))
0
     end
0
   end
0
 end
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
0
@@ -1 +1,241 @@
0
+require File.dirname(__FILE__) + '/spec_helper'
0
+include Merb::AssetsMixin
0
+
0
+describe "Accessing Assets" do
0
+ it "should create link to name with absolute url" do
0
+ link_to("The Merb home page", "http://www.merbivore.com/").should ==
0
+ "<a href=\"http://www.merbivore.com/\">The Merb home page</a>"
0
+ end
0
+
0
+ it "should create link to name with relative url" do
0
+ link_to("The Entry Title", "/blog/show/13").should ==
0
+ "<a href=\"/blog/show/13\">The Entry Title</a>"
0
+ end
0
+
0
+ it "should create link with attributes" do
0
+ link_to("The Ruby home page", "http://www.ruby-lang.org", {'class' => 'special', 'target' => 'blank'}).should ==
0
+ "<a class=\"special\" href=\"http://www.ruby-lang.org\" target=\"blank\">The Ruby home page</a>"
0
+ end
0
+
0
+ it "should create link with explicit href" do
0
+ link_to("The Foo home page", "http://not.foo.example.com/", :href => "http://foo.example.com").should ==
0
+ "<a href=\"http://foo.example.com\">The Foo home page</a>"
0
+ end
0
+
0
+ it "should create image tag with absolute url" do
0
+ image_tag('http://example.com/foo.gif').should ==
0
+ "<img src=\"http://example.com/foo.gif\" />"
0
+ end
0
+
0
+ it "should create image tag with relative url" do
0
+ image_tag('foo.gif').should ==
0
+ "<img src=\"/images/foo.gif\" />"
0
+ end
0
+
0
+ it "should create image tag with class" do
0
+ result = image_tag('foo.gif', :class => 'bar')
0
+ result.should match(%r{<img .*? />})
0
+ result.should match(%r{src="/images/foo.gif"})
0
+ result.should match(/class="bar"/)
0
+ end
0
+
0
+ it "should create image tag with specified path" do
0
+ image_tag('foo.gif', :path => '/files/').should ==
0
+ "<img src=\"/files/foo.gif\" />"
0
+ end
0
+
0
+ it "should create image tag without extension" do
0
+ image_tag('/dynamic/charts').should ==
0
+ "<img src=\"/dynamic/charts\" />"
0
+ end
0
+
0
+ it "should create image tag without extension and with specified path" do
0
+ image_tag('charts', :path => '/dynamic/').should ==
0
+ "<img src=\"/dynamic/charts\" />"
0
+ end
0
+
0
+end
0
+
0
+describe "JavaScript related functions" do
0
+ it "should escape js having quotes" do
0
+ escape_js("'Lorem ipsum!' -- Some guy").should ==
0
+ "\\'Lorem ipsum!\\' -- Some guy"
0
+ end
0
+
0
+ it "should escape js having new lines" do
0
+ escape_js("Please keep text\nlines as skinny\nas possible.").should ==
0
+ "Please keep text\\nlines as skinny\\nas possible."
0
+ end
0
+
0
+ it "should create link to a function" do
0
+ link_to_function('Click me', "alert('hi!')").should ==
0
+ "<a href=\"#\" onclick=\"alert('hi!'); return false;\">Click me</a>"
0
+ end
0
+
0
+ it "should create a link to a function having multiple statements" do
0
+ link_to_function('Add to cart', "item_total += 1; alert('Item added!');").should ==
0
+ "<a href=\"#\" onclick=\"item_total += 1; alert('Item added!'); return false;\">Add to cart</a>"
0
+ end
0
+
0
+ it "should convert objects that respond to to_json to json" do
0
+ js({'user' => 'Lewis', 'page' => 'home'}).should ==
0
+ "{\"user\":\"Lewis\",\"page\":\"home\"}"
0
+ end
0
+
0
+ it "should convert objects using inspect that don't respond to_json to json" do
0
+ js([ 1, 2, {"a"=>3.141}, false, true, nil, 4..10 ]).should ==
0
+ "[1,2,{\"a\":3.141},false,true,null,\"4..10\"]"
0
+ end
0
+end
0
+
0
+describe "External JavaScript and Stylesheets" do
0
+ it "should require a js file only once" do
0
+ require_js 'jquery'
0
+ require_js 'jquery', 'effects'
0
+
0
+ include_required_js.scan(%r{/javascripts/jquery.js}).should have(1).things
0
+ include_required_js.scan(%r{/javascripts/effects.js}).should have(1).things
0
+ end
0
+
0
+ it "should require a css file only once" do
0
+ require_css('style')
0
+ require_css('style', 'ie-specific')
0
+
0
+ include_required_css.scan(%r{/stylesheets/style.css}).should have(1).things
0
+ include_required_css.scan(%r{/stylesheets/ie-specific.css}).should have(1).things
0
+ end
0
+
0
+ it "should require included js" do
0
+ require_js 'jquery', 'effects', 'validation'
0
+ result = include_required_js
0
+ result.scan(/<script/).should have(3).things
0
+ result.should match(%r{/javascripts/jquery.js})
0
+ result.should match(%r{/javascripts/effects.js})
0
+ result.should match(%r{/javascripts/validation.js})
0
+ end
0
+
0
+ it "should require included css" do
0
+ require_css 'style', 'ie-specific'
0
+ result = include_required_css
0
+ result.scan(/<link/).should have(2).things
0
+ result.should match(%r{/stylesheets/style.css})
0
+ result.should match(%r{/stylesheets/ie-specific.css})
0
+ end
0
+
0
+ it "should create a js include tag with the extension specified" do
0
+ js_include_tag('jquery.js').should ==
0
+ "<script type=\"text/javascript\" src=\"/javascripts/jquery.js\"></script>"
0
+ end
0
+
0
+ it "should create a js include tag and and the extension" do
0
+ js_include_tag('jquery').should ==
0
+ "<script type=\"text/javascript\" src=\"/javascripts/jquery.js\"></script>"
0
+ end
0
+
0
+ it "should create a js include tag for multiple includes" do
0
+ result = js_include_tag('jquery.js', :effects)
0
+ result.scan(/<script/).should have(2).things
0
+ result.should match(%r{/javascripts/jquery.js})
0
+ result.should match(%r{/javascripts/effects.js})
0
+ end
0
+
0
+ it "should create a css include tag with the extension specified" do
0
+ result = css_include_tag('style.css')
0
+ result.should match(%r{<link (.*?) />})
0
+ result.should match(/charset="utf-8"/)
0
+ result.should match(%r{type="text/css"})
0
+ result.should match(%r{href="/stylesheets/style.css"})
0
+ result.should match(%r{rel="Stylesheet"})
0
+ result.should match(%r{media="all"})
0
+ end
0
+
0
+ it "should create a css include tag and add the extension" do
0
+ result = css_include_tag('style')
0
+ result.should match(%r{<link (.*?) />})
0
+ result.should match(/charset="utf-8"/)
0
+ result.should match(%r{type="text/css"})
0
+ result.should match(%r{href="/stylesheets/style.css"})
0
+ result.should match(%r{rel="Stylesheet"})
0
+ result.should match(%r{media="all"})
0
+ end
0
+
0
+ it "should create a css include tag for multiple includes" do
0
+ result = css_include_tag('style.css', :layout)
0
+ result.scan(/<link/).should have(2).things
0
+ result.should match(%r{/stylesheets/style.css})
0
+ result.should match(%r{/stylesheets/layout.css})
0
+ end
0
+
0
+ it "should create a css include tag with the specified media" do
0
+ css_include_tag('style', :media => :print).should match(%r{media="print"})
0
+ end
0
+
0
+ it "should create a css include tag with the specified charset" do
0
+ css_include_tag('style', :charset => 'iso-8859-1').should match(%r{charset="iso-8859-1"})
0
+ end
0
+
0
+ it "should return a uniq path for a single asset" do
0
+ uniq_path("/javascripts/my.js").should ==
0
+ "http://assets2.my-awesome-domain.com/javascripts/my.js"
0
+ end
0
+
0
+ it "should return a uniq path for multiple assets" do
0
+ uniq_path("/javascripts/my.js","/javascripts/my.css").should ==
0
+ ["http://assets2.my-awesome-domain.com/javascripts/my.js", "http://assets2.my-awesome-domain.com/javascripts/my.css"]
0
+ end
0
+
0
+ it "should return a uniq path for multiple assets passed as a single array" do
0
+ uniq_path(["/javascripts/my.js","/javascripts/my.css"]).should ==
0
+ ["http://assets2.my-awesome-domain.com/javascripts/my.js", "http://assets2.my-awesome-domain.com/javascripts/my.css"]
0
+ end
0
+
0
+ it "should return a uniq js path for a single js file" do
0
+ uniq_js_path("my").should ==
0
+ "http://assets2.my-awesome-domain.com/javascripts/my.js"
0
+ end
0
+
0
+ it "should return a uniq js path for multiple js files" do
0
+ uniq_js_path(["admin/secrets","home/signup"]).should ==
0
+ ["http://assets1.my-awesome-domain.com/javascripts/admin/secrets.js", "http://assets2.my-awesome-domain.com/javascripts/home/signup.js"]
0
+ end
0
+
0
+ it "should return a uniq css path for a single css file" do
0
+ uniq_css_path("my").should ==
0
+ "http://assets1.my-awesome-domain.com/stylesheets/my.css"
0
+ end
0
+
0
+ it "should return a uniq css path for multiple css files" do
0
+ uniq_css_path(["admin/secrets","home/signup"]).should ==
0
+ ["http://assets4.my-awesome-domain.com/stylesheets/admin/secrets.css", "http://assets1.my-awesome-domain.com/stylesheets/home/signup.css"]
0
+ end
0
+
0
+ it "should create a uniq js tag for a single js file" do
0
+ uniq_js_tag("my").should ==
0
+ "<script type=\"text/javascript\" src=\"http://assets2.my-awesome-domain.com/javascripts/my.js\"></script>"
0
+ end
0
+
0
+ it "should create a uniq js tag for each js file specified" do
0
+ result = uniq_js_tag("jquery.js", :effects)
0
+ result.scan(/<script/).should have(2).things
0
+ result.should match(%r{/javascripts/jquery.js})
0
+ result.should match(%r{/javascripts/effects.js})
0
+ end
0
+
0
+ it "should create a uniq css tag for a single css file" do
0
+ result = uniq_css_tag("my")
0
+ result.should match(%r{<link (.*?) />})
0
+ result.should match(/charset="utf-8"/)
0
+ result.should match(%r{type="text/css"})
0
+ result.should match(%r{http://assets1.my-awesome-domain.com/stylesheets/my.css})
0
+ result.should match(%r{rel="Stylesheet"})
0
+ result.should match(%r{media="all"})
0
+ end
0
+
0
+ it "should create a uniq css tag for each css file specified" do
0
+ result = uniq_css_tag("style.css", :layout)
0
+ result.scan(/<link/).should have(2).things
0
+ result.should match(%r{/stylesheets/style.css})
0
+ result.should match(%r{/stylesheets/layout.css})
0
+ end
0
+end
...
1
2
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
0
@@ -1,3 +1,14 @@
0
 $TESTING=true
0
 $:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
0
+require "rubygems"
0
+require "merb-core"
0
+require "merb-assets"
0
+# require File.dirname(__FILE__) / "controllers" / "action-args"
0
+require "spec"
0
+
0
+Merb.start :environment => 'test'
0
+
0
+Spec::Runner.configure do |config|
0
+ config.include Merb::Test::RequestHelper
0
+end

Comments

    No one has commented yet.