Skip to content

Commit 3bdc090

Browse files
author
Seiichi Yonezawa
committed
2 parents 50ad089 + d7fb5b1 commit 3bdc090

38 files changed

+278
-313
lines changed

README.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
Sinatra Recipes
2-
---------------
1+
# Sinatra Recipes
32

43
Community contributed recipes and techniques for the [Sinatra Web
54
Framework][sinatra].
@@ -18,7 +17,7 @@ the first place you should go if you're looking to contribute.
1817

1918
Once you have [forked the project][github-forking] send a [pull
2019
request][github-pull-requests], just be sure to follow the [styling
21-
guidelines][style-guidelines].
20+
guidelines][style-guidelines].
2221

2322
You can also get a hold of us on [irc][irc] or the [mailinglist][mailinglist].
2423

@@ -86,5 +85,3 @@ wiki][translations].
8685
[mailinglist]: http://groups.google.com/group/sinatrarb
8786
[github-forking]: http://help.github.com/forking/
8887
[github-pull-requests]: http://help.github.com/pull-requests/
89-
90-

app.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,9 @@ def get_activity_by_author
270270

271271
@import 'public/stylesheets/gridset.scss'
272272

273+
html
274+
background-color: white
275+
273276
body
274277
font-family: 'Lucida Grande', Verdana, sans-serif
275278
font-size: 14px

asset_management/README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
# Asset Management
22

3-
There are quite a few reasons why a production app should have proper
3+
There are quite a few reasons why a production app should have proper
44
asset management (CSS, JS, Image files). To get an overview of what asset-
5-
management means or why it is required, a section "Why asset management" is
5+
management means or why it is required, a section "Why asset management" is
66
added to this topic below.
77

88
__Ruby-based Assetmangement gems:__
9-

asset_management/sinatra_assetpack.md

Lines changed: 41 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## Sinatra-Assetpack
1+
# Sinatra-Assetpack
22

33
Install the gem using:
44

@@ -22,18 +22,17 @@ class App < Sinatra::Base
2222
end
2323
```
2424

25-
26-
### Generic Structure
25+
## Generic Structure
2726

2827
__Defaults__
2928

30-
If you do not use any CSS dev tools such as Compass or Foundation, and have a
31-
simple app structure that you generate on a per-project basis, there are
32-
certain defaults added to `Sinatra::Assetpack`. By default, the gem assumes
33-
your asset files are located under a folder called "app" in your app's root
29+
If you do not use any CSS dev tools such as Compass or Foundation, and have a
30+
simple app structure that you generate on a per-project basis, there are
31+
certain defaults added to `Sinatra::Assetpack`. By default, the gem assumes
32+
your asset files are located under a folder called "app" in your app's root
3433
directory.
3534

36-
This is the default structure which when used, makes it possible to use the
35+
This is the default structure which when used, makes it possible to use the
3736
gem with almost zero configuration.
3837

3938
```
@@ -51,12 +50,12 @@ myapp.rb
5150

5251
Some points of note:
5352

54-
* There is no need to stick to this structure. The filepaths can be
55-
configured in `sinatra-assetpack` in case you need it.
56-
* There is no need for the `public` folder which is the default asset
53+
* There is no need to stick to this structure. The filepaths can be
54+
configured in `sinatra-assetpack` in case you need it.
55+
* There is no need for the `public` folder which is the default asset
5756
look-up path for Sinatra.
58-
* The `.sass` files go into the `css` directory. The conversion will be
59-
handled by `sinatra-assetpack` automatically. You just need the `sass` gem
57+
* The `.sass` files go into the `css` directory. The conversion will be
58+
handled by `sinatra-assetpack` automatically. You just need the `sass` gem
6059
installed and loaded using `require 'sass'`.
6160

6261
Inside the `myapp.rb` file, inside the `assets do .. end` block,
@@ -84,7 +83,7 @@ assets do
8483
end
8584
```
8685

87-
The symbol that is sent to the `js` and `css` methods becomes the access
86+
The symbol that is sent to the `js` and `css` methods becomes the access
8887
helper in your views. You can use those helpers like so:
8988

9089
```ruby
@@ -100,7 +99,7 @@ Which gets expanded to:
10099

101100
That is it!
102101

103-
### Custom structure
102+
## Custom structure
104103

105104
__Level 2__
106105

