Skip to content

Commit 3fa4517

Browse files
committed
Replace sinatra-assetpack with sprockets
In this change, I'm replacing Sinatra-assetpack with sprockets because assetpack is no longer maintained. Sprockets is in active development and has a very good non-Rails support. I also noticed that we are not compiling assets on Heroku. For Rails applications, the buildpack will run `rake assets:precompile`, but there isn't anything similar for rack based applications. The best option would be fork the buildpack, but because each instance of String only serves one user, I don't think it will become a problem. This change solves issue #427
1 parent 70f9ab9 commit 3fa4517

23 files changed

+98
-51
lines changed

.ruby-version

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.3.0
1+
2.3.3

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ before_script:
88
cache: bundler
99
language: ruby
1010
rvm:
11-
- 2.3.0
11+
- 2.3.3
1212
- ruby-head
1313
matrix:
1414
allow_failures:

Gemfile

+5-2
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,17 @@ gem "feedjira", "~> 2.1.0"
3333
gem "i18n"
3434
gem "loofah", "~> 2.0"
3535
gem "nokogiri", "~> 1.6", ">= 1.6.7.2"
36+
gem "rack-protection", "~> 1.5"
3637
gem "rack-ssl", "~> 1.4"
3738
gem "racksh", "~> 1.0"
3839
gem "rake", "~> 10.1", ">= 10.1.1"
40+
gem "sass"
3941
gem "sinatra", "~> 1.4.8", ">= 1.4.8"
4042
gem "sinatra-activerecord", "~> 1.2", ">= 1.2.3"
41-
gem "sinatra-assetpack", "~> 0.3", require: "sinatra/assetpack"
4243
gem "sinatra-contrib", "~> 1.4.7"
4344
gem "sinatra-flash", "~> 0.3"
45+
gem "sprockets", "~> 3.0"
46+
gem "sprockets-helpers"
4447
gem "thread", "~> 0.2"
48+
gem "uglifier"
4549
gem "will_paginate", "~> 3.1"
46-
gem "rack-protection", "~> 1.5"

Gemfile.lock

+16-9
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ GEM
3232
xpath (~> 2.0)
3333
coderay (1.1.1)
3434
columnize (0.9.0)
35+
concurrent-ruby (1.0.2)
3536
coveralls (0.8.13)
3637
json (~> 1.8)
3738
simplecov (~> 0.11.0)
@@ -46,6 +47,7 @@ GEM
4647
delayed_job (>= 3.0, < 5)
4748
diff-lcs (1.2.5)
4849
docile (1.1.5)
50+
execjs (2.7.0)
4951
faker (1.6.3)
5052
i18n (~> 0.5)
5153
faraday (0.11.0)
@@ -60,7 +62,6 @@ GEM
6062
loofah (>= 2.0)
6163
sax-machine (>= 1.0)
6264
i18n (0.7.0)
63-
jsmin (1.0.1)
6465
json (1.8.3)
6566
kgio (2.10.0)
6667
loofah (2.0.3)
@@ -122,6 +123,7 @@ GEM
122123
ruby-progressbar (~> 1.7)
123124
unicode-display_width (~> 1.0, >= 1.0.1)
124125
ruby-progressbar (1.7.5)
126+
sass (3.4.22)
125127
sax-machine (1.3.2)
126128
shotgun (0.9.1)
127129
rack (>= 1.0)
@@ -137,11 +139,6 @@ GEM
137139
sinatra-activerecord (1.7.0)
138140
activerecord (>= 3.0)
139141
sinatra (~> 1.0)
140-
sinatra-assetpack (0.3.5)
141-
jsmin
142-
rack-test
143-
sinatra
144-
tilt (>= 1.3.0, < 2.0)
145142
sinatra-contrib (1.4.7)
146143
backports (>= 2.0)
147144
multi_json
@@ -152,6 +149,11 @@ GEM
152149
sinatra-flash (0.3.0)
153150
sinatra (>= 1.0.0)
154151
slop (3.6.0)
152+
sprockets (3.7.0)
153+
concurrent-ruby (~> 1.0)
154+
rack (> 1, < 3)
155+
sprockets-helpers (1.2.1)
156+
sprockets (>= 2.2)
155157
sqlite3 (1.3.11)
156158
term-ansicolor (1.3.2)
157159
tins (~> 1.0)
@@ -163,6 +165,8 @@ GEM
163165
tins (1.6.0)
164166
tzinfo (1.2.2)
165167
thread_safe (~> 0.1)
168+
uglifier (3.0.2)
169+
execjs (>= 0.3.0, < 3)
166170
unicode-display_width (1.0.2)
167171
unicorn (4.9.0)
168172
kgio (~> 2.6)
@@ -198,20 +202,23 @@ DEPENDENCIES
198202
rspec (~> 3.4)
199203
rspec-html-matchers (~> 0.7)
200204
rubocop (~> 0.38)
205+
sass
201206
shotgun (~> 0.9)
202207
sinatra (~> 1.4.8, >= 1.4.8)
203208
sinatra-activerecord (~> 1.2, >= 1.2.3)
204-
sinatra-assetpack (~> 0.3)
205209
sinatra-contrib (~> 1.4.7)
206210
sinatra-flash (~> 0.3)
211+
sprockets (~> 3.0)
212+
sprockets-helpers
207213
sqlite3 (~> 1.3, >= 1.3.8)
208214
thread (~> 0.2)
209215
timecop (~> 0.8)
216+
uglifier
210217
unicorn (~> 4.7)
211218
will_paginate (~> 3.1)
212219

