sinatra-js 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +23 -0
- data/CHANGES +4 -0
- data/LICENSE +20 -0
- data/README.rdoc +265 -0
- data/Rakefile +91 -0
- data/VERSION +1 -0
- data/lib/sinatra/js.rb +564 -0
- data/sinatra-js.gemspec +70 -0
- data/spec/fixtures/app/views/js/app.rjs +1 -0
- data/spec/fixtures/public/insert.js +1 -0
- data/spec/fixtures/public/inserted-from-path.js +1 -0
- data/spec/sinatra/js_spec.rb +315 -0
- data/spec/spec_helper.rb +59 -0
- metadata +156 -0
data/sinatra-js.gemspec
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{sinatra-js}
|
8
|
+
s.version = "0.1.5"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["kematzy"]
|
12
|
+
s.date = %q{2010-08-10}
|
13
|
+
s.description = %q{A Sinatra Extension that makes working with JS easy.}
|
14
|
+
s.email = %q{kematzy@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"CHANGES",
|
23
|
+
"LICENSE",
|
24
|
+
"README.rdoc",
|
25
|
+
"Rakefile",
|
26
|
+
"VERSION",
|
27
|
+
"lib/sinatra/js.rb",
|
28
|
+
"sinatra-js.gemspec",
|
29
|
+
"spec/fixtures/app/views/js/app.rjs",
|
30
|
+
"spec/fixtures/public/insert.js",
|
31
|
+
"spec/fixtures/public/inserted-from-path.js",
|
32
|
+
"spec/sinatra/js_spec.rb",
|
33
|
+
"spec/spec_helper.rb"
|
34
|
+
]
|
35
|
+
s.homepage = %q{http://github.com/kematzy/sinatra-js}
|
36
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
37
|
+
s.require_paths = ["lib"]
|
38
|
+
s.rubygems_version = %q{1.3.7}
|
39
|
+
s.summary = %q{A Sinatra Extension that makes working with JS easy.}
|
40
|
+
s.test_files = [
|
41
|
+
"spec/sinatra/js_spec.rb",
|
42
|
+
"spec/spec_helper.rb"
|
43
|
+
]
|
44
|
+
|
45
|
+
if s.respond_to? :specification_version then
|
46
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
47
|
+
s.specification_version = 3
|
48
|
+
|
49
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
50
|
+
s.add_runtime_dependency(%q<sinatra>, [">= 1.0"])
|
51
|
+
s.add_runtime_dependency(%q<sinatra-tags>, [">= 0"])
|
52
|
+
s.add_runtime_dependency(%q<sinatra-assets>, [">= 0"])
|
53
|
+
s.add_development_dependency(%q<sinatra-tests>, [">= 0.1.6"])
|
54
|
+
s.add_development_dependency(%q<rspec>, [">= 1.3.0"])
|
55
|
+
else
|
56
|
+
s.add_dependency(%q<sinatra>, [">= 1.0"])
|
57
|
+
s.add_dependency(%q<sinatra-tags>, [">= 0"])
|
58
|
+
s.add_dependency(%q<sinatra-assets>, [">= 0"])
|
59
|
+
s.add_dependency(%q<sinatra-tests>, [">= 0.1.6"])
|
60
|
+
s.add_dependency(%q<rspec>, [">= 1.3.0"])
|
61
|
+
end
|
62
|
+
else
|
63
|
+
s.add_dependency(%q<sinatra>, [">= 1.0"])
|
64
|
+
s.add_dependency(%q<sinatra-tags>, [">= 0"])
|
65
|
+
s.add_dependency(%q<sinatra-assets>, [">= 0"])
|
66
|
+
s.add_dependency(%q<sinatra-tests>, [">= 0.1.6"])
|
67
|
+
s.add_dependency(%q<rspec>, [">= 1.3.0"])
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
@@ -0,0 +1 @@
|
|
1
|
+
// hi from <%= __FILE__.sub(fixtures_path, '') %>
|
@@ -0,0 +1 @@
|
|
1
|
+
document.write("insert into HTML output works");
|
@@ -0,0 +1 @@
|
|
1
|
+
document.write("insert from path into HTML output also works");
|
@@ -0,0 +1,315 @@
|
|
1
|
+
|
2
|
+
require "#{File.dirname(File.dirname(File.expand_path(__FILE__)))}/spec_helper"
|
3
|
+
|
4
|
+
describe "Sinatra" do
|
5
|
+
|
6
|
+
class MyTestApp
|
7
|
+
register(Sinatra::JS)
|
8
|
+
register(Sinatra::Cache)
|
9
|
+
|
10
|
+
set :cache_enabled, true
|
11
|
+
set :cache_environment, :test
|
12
|
+
set :cache_output_dir, "#{public_fixtures_path}/system/cache"
|
13
|
+
|
14
|
+
set :views, "#{fixtures_path}/app/views"
|
15
|
+
|
16
|
+
get_all_js_requests
|
17
|
+
get_all_js_requests('/javascripts')
|
18
|
+
get_all_js_requests('/custom', :cache => true) # loads from /custom
|
19
|
+
end
|
20
|
+
|
21
|
+
class MyCustomTestApp < Sinatra::Base
|
22
|
+
register(Sinatra::JS)
|
23
|
+
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
it_should_behave_like "MyTestApp"
|
28
|
+
|
29
|
+
describe "JS" do
|
30
|
+
|
31
|
+
describe "VERSION" do
|
32
|
+
|
33
|
+
it "should return the VERSION string" do
|
34
|
+
Sinatra::JS::VERSION.should be_a_kind_of(String)
|
35
|
+
Sinatra::JS::VERSION.should match(/\d\.\d+\.\d+(\.\d)?/)
|
36
|
+
end
|
37
|
+
|
38
|
+
end #/ VERSION
|
39
|
+
|
40
|
+
describe "#self.version" do
|
41
|
+
|
42
|
+
it "should return a version of the Sinatra::JS VERSION string" do
|
43
|
+
Sinatra::JS.version.should be_a_kind_of(String)
|
44
|
+
Sinatra::JS.version.should match(/Sinatra::JS v\d\.\d+\.\d+(\.\d)?/)
|
45
|
+
end
|
46
|
+
|
47
|
+
end #/ #self.version
|
48
|
+
|
49
|
+
|
50
|
+
describe "Configuration" do
|
51
|
+
|
52
|
+
describe "with Default settings" do
|
53
|
+
|
54
|
+
# it "should set the :pagetitle_default_options[:admin] to 'false'" do
|
55
|
+
# MyTestApp.pagetitle_default_options[:admin].should == false
|
56
|
+
# end
|
57
|
+
|
58
|
+
end #/ with Default settings
|
59
|
+
|
60
|
+
describe "with Custom Settings" do
|
61
|
+
|
62
|
+
# it "should set the :pagetitle_default_options[:admin] to 'true'" do
|
63
|
+
# MyCustomTestApp.pagetitle_default_options[:admin].should == true
|
64
|
+
# end
|
65
|
+
|
66
|
+
|
67
|
+
end #/ with Custom Settings
|
68
|
+
|
69
|
+
end #/ Configuration
|
70
|
+
|
71
|
+
|
72
|
+
describe "Helpers" do
|
73
|
+
|
74
|
+
describe "#js" do
|
75
|
+
|
76
|
+
describe "when a block is passed" do
|
77
|
+
|
78
|
+
it "should return inline javascript tag " do
|
79
|
+
markup = app.javascript do
|
80
|
+
"document.write('this works');"
|
81
|
+
end
|
82
|
+
markup.should have_tag('script[@type=text/javascript]', /document\.write\('this works'\);/ )
|
83
|
+
markup.should have_tag('script[@charset=utf-8]')
|
84
|
+
end
|
85
|
+
|
86
|
+
end #/ when a block is passed
|
87
|
+
|
88
|
+
it "should assign type attribute" do
|
89
|
+
erb_app "<%= javascript('jquery.js') %>"
|
90
|
+
|
91
|
+
body.should have_tag('script[@src=/jquery.js]')
|
92
|
+
body.should have_tag('script[@type=text/javascript]')
|
93
|
+
body.should have_tag('script[@charset=utf-8]')
|
94
|
+
end
|
95
|
+
|
96
|
+
it "should assign charset UTF-8 attribute" do
|
97
|
+
erb_app "<%= javascript('jquery.js') %>"
|
98
|
+
body.should have_tag('script[@charset=utf-8]')
|
99
|
+
end
|
100
|
+
|
101
|
+
it "should assign src attribute" do
|
102
|
+
erb_app "<%= javascript('/js/jquery.js') %>"
|
103
|
+
body.should have_tag('script[@src=/js/jquery.js]')
|
104
|
+
end
|
105
|
+
|
106
|
+
it "should accept an array of files" do
|
107
|
+
erb_app "<%= js(['/js/jquery.js','/js/jquery.ui']) %>"
|
108
|
+
# body.should have_tag(:debug)
|
109
|
+
body.should have_tag('script[@src=/js/jquery.js]')
|
110
|
+
body.should have_tag('script[@src=/js/jquery.ui.js]')
|
111
|
+
end
|
112
|
+
|
113
|
+
end #/ #js
|
114
|
+
|
115
|
+
describe "#js_custom_add" do
|
116
|
+
|
117
|
+
it "should add the custom JS to the buffer" do
|
118
|
+
@app.js_custom_add("does this work?")
|
119
|
+
@app.sinatra_js_custom_code.should == ["does this work?"]
|
120
|
+
end
|
121
|
+
|
122
|
+
it "should handle multiple additions of JS to the buffer" do
|
123
|
+
@app.js_custom_add("does this work?")
|
124
|
+
@app.js_custom_add("yes it does")
|
125
|
+
@app.sinatra_js_custom_code.should == ["does this work?", "yes it does"]
|
126
|
+
end
|
127
|
+
|
128
|
+
end #/ #js_custom_add
|
129
|
+
|
130
|
+
describe "#js_custom_add_file" do
|
131
|
+
before(:each) do
|
132
|
+
@used_layout = "<%= js_custom_files %>"
|
133
|
+
end
|
134
|
+
|
135
|
+
it "should add custom JS files to the buffer" do
|
136
|
+
@app.js_custom_add_file('dummy.js')
|
137
|
+
@app.sinatra_js_custom_files.should == ["dummy.js"]
|
138
|
+
end
|
139
|
+
|
140
|
+
it "should handle multiple additions of files to the buffer" do
|
141
|
+
@app.js_custom_add_file('jquery.js')
|
142
|
+
@app.js_custom_add_file(['print', 'jquery'])
|
143
|
+
@app.sinatra_js_custom_files.should == ["jquery.js", "print", "jquery"]
|
144
|
+
end
|
145
|
+
|
146
|
+
it "should handle adding remote custom JS files to the buffer" do
|
147
|
+
erb_app "<% js_custom_add_file('http://jquery.com/jquery.js') %>", :layout => @used_layout
|
148
|
+
body.should have_tag('script[@src=http://jquery.com/jquery.js]')
|
149
|
+
# @app.js_custom_add_file('http://jquery.com/jquery.js')
|
150
|
+
# @app.sinatra_js_custom_files.should == ["http://jquery.com/jquery.js"]
|
151
|
+
end
|
152
|
+
|
153
|
+
it "should embed JS when adding :insert_into_html to the tag" do
|
154
|
+
@app.js_custom_add_file('insert.js', :insert_into_html)
|
155
|
+
@app.sinatra_js_custom_files.should == nil
|
156
|
+
markup = @app.js_custom
|
157
|
+
markup.should match(/<script type="text\/javascript" charset="utf-8">\n/)
|
158
|
+
markup.should match(/\s*document\.write\("insert into HTML output works"\);\n/)
|
159
|
+
markup.should match(/\s*<\/script>\n/)
|
160
|
+
end
|
161
|
+
|
162
|
+
it "should embed JS when adding :insert_into_html and :path to the tag" do
|
163
|
+
@app.js_custom_add_file('inserted-from-path.js', :insert_into_html, "#{public_fixtures_path}/")
|
164
|
+
@app.sinatra_js_custom_files.should == nil
|
165
|
+
markup = @app.js_custom
|
166
|
+
markup.should match(/<script type="text\/javascript" charset="utf-8">\n/)
|
167
|
+
markup.should match(/\s*document\.write\("insert from path into HTML output also works"\);\n/)
|
168
|
+
markup.should match(/\s*<\/script>\n/)
|
169
|
+
end
|
170
|
+
|
171
|
+
it "should NOT embed JS when adding :insert_into_html to the tag and the file is NOT found" do
|
172
|
+
@app.js_custom_add_file('js/does-not-exist.js', :insert_into_html)
|
173
|
+
@app.sinatra_js_custom_files.should == nil
|
174
|
+
@app.js_custom.should == ''
|
175
|
+
end
|
176
|
+
|
177
|
+
end #/ #js_custom_add_file
|
178
|
+
|
179
|
+
describe "#js_custom" do
|
180
|
+
|
181
|
+
it "should return a <script> tag with the custom JS" do
|
182
|
+
@app.js_custom_add("window.alert('this works');")
|
183
|
+
@app.js_custom_add("var doc = document;")
|
184
|
+
expected = @app.js_custom
|
185
|
+
|
186
|
+
expected.should match(/<script type="text\/javascript" charset="utf-8">\n/)
|
187
|
+
# expected.should match(/\s*\$\(document\)\.ready\(\s*function\s*\(\)\s*\{/)
|
188
|
+
expected.should match(/\s*window\.alert\('this works'\);\n/)
|
189
|
+
# expected.should match(/\s*\}\s*\);\n/)
|
190
|
+
expected.should match(/\s*<\/script>\n/)
|
191
|
+
end
|
192
|
+
|
193
|
+
it "should return nothing when no custom JS has been added" do
|
194
|
+
@app.js_custom.should == ''
|
195
|
+
end
|
196
|
+
|
197
|
+
it "should return nothing when no custom JS has been added together with :add_jquery_block" do
|
198
|
+
@app.js_custom('',:add_jquery_block).should == ''
|
199
|
+
end
|
200
|
+
|
201
|
+
it "should reach the HTML output" do
|
202
|
+
erb_app %Q{ <% js_custom_add("window.alert('this works');") %> <% js_custom_add("var doc = document;") %> },
|
203
|
+
:layout => %Q{ <%= js_custom(:jquery_block) %> }
|
204
|
+
# body.should have_tag(:debug)
|
205
|
+
body.should have_tag('script[@type=text/javascript]')
|
206
|
+
body.should match(/\n\s*\$\(document\)\.ready\( function \(\) \{\n\s*window\.alert\('this works'\);\n\s*var doc = document;\n \} \);\n/)
|
207
|
+
end
|
208
|
+
|
209
|
+
end #/ #js_custom
|
210
|
+
|
211
|
+
describe "#js_custom_files" do
|
212
|
+
|
213
|
+
it "should work with a single symbol passed to :js_custom_add_file" do
|
214
|
+
block = %Q[<% js_custom_add_file(:jquery) %>]
|
215
|
+
erb_app block, :layout => %Q{ <%= js_custom_files %> }
|
216
|
+
|
217
|
+
# body.should have_tag(:debug)
|
218
|
+
body.should match(/<!-- custom js files -->\n/)
|
219
|
+
body.should have_tag('script[@src=/jquery.js]') do |link|
|
220
|
+
link.attributes['charset'].should == 'utf-8'
|
221
|
+
link.attributes['type'].should == 'text/javascript'
|
222
|
+
end
|
223
|
+
body.should match(/\s*<!-- \/custom js files -->\n/)
|
224
|
+
end
|
225
|
+
|
226
|
+
it "should work with an absolute path passed to :js_custom_add_file" do
|
227
|
+
block = %Q[<% js_custom_add_file('/js/jquery') %>]
|
228
|
+
block << %Q[<% js_custom_add_file('/js/jquery.ui.js') %>]
|
229
|
+
erb_app block, :layout => %Q{ <%= js_custom_files %> }
|
230
|
+
|
231
|
+
# body.should have_tag(:debug)
|
232
|
+
body.should match(/<!-- custom js files -->\n/)
|
233
|
+
body.should have_tag('script[@src=/js/jquery.js]')
|
234
|
+
body.should have_tag('script[@src=/js/jquery.ui.js]')
|
235
|
+
body.should match(/\s*<!-- \/custom js files -->\n/)
|
236
|
+
end
|
237
|
+
|
238
|
+
it "should work with an array passed to :js_custom_add_file" do
|
239
|
+
block = %Q[<% js_custom_add_file(['jquery.js', 'jquery.ui']) %>]
|
240
|
+
block << %Q[<% js_custom_add_file(['app']) %>]
|
241
|
+
block << %Q[<% js_custom_add_file(['http://remote.com/remote.js']) %>]
|
242
|
+
|
243
|
+
erb_app block, :layout => %Q{ <%= js_custom_files %> }
|
244
|
+
|
245
|
+
# body.should have_tag(:debug)
|
246
|
+
body.should have_tag('script[@src=/jquery.js]')
|
247
|
+
body.should have_tag('script[@src=/app.js]')
|
248
|
+
body.should have_tag('script[@src=http://remote.com/remote.js]')
|
249
|
+
end
|
250
|
+
|
251
|
+
it "should return nothing when no custom JS files have been added" do
|
252
|
+
erb_app %Q[<%= js_custom_files %>]
|
253
|
+
|
254
|
+
# body.should have_tag(:debug)
|
255
|
+
body.should == ''
|
256
|
+
end
|
257
|
+
|
258
|
+
end #/ #js_custom_add_files
|
259
|
+
|
260
|
+
describe "#js_insert_file" do
|
261
|
+
|
262
|
+
it "should have some tests"
|
263
|
+
|
264
|
+
end #/ #js_insert_file
|
265
|
+
|
266
|
+
end #/ Helpers
|
267
|
+
|
268
|
+
|
269
|
+
describe "DSL methods" do
|
270
|
+
|
271
|
+
describe "#get_all_js_requests" do
|
272
|
+
|
273
|
+
it "should get a .rjs javascript using defaults and return it" do
|
274
|
+
get('/js/app.js')
|
275
|
+
|
276
|
+
status.should == 200
|
277
|
+
response.headers['Content-type'].should match(/text\/javascript/)
|
278
|
+
body.should == "// hi from /app/views/js/app.rjs"
|
279
|
+
end
|
280
|
+
|
281
|
+
it "should get a .rjs javascript using a custom path and return it" do
|
282
|
+
get('/javascripts/app.js')
|
283
|
+
|
284
|
+
status.should == 200
|
285
|
+
response.headers['Content-type'].should match(/text\/javascript/)
|
286
|
+
body.should == "// hi from /app/views/js/app.rjs"
|
287
|
+
end
|
288
|
+
|
289
|
+
it "should return a 404 when the javascript is NOT found" do
|
290
|
+
get('/js/does-not-exist.js')
|
291
|
+
|
292
|
+
status.should == 404
|
293
|
+
response.headers['Content-type'].should == "text/html"
|
294
|
+
# NOTE: dependent upon the error handling of Sinatra::Basic, but that's NOT really a core feature
|
295
|
+
# of the Sinatra::Sass extension
|
296
|
+
# body.should have_tag('h1', 'Not Found') #match(/<h1>Not Found<\/h1>\n/)
|
297
|
+
# body.should have_tag('p',/The requested URL \[ \/css\/does-not-exist\.css \] was not found on this server/ )
|
298
|
+
end
|
299
|
+
|
300
|
+
it "should function when given ':cache => Boolean'" do
|
301
|
+
get('/custom/app.js')
|
302
|
+
|
303
|
+
status.should == 200
|
304
|
+
response.headers['Content-type'].should match(/text\/javascript/)
|
305
|
+
body.should == "// hi from /app/views/js/app.rjs"
|
306
|
+
end
|
307
|
+
|
308
|
+
end #/ #get_all_js_requests
|
309
|
+
|
310
|
+
end #/ DSL methods
|
311
|
+
|
312
|
+
|
313
|
+
end #/ JS
|
314
|
+
|
315
|
+
end #/ Sinatra
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
|
2
|
+
::APP_ROOT = "#{File.dirname(File.expand_path(__FILE__))}/fixtures"
|
3
|
+
|
4
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
5
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
6
|
+
|
7
|
+
ENV['RACK_ENV'] = 'test'
|
8
|
+
|
9
|
+
#--
|
10
|
+
# DEPENDENCIES
|
11
|
+
#++
|
12
|
+
%w(
|
13
|
+
sinatra/base
|
14
|
+
).each {|lib| require lib }
|
15
|
+
|
16
|
+
#--
|
17
|
+
## SINATRA EXTENSIONS
|
18
|
+
#++
|
19
|
+
%w(
|
20
|
+
sinatra/tests
|
21
|
+
sinatra/js
|
22
|
+
sinatra/cache
|
23
|
+
).each {|ext| require ext }
|
24
|
+
|
25
|
+
|
26
|
+
Spec::Runner.configure do |config|
|
27
|
+
config.include RspecHpricotMatchers
|
28
|
+
config.include Sinatra::Tests::TestCase
|
29
|
+
config.include Sinatra::Tests::RSpec::SharedSpecs
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
# quick convenience methods..
|
34
|
+
|
35
|
+
def fixtures_path
|
36
|
+
"#{File.dirname(File.expand_path(__FILE__))}/fixtures"
|
37
|
+
end
|
38
|
+
|
39
|
+
def public_fixtures_path
|
40
|
+
"#{fixtures_path}/public"
|
41
|
+
end
|
42
|
+
|
43
|
+
class MyTestApp < Sinatra::Base
|
44
|
+
# need to set the root of the app for the default :cache_fragments_output_dir to work
|
45
|
+
set :root, ::APP_ROOT
|
46
|
+
set :app_dir, "#{APP_ROOT}/apps/base"
|
47
|
+
set :public, "#{fixtures_path}/public"
|
48
|
+
set :views, "#{app_dir}/views"
|
49
|
+
|
50
|
+
register(Sinatra::Tests)
|
51
|
+
|
52
|
+
enable :raise_errors
|
53
|
+
|
54
|
+
end #/class MyTestApp
|
55
|
+
|
56
|
+
|
57
|
+
class Test::Unit::TestCase
|
58
|
+
Sinatra::Base.set :environment, :test
|
59
|
+
end
|