@@ -119,7 +118,7 @@ assets
119118
myapp.rb
120119
```
121120

122-
The `serve` method can be specified so that the folder from which the
121+
The `serve` method can be specified so that the folder from which the
123122
assets gets served from may be explained to the gem.
124123

125124
```ruby
@@ -131,39 +130,39 @@ assets gets served from may be explained to the gem.
131130
'/js/app.js'
132131
# You can also do this: 'js/*.js'
133132
]
134-
133+
135134
serve '/css', :from => 'assets/stylesheets'
136135
css :application, [
137136
'/css/jqueryui.css',
138137
'/css/reset.css',
139138
'/css/foundation.sass',
140139
'/css/app.sass'
141140
]
142-
141+
143142
js_compression :jsmin
144143
css_compression :sass
145144
end
146145
```
147146

148147
And everything else remains the same.
149148

150-
### Foundation framework + Compass
149+
## Foundation framework + Compass
151150

152151
__Level Awesome__
153152

154-
The previous sections dealt with limited number of assets. What if
153+
The previous sections dealt with limited number of assets. What if
155154
you have vendor assets that need to be served in a particular order?
156155

157-
This section deals with using the foundation framework with
158-
`sinatra-assetpack`. The example also uses the `compass` gem to start a
159-
project with the `zurb-foundation` framework.
156+
This section deals with using the foundation framework with
157+
`sinatra-assetpack`. The example also uses the `compass` gem to start a
158+
project with the `zurb-foundation` framework.
160159

161160
```
162161
gem install zurb-foundation
163162
gem install compass
164163
```
165164

166-
Complete instructions are not provided here. Follow the Zurb-foundation
165+
Complete instructions are not provided here. Follow the Zurb-foundation
167166
link provided in the links section for detailed instructions
168167

169168
The created project has a structure like this:
@@ -200,16 +199,16 @@ The created project has a structure like this:
200199
myapp.rb
201200
```
202201

203-
In this app, the `.sass` to `.css` conversion is handled by running
204-
`compass watch` which compiles the `.scss` files whenever it detects a
205-
change. So, we'll ignore the conversion for now. The concentration would
202+
In this app, the `.sass` to `.css` conversion is handled by running
203+
`compass watch` which compiles the `.scss` files whenever it detects a
204+
change. So, we'll ignore the conversion for now. The concentration would
206205
be on how to configure the app to get the files in a particular order.
207206

208207
In this case, the order of the JS files that need to be loaded/merged is:
209208

210209
1. Vendor JS files like jQuery, Modernizr and Zepto.
211210

212-
2. The "foundation.js" file which defines the `Foundation` prototype that
211+
2. The "foundation.js" file which defines the `Foundation` prototype that
213212
gets used in the rest of the `foundation.*.js` files.
214213

215214
3. The `foundation.*.js` files.
@@ -229,13 +228,13 @@ assets do
229228
'/js/vendor/*.js',
230229
'/js/app.js'
231230
]
232-
231+
233232
serve '/css', :from => 'stylesheets'
234233
css :application, [
235234
'/css/normalize.css',
236235
'/css/app.css'
237236
]
238-
237+
239238
js_compression :jsmin
240239
css_compression :sass
241240
end
@@ -249,27 +248,27 @@ Inside the view:
249248
<%= js :foundation %>
250249
```
251250
252-
Do this, and you'll see that the files are loaded properly and there will be
251+
Do this, and you'll see that the files are loaded properly and there will be
253252
no JS errors in the browser's console.
254253
255-
### Merging
254+
## Merging
256255
257-
If you have tried the code samples, you might've observed that the files are
258-
not getting merged before they are sent to the browser. This is true and
256+
If you have tried the code samples, you might've observed that the files are
257+
not getting merged before they are sent to the browser. This is true and
259258
happens only in development mode. Change it to production:
260259
261260
```
262261
export RACK_ENV="production"
263262
```
264263
265-
Voila! Now you should see only three asset files being loaded viz.,
264+
Voila! Now you should see only three asset files being loaded viz.,
266265
"application.css", "application.js" and "foundation.js".
267266
268-
### Pre-compilation
267+
## Pre-compilation
269268
270-
Up until now, the concatenation, compression are done after a request
271-
reaches the server for the first time. This step is done only once for the
272-
first request. However, if you need to pre-generate the compressed and
269+
Up until now, the concatenation, compression are done after a request
270+
reaches the server for the first time. This step is done only once for the
271+
first request. However, if you need to pre-generate the compressed and
273272
concatenated assets, you can use the rake task provided in the gem:
274273
275274
Create a `Rakefile` in your app directory with the following contents:
@@ -281,12 +280,11 @@ APP_CLASS = 'Sinatra::Application'
281280
require 'sinatra/assetpack/rake'
282281
```
283282
284-
And run `rake assetpack:build` to generate the assets which automatically
285-
get stored in a `public` directory.
283+
And run `rake assetpack:build` to generate the assets which automatically
284+
get stored in a `public` directory.
286285
287-
### Resources
286+
## Resources
288287
289288
* [Sinatra-Assetpack](https://github.com/rstacruz/sinatra-assetpack)
290289
* [Foundation Framework](http://foundation.zurb.com/docs/index.html)
291290
* [Compass Framework](http://compass-style.org/)
292-
Lines changed: 35 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,37 @@
1-
## Why Asset Management
1+
# Why Asset Management
22

3-
4-
A typical app ends up with many `.css` and `.js` (or `.coffee` for
5-
that matter) especially if you use a lot of external plugins such as
3+
A typical app ends up with many `.css` and `.js` (or `.coffee` for
4+
that matter) especially if you use a lot of external plugins such as
65
the Compass framework or say, Ember.js
76

8-
Serving uncompressed/minified assets is not advisable and that is one
9-
of the reason why some of the popular libraries like jQuery and Ember
7+
Serving uncompressed/minified assets is not advisable and that is one
8+
of the reason why some of the popular libraries like jQuery and Ember
109
provide a minified version of the library out of the box.
1110

12-
Even if you have a minifed version of those vendor plugins, if you opt to
13-
follow best practices and write your `.css`|`.sass` and `.js`|`.coffee`
14-
code in a modular fashion, you'll end up with a bunch of those files that
11+
Even if you have a minifed version of those vendor plugins, if you opt to
12+
follow best practices and write your `.css`|`.sass` and `.js`|`.coffee`
13+
code in a modular fashion, you'll end up with a bunch of those files that
1514
need to be served to the end-user.
1615

17-
To optimize and better manage your workflow involving your app's assets,
18-
there are quite a few tools available. Some of the tools are available as
16+
To optimize and better manage your workflow involving your app's assets,
17+
there are quite a few tools available. Some of the tools are available as
1918
gems and some are available as GUI applications.
2019

21-
22-
2320
In general, these tools do the following:
2421

2522
* Compile (If they are written in, say, Sass or Coffeescript)
26-
* Concatenate
23+
* Concatenate
2724
* Compress
2825
* Cache busting
2926

30-
31-
### Compile
27+
## Compile
3228

3329
As the name suggests, this step coverts the markup scripts to plain old `css`
3430
or `js` (from `sass`, `coffeescript`)
3531

36-
### Concatenate
32+
## Concatenate
3733

38-
Multiple files may be concatenated into a single file and so, instead of
34+
Multiple files may be concatenated into a single file and so, instead of
3935
having:
4036

4137
```html
@@ -54,35 +50,34 @@ you'll end up with:
5450
<script type="text/javascript" src="application.js"></script>
5551
```
5652

57-
### Compress
53+
## Compress
5854

59-
The compress step involves minifying the Javascript files (and CSS files)
60-
by removing extra whitespaces which add to the file size and replacing
61-
long variable names with shorter ones inside function bodies. Note that
62-
it won't change the API – only internal variables will be changed. This
55+
The compress step involves minifying the Javascript files (and CSS files)
56+
by removing extra whitespaces which add to the file size and replacing
57+
long variable names with shorter ones inside function bodies. Note that
58+
it won't change the API – only internal variables will be changed. This
6359
step reduces the sizes of JS and CSS files by a large extent.
6460

65-
### Cache busting
61+
## Cache busting
6662

67-
Browser-caching can be used to ensure the files won't be downloaded from
68-
the server for every request. Many browsers check for the availability of
69-
the static files in the local cache before making a request to the
70-
server.
63+
Browser-caching can be used to ensure the files won't be downloaded from
64+
the server for every request. Many browsers check for the availability of
65+
the static files in the local cache before making a request to the
66+
server.
7167

72-
Cache busting is a procedure where the assets are numbered with a unique
73-
number that lets the browser download the latest version of the assets.
74-
For example, assume that the name of the compressed javscript file as
75-
"app.js" for a version of your production app that was pushed yesterday.
76-
If you enabled caching by sending a `Cache-Control:public`, the "app.js"
68+
Cache busting is a procedure where the assets are numbered with a unique
69+
number that lets the browser download the latest version of the assets.
70+
For example, assume that the name of the compressed javscript file as
71+
"app.js" for a version of your production app that was pushed yesterday.
72+
If you enabled caching by sending a `Cache-Control:public`, the "app.js"
7773
gets stored in the browser cache.
7874

79-
Now, if you've changed the "app.js" file today and pushed the new change
80-
to the server, the browser may still load the cached "app.js" and not
75+
Now, if you've changed the "app.js" file today and pushed the new change
76+
to the server, the browser may still load the cached "app.js" and not
8177
the new one.
8278

83-
This problem may be avoided by having a unique number added to the end
79+
This problem may be avoided by having a unique number added to the end
8480
of the filename which is changed once for every compilation-concatenation-
85-
compress step. This ensures that the browser knows the version of the file
86-
stored in the cache and the version that is in the server. If there is a
87-
difference between these two, only then the newer asset will be downloaded.
88-
81+
compress step. This ensures that the browser knows the version of the file
82+
stored in the cache and the version that is in the server. If there is a
83+
difference between these two, only then the newer asset will be downloaded.

databases/README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
Databases
2-
---------
1+
# Databases
32

43
Often times, applications need to talk to some external datasource. This
54
section will cover recipes for integrating different types of databases

0 commit comments

Comments
 (0)