213220
RUBY VERSION
214-
ruby 2.3.0p0
221+
ruby 2.3.3p222
215222

216223
BUNDLED WITH
217-
1.14.5
224+
1.14.6

app.rb

+4-31
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22
require "sinatra/activerecord"
33
require "sinatra/flash"
44
require "sinatra/contrib/all"
5-
require "sinatra/assetpack"
65
require "rack/ssl"
76
require "json"
87
require "i18n"
98
require "will_paginate"
109
require "will_paginate/active_record"
10+
require "sprockets"
11+
require "sprockets-helpers"
1112

1213
require_relative "app/helpers/authentication_helpers"
1314
require_relative "app/repositories/user_repository"
15+
require_relative "config/asset_pipeline"
1416

1517
I18n.load_path += Dir[File.join(File.dirname(__FILE__), "config/locales", "*.yml").to_s]
1618
I18n.config.enforce_available_locales = false
@@ -22,7 +24,7 @@ class Stringer < Sinatra::Base
2224
register Sinatra::ActiveRecordExtension
2325
register Sinatra::Flash
2426
register Sinatra::Contrib
25-
register Sinatra::AssetPack
27+
register AssetPipeline
2628

2729
configure do
2830
set :database_file, "config/database.yml"
@@ -58,35 +60,6 @@ def t(*args)
5860
end
5961
end
6062

61-
assets do
62-
serve "/js", from: "app/public/js"
63-
serve "/css", from: "app/public/css"
64-
serve "/images", from: "app/public/img"
65-
66-
js :application, "/js/application.js", [
67-
"/js/jquery-min.js",
68-
"/js/bootstrap-min.js",
69-
"/js/bootstrap.file-input.js",
70-
"/js/mousetrap-min.js",
71-
"/js/jquery-visible-min.js",
72-
"/js/underscore-min.js",
73-
"/js/backbone-min.js",
74-
"/js/app.js"
75-
]
76-
77-
css :application, "/css/application.css", [
78-
"/css/bootstrap-min.css",
79-
"/css/flat-ui-no-icons.css",
80-
"/css/font-awesome-min.css",
81-
"/css/styles.css"
82-
]
83-
84-
js_compression :jsmin
85-
css_compression :simple
86-
87-
prebuild true unless ENV["RACK_ENV"] == "test"
88-
end
89-
9063
before do
9164
I18n.locale = ENV["LOCALE"].blank? ? :en : ENV["LOCALE"].to_sym
9265

