wbzyl-sinatra-static-assets 0.0.8 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +77 -61
- data/VERSION.yml +1 -1
- data/examples/summer/config.ru +4 -0
- data/examples/{app1 → summer}/public/images/tatry1.jpg +0 -0
- data/examples/summer/public/javascripts/app.js +1 -0
- data/examples/{app1/public/stylesheets/app1.css → summer/public/stylesheets/app.css} +0 -0
- data/examples/{app1 → summer}/public/stylesheets/src/bronzed_olive.png +0 -0
- data/examples/{app1/app1.rb → summer/summer.rb} +1 -2
- data/examples/{app1 → summer}/tmp/always_restart.txt +0 -0
- data/examples/{app1 → summer}/views/index.erb +0 -0
- data/examples/{app1 → summer}/views/layout.erb +2 -2
- data/examples/{app2 → winter}/app2.rb +0 -0
- data/examples/{app2 → winter}/config.ru +0 -0
- data/examples/{app2 → winter}/public/images/tatry2.jpg +0 -0
- data/examples/{app2 → winter}/public/javascripts/app2.js +0 -0
- data/examples/{app2 → winter}/public/stylesheets/app2.css +0 -0
- data/examples/{app2 → winter}/public/stylesheets/src/skating.png +0 -0
- data/examples/{app2 → winter}/tmp/always_restart.txt +0 -0
- data/examples/{app2 → winter}/views/index.erb +0 -0
- data/examples/{app2 → winter}/views/layout.erb +0 -0
- metadata +22 -23
- data/examples/app1/config.ru +0 -7
- data/examples/app1/public/javascripts/app1.js +0 -1
- data/examples/sinatra.conf +0 -18
data/README.markdown
CHANGED
@@ -1,91 +1,107 @@
|
|
1
|
-
#
|
1
|
+
# Sinatra Extension: StaticAssets
|
2
2
|
|
3
|
-
|
3
|
+
Gem *sinatra-static-assets* implements the following helpers methods:
|
4
|
+
|
5
|
+
* `stylesheet_link_tag`
|
6
|
+
* `javascript_script_tag`
|
7
|
+
* `image_tag`
|
8
|
+
* `link_tag`
|
9
|
+
|
10
|
+
To install it, run from the command line:
|
4
11
|
|
5
12
|
sudo gem install wbzyl-sinatra-static-assets -s http://gems.github.com
|
6
13
|
|
7
|
-
|
14
|
+
All methods are simple wrappers around the `url_for` method
|
15
|
+
from the [sinatra-url-for](http://github.com/emk/sinatra-url-for/).
|
8
16
|
|
9
|
-
|
10
|
-
gem 'wbzyl-sinatra-static-assets'
|
11
|
-
require 'sinatra/static_assets'
|
17
|
+
## When will you need it?
|
12
18
|
|
13
|
-
|
19
|
+
Whenever you use the
|
20
|
+
[Passenger module for Apache2](http://www.modrails.com/documentation/Users%20guide%20Apache.html#deploying_rack_to_sub_uri)
|
21
|
+
or use `Rack::URLMap` to dispatch an application to
|
22
|
+
sub URI.
|
14
23
|
|
15
|
-
|
16
|
-
|
24
|
+
Suppose that we already have a virtual host `hitch.local`
|
25
|
+
and two Sinatra application which live in
|
26
|
+
`/home/me/www/summer` and `/home/me/www/winter`
|
27
|
+
directories, respectively.
|
28
|
+
We want our Sinatra applications to be accessible from the URLs:
|
29
|
+
`http://hitch.local/summer` and `http://hitch.local/winter`.
|
17
30
|
|
18
|
-
|
19
|
-
|
31
|
+
To configure Apache2 and Passenger to serve our applications
|
32
|
+
we create a new configuration file with the content:
|
20
33
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
34
|
+
<VirtualHost *:80>
|
35
|
+
ServerName hitch.local
|
36
|
+
DocumentRoot /srv/www/hitch.local
|
37
|
+
|
38
|
+
RackBaseURI /summer
|
39
|
+
RackBaseURI /winter
|
40
|
+
</VirtualHost>
|
27
41
|
|
28
|
-
|
29
|
-
application](http://www.modrails.com/documentation/Users guide Apache.html#_deploying_a_rack_based_ruby_application),
|
30
|
-
Deploying to a sub URI.
|
42
|
+
and link the applications directories in `/srv/www/hitch.local`:
|
31
43
|
|
44
|
+
ln -s /home/me/www/summer/public /srv/www/hitch.local/summer
|
45
|
+
ln -s /home/me/www/winter/public /srv/www/hitch.local/winter
|
32
46
|
|
33
|
-
|
47
|
+
After restarting the Apache2 server and visiting, for example the
|
48
|
+
first application at `http://hitch.local/summer` we see that links to
|
49
|
+
images, stylesheets and javascripts are broken.
|
34
50
|
|
35
|
-
|
36
|
-
|
37
|
-
(i.e. http://mysite.com/railsapp/). The reason for this usually is
|
38
|
-
that you used a static URI for your image in the views. This means
|
39
|
-
your img source probably refers to something like /images/foo.jpg. The
|
40
|
-
leading slash means that it's an absolute URI: you're telling the
|
41
|
-
browser to always load http://mysite.com/images/foo.jpg no matter
|
42
|
-
what. The problem is that the image is actually at
|
43
|
-
http://mysite.com/railsapp/images/foo.jpg. There are two ways to fix
|
44
|
-
this.
|
51
|
+
The hitch here is that in Sinatra apps we usually refer to
|
52
|
+
images/stylesheets/javascripts with absolute URI:
|
45
53
|
|
46
|
-
|
47
|
-
methods to output tags for static assets. These helper methods
|
48
|
-
automatically take care of prepending the base URI that you've
|
49
|
-
deployed the application to. For images there is `image_tag`, for
|
50
|
-
JavaScript there is `javascript_include_tag` and for CSS there is
|
51
|
-
`stylesheet_link_tag`. In the above example you would simply remove the
|
52
|
-
`<img>` HTML tag and replace it with inline Ruby like this:
|
54
|
+
/images/tatry1.jpg /stylesheets/app.css /javascripts/app.js
|
53
55
|
|
54
|
-
|
56
|
+
That setup **works** whenever we are running applications locally.
|
57
|
+
The absolute URI above tells browser to load images
|
58
|
+
(stylesheets and javascripts) from:
|
55
59
|
|
56
|
-
|
60
|
+
http://localhost:4567/images/tatry1.jpg
|
57
61
|
|
58
|
-
|
62
|
+
which in turn refer to:
|
59
63
|
|
60
|
-
|
61
|
-
no matter what sub-URI you've deployed to.
|
64
|
+
/home/me/www/summer/public/images/tatry1.jpg
|
62
65
|
|
63
|
-
|
66
|
+
And this is the **correct** directory with the default setup of Sinatra
|
67
|
+
application.
|
64
68
|
|
65
|
-
|
69
|
+
But these absolute URIs do not work whenever, for example,
|
70
|
+
the *summer* application is dispatched to sub URI.
|
71
|
+
Now, images are at:
|
66
72
|
|
67
|
-
|
73
|
+
http://hitch.local/summer/images/tatry1.jpg
|
68
74
|
|
69
|
-
|
75
|
+
but we request images from:
|
70
76
|
|
71
|
-
|
72
|
-
ln -s ~/public_git/forks/sinatra-static-assets/examples/app2/public /srv/www/sinatra/app2
|
77
|
+
http://hitch.local/images/tatry1.jpg
|
73
78
|
|
74
|
-
|
79
|
+
And this **does not work** beacause there is no application
|
80
|
+
dispatched to *images* sub URI.
|
75
81
|
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
RackBaseURI /app1
|
81
|
-
RackBaseURI /app2
|
82
|
-
</VirtualHost>
|
82
|
+
The recommended way to cope with broken links is to use helper
|
83
|
+
methods which automatically take of *prepending*
|
84
|
+
the *RackBaseURI* that we have dispatched application to.
|
83
85
|
|
84
|
-
|
86
|
+
In the above example you would simply remove the `<img>` HTML tag
|
87
|
+
and replace it with inline Ruby like this:
|
88
|
+
|
89
|
+
<%= image_tag("/images/tatry1.jpg", :alt => "Błyszcz, 2159 m") %>
|
90
|
+
|
91
|
+
See also, [How to fix broken images/CSS/JavaScript URIs in sub-URI
|
92
|
+
deployments](http://www.modrails.com/documentation/Users%20guide%20Apache.html#sub_uri_deployment_uri_fix)
|
93
|
+
|
94
|
+
## Usage examples
|
95
|
+
|
96
|
+
To use, include it in a Sinatra application:
|
97
|
+
|
98
|
+
require 'rubygems'
|
99
|
+
gem 'wbzyl-sinatra-static-assets'
|
100
|
+
require 'sinatra/static_assets'
|
85
101
|
|
86
102
|
|
87
|
-
##
|
103
|
+
## Miscellaneous stuff
|
88
104
|
|
89
|
-
|
90
|
-
caching. For more information, see the Rails API docs.
|
105
|
+
[UNIX] To create virual host, add to */etc/hosts*:
|
91
106
|
|
107
|
+
127.0.0.1 localhost.localdomain localhost hitch.local
|
data/VERSION.yml
CHANGED
File without changes
|
@@ -0,0 +1 @@
|
|
1
|
+
/* summer: app.js */
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
@@ -2,8 +2,8 @@
|
|
2
2
|
<html>
|
3
3
|
<head>
|
4
4
|
<meta http-equiv="content-type" content="text/html; charset=utf-8">
|
5
|
-
<%= stylesheet_link_tag "/stylesheets/
|
6
|
-
<%= javascript_script_tag "/javascripts/
|
5
|
+
<%= stylesheet_link_tag "/stylesheets/app.css" %>
|
6
|
+
<%= javascript_script_tag "/javascripts/app.js" %>
|
7
7
|
|
8
8
|
<title><%= @title %></title>
|
9
9
|
</head>
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: wbzyl-sinatra-static-assets
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Wlodek Bzyl
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-06-01 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -64,24 +64,6 @@ extra_rdoc_files:
|
|
64
64
|
files:
|
65
65
|
- TODO
|
66
66
|
- VERSION.yml
|
67
|
-
- examples/app1/app1.rb
|
68
|
-
- examples/app1/config.ru
|
69
|
-
- examples/app1/public/images/tatry1.jpg
|
70
|
-
- examples/app1/public/javascripts/app1.js
|
71
|
-
- examples/app1/public/stylesheets/app1.css
|
72
|
-
- examples/app1/public/stylesheets/src/bronzed_olive.png
|
73
|
-
- examples/app1/tmp/always_restart.txt
|
74
|
-
- examples/app1/views/index.erb
|
75
|
-
- examples/app1/views/layout.erb
|
76
|
-
- examples/app2/app2.rb
|
77
|
-
- examples/app2/config.ru
|
78
|
-
- examples/app2/public/images/tatry2.jpg
|
79
|
-
- examples/app2/public/javascripts/app2.js
|
80
|
-
- examples/app2/public/stylesheets/app2.css
|
81
|
-
- examples/app2/public/stylesheets/src/skating.png
|
82
|
-
- examples/app2/tmp/always_restart.txt
|
83
|
-
- examples/app2/views/index.erb
|
84
|
-
- examples/app2/views/layout.erb
|
85
67
|
- examples/mapp1/config.ru
|
86
68
|
- examples/mapp1/mapp.rb
|
87
69
|
- examples/mapp1/public/images/tatry1.jpg
|
@@ -101,7 +83,24 @@ files:
|
|
101
83
|
- examples/mapp2/views/layout.erb
|
102
84
|
- examples/mapp2/views/mapp.erb
|
103
85
|
- examples/mconfig.ru
|
104
|
-
- examples/
|
86
|
+
- examples/summer/config.ru
|
87
|
+
- examples/summer/public/images/tatry1.jpg
|
88
|
+
- examples/summer/public/javascripts/app.js
|
89
|
+
- examples/summer/public/stylesheets/app.css
|
90
|
+
- examples/summer/public/stylesheets/src/bronzed_olive.png
|
91
|
+
- examples/summer/summer.rb
|
92
|
+
- examples/summer/tmp/always_restart.txt
|
93
|
+
- examples/summer/views/index.erb
|
94
|
+
- examples/summer/views/layout.erb
|
95
|
+
- examples/winter/app2.rb
|
96
|
+
- examples/winter/config.ru
|
97
|
+
- examples/winter/public/images/tatry2.jpg
|
98
|
+
- examples/winter/public/javascripts/app2.js
|
99
|
+
- examples/winter/public/stylesheets/app2.css
|
100
|
+
- examples/winter/public/stylesheets/src/skating.png
|
101
|
+
- examples/winter/tmp/always_restart.txt
|
102
|
+
- examples/winter/views/index.erb
|
103
|
+
- examples/winter/views/layout.erb
|
105
104
|
- lib/sinatra/static_assets.rb
|
106
105
|
- test/sinatra_static_assets_test.rb
|
107
106
|
- test/test_helper.rb
|
@@ -136,7 +135,7 @@ summary: Sinatra extension providing helper methods to output tags for static as
|
|
136
135
|
test_files:
|
137
136
|
- test/test_helper.rb
|
138
137
|
- test/sinatra_static_assets_test.rb
|
139
|
-
- examples/
|
138
|
+
- examples/summer/summer.rb
|
140
139
|
- examples/mapp2/mapp.rb
|
141
140
|
- examples/mapp1/mapp.rb
|
142
|
-
- examples/
|
141
|
+
- examples/winter/app2.rb
|
data/examples/app1/config.ru
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
/* app1.js */
|
data/examples/sinatra.conf
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
<VirtualHost *:80>
|
2
|
-
ServerName sinatra.local
|
3
|
-
DocumentRoot /srv/www/sinatra
|
4
|
-
|
5
|
-
RackBaseURI /app1
|
6
|
-
RackBaseURI /app2
|
7
|
-
|
8
|
-
#<Location /app1>
|
9
|
-
# PassengerAppRoot /path-to-app1-root
|
10
|
-
#</Location>
|
11
|
-
#
|
12
|
-
# or
|
13
|
-
#
|
14
|
-
#<Directory /app1>
|
15
|
-
# PassengerAppRoot /path-to-app1-root
|
16
|
-
#</Directory>
|
17
|
-
|
18
|
-
</VirtualHost>
|