app/public/js/app.js renamed to app/assets/javascripts/application.js

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
//= require jquery-min.js
2+
//= require bootstrap-min.js
3+
//= require bootstrap.file-input.js
4+
//= require mousetrap-min.js
5+
//= require jquery-visible-min.js
6+
//= require underscore-min.js
7+
//= require backbone-min.js
8+
19
_.templateSettings = {
210
interpolate: /\{\{\=(.+?)\}\}/g,
311
evaluate: /\{\{(.+?)\}\}/g
File renamed without changes.

app/public/css/styles.css renamed to app/assets/stylesheets/application.css

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/*
2+
*= require_tree .
3+
*/
4+
15
html, body {
26
height: 100%;
37
color: #484948;
File renamed without changes.

app/views/layout.erb

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@
99
<meta name="viewport" content="width=device-width, initial-scale=1" />
1010
<link rel="shortcut icon" href="/img/favicon.png">
1111
<link rel="apple-touch-icon-precomposed" href="/img/apple-touch-icon-precomposed.png">
12-
<link href="/css/lato-fonts.css" rel="stylesheet" type="text/css">
1312

1413
<%= yield_content :head %>
1514

16-
<%= css :application %>
17-
<%= js :application %>
15+
<link href="<%= stylesheet_path 'application' %>" rel="stylesheet" />
16+
<script src="<%= javascript_path 'application' %>"></script>
1817
</head>
1918
<body>
2019
<div id="wrap">

config/asset_pipeline.rb

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
module AssetPipeline
2+
def registered(app)
3+
app.set :sprockets, Sprockets::Environment.new(app.root)
4+
5+
app.get "/assets/*" do
6+
env["PATH_INFO"].sub!(%r{^/assets}, "")
7+
settings.sprockets.call(env)
8+
end
9+
10+
append_paths(app)
11+
configure_development(app)
12+
configure_production(app)
13+
register_helpers(app)
14+
end
15+
16+
private
17+
18+
def append_paths(app)
19+
app.sprockets.append_path File.join(app.root, "app", "assets")
20+
app.sprockets.append_path File.join(app.root, "app", "assets", "stylesheets")
21+
app.sprockets.append_path File.join(app.root, "app", "assets", "javascripts")
22+
end
23+
24+
def configure_development(app)
25+
app.configure :development do
26+
app.sprockets.cache = Sprockets::Cache::FileStore.new("./tmp")
27+
end
28+
end
29+
30+
def configure_production(app)
31+
app.configure :production do
32+
app.sprockets.css_compressor = :scss
33+
app.sprockets.js_compressor = :uglify
34+
end
35+
end
36+
37+
def register_helpers(app)
38+
Sprockets::Helpers.configure do |config|
39+
config.environment = app.sprockets
40+
config.prefix = "/assets"
41+
config.debug = true if app.development?
42+
config.digest = true if app.production?
43+
end
44+
45+
app.helpers Sprockets::Helpers
46+
end
47+
48+
module_function :registered,
49+
:append_paths,
50+
:configure_development,
51+
:configure_production,
52+
:register_helpers
53+
end

docs/VPS.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ We are going to use Rbenv to manage the version of Ruby you use.
6161
git clone git://github.com/sstephenson/ruby-build.git $HOME/.rbenv/plugins/ruby-build
6262
source ~/.bash_profile
6363

64-
rbenv install 2.3.0
65-
rbenv local 2.3.0
64+
rbenv install 2.3.3
65+
rbenv local 2.3.3
6666
rbenv rehash
6767

6868
We also need to install bundler which will handle Stringer's dependencies

spec/javascript/support/views/index.erb

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
<head>
44
<title>Stringer JavaScript Test Suite</title>
55
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" >
6-
<%= js :application %>
6+
<script src="<%= javascript_path 'application' %>"></script>
77

88
<% js_files.each do |js_file| %>
99
<script src="<%= js_file %>"></script>
1010
<% end %>
1111
<% css_files.each do |css_file| %>
12-
<link rel="stylesheet" type="text/css" href="<%= css_file%>">
12+
<link rel="stylesheet" type="text/css" href="<%= css_file %>">
1313
<% end %>
1414

1515
<!-- Javascript templates -->

0 commit comments

Comments
 (0)