sprockets-helpers 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/LICENSE +20 -0
- data/README.md +144 -0
- data/Rakefile +6 -0
- data/lib/sprockets-helpers.rb +1 -0
- data/lib/sprockets/helpers.rb +154 -0
- data/lib/sprockets/helpers/asset_path.rb +31 -0
- data/lib/sprockets/helpers/file_path.rb +67 -0
- data/lib/sprockets/helpers/version.rb +5 -0
- data/spec/spec_helper.rb +26 -0
- data/spec/sprockets-helpers_spec.rb +122 -0
- data/sprockets-helpers.gemspec +25 -0
- metadata +137 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Peter Browne
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,144 @@
|
|
1
|
+
sprockets-helpers
|
2
|
+
=================
|
3
|
+
|
4
|
+
**Asset path helpers for Sprockets 2.0 applications**
|
5
|
+
|
6
|
+
Sprockets::Helpers adds the asset_path helpers, familiar to Rails developers, to Sprockets 2.0 assets and application.
|
7
|
+
|
8
|
+
### Features
|
9
|
+
|
10
|
+
* Includes image_path, javascript_path, & stylesheet_path helpers.
|
11
|
+
* Automatically appends extension if necessary.
|
12
|
+
* Optionally outputs digest paths.
|
13
|
+
* Falls back to file paths in the public directory.
|
14
|
+
|
15
|
+
|
16
|
+
Installation
|
17
|
+
------------
|
18
|
+
|
19
|
+
``` bash
|
20
|
+
$ gem install sprockets-helpers
|
21
|
+
```
|
22
|
+
|
23
|
+
|
24
|
+
Setup
|
25
|
+
-----
|
26
|
+
|
27
|
+
Let's build a simple Sinatra app using Sprockets and Sprockets::Helpers (See [sinatra-asset-pipeline](https://github.com/stevehodgkiss/sinatra-asset-pipeline) for complete setup):
|
28
|
+
|
29
|
+
``` ruby
|
30
|
+
require "sinatra/base"
|
31
|
+
require "sprockets"
|
32
|
+
require "sprockets-helpers"
|
33
|
+
|
34
|
+
class App < Sinatra::Base
|
35
|
+
set :sprockets, Sprockets::Environment.new(root)
|
36
|
+
set :assets_prefix, "/assets"
|
37
|
+
set :digest_assets, false
|
38
|
+
|
39
|
+
configure do
|
40
|
+
# Setup Sprockets
|
41
|
+
sprockets.append_path File.join(root, "assets", "stylesheets")
|
42
|
+
sprockets.append_path File.join(root, "assets", "javascripts")
|
43
|
+
sprockets.append_path File.join(root, "assets", "images")
|
44
|
+
|
45
|
+
# Configure Sprockets::Helpers (if necessary)
|
46
|
+
Sprockets::Helpers.prefix = assets_prefix
|
47
|
+
Sprockets::Helpers.digest = digest_assets
|
48
|
+
Sprockets::Helpers.public_path = public_folder
|
49
|
+
end
|
50
|
+
|
51
|
+
helpers do
|
52
|
+
include Sprockets::Helpers
|
53
|
+
|
54
|
+
# required for Sprockets::Helpers to work!
|
55
|
+
def environment
|
56
|
+
settings.sprockets
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
get "/" do
|
61
|
+
erb :index
|
62
|
+
end
|
63
|
+
end
|
64
|
+
```
|
65
|
+
|
66
|
+
|
67
|
+
Usage in Assets
|
68
|
+
---------------
|
69
|
+
|
70
|
+
Simply requiring sprockets-helpers will add the asset path helpers to the Sprocket context, making them available within any asset. For example, a file `assets/javascripts/paths.js.erb`:
|
71
|
+
|
72
|
+
``` erb
|
73
|
+
var Paths = { railsImage: "<%= image_path "rails.png %>" };
|
74
|
+
```
|
75
|
+
|
76
|
+
Would be transformed into:
|
77
|
+
|
78
|
+
``` javascript
|
79
|
+
var Paths = { railsImage: "/assets/rails.png" };
|
80
|
+
```
|
81
|
+
|
82
|
+
|
83
|
+
Usage in the App
|
84
|
+
----------------
|
85
|
+
|
86
|
+
The helpers can also be used in the app itself. You just include the `Sprockets::Helpers` module and add an `#environment` method which returns a reference to the Sprockets environment (see the Sinatra app setup above).
|
87
|
+
|
88
|
+
Now the following index file:
|
89
|
+
|
90
|
+
``` erb
|
91
|
+
<!doctype html>
|
92
|
+
<html lang="en">
|
93
|
+
<head>
|
94
|
+
<meta charset="utf-8">
|
95
|
+
<title>Sinatra with Sprockets 2 (Asset Pipeline)</title>
|
96
|
+
<link rel="stylesheet" href="<%= stylesheet_path "application" %>">
|
97
|
+
<script src="<%= javascript_path "application" %>"></script>
|
98
|
+
</head>
|
99
|
+
<body>
|
100
|
+
<img src="<%= image_path "rails.png" %>">
|
101
|
+
</body>
|
102
|
+
</html>
|
103
|
+
```
|
104
|
+
|
105
|
+
Would become:
|
106
|
+
|
107
|
+
``` html
|
108
|
+
<!doctype html>
|
109
|
+
<html lang="en">
|
110
|
+
<head>
|
111
|
+
<meta charset="utf-8">
|
112
|
+
<title>Sinatra with Sprockets 2 (Asset Pipeline)</title>
|
113
|
+
<link rel="stylesheet" href="/assets/application.css">
|
114
|
+
<script src="/assets/application.js"></script>
|
115
|
+
</head>
|
116
|
+
<body>
|
117
|
+
<img src="/assets/rails.png">
|
118
|
+
</body>
|
119
|
+
</html>
|
120
|
+
```
|
121
|
+
|
122
|
+
|
123
|
+
Fallback to Public Directory
|
124
|
+
----------------------------
|
125
|
+
|
126
|
+
If the source is not an asset in the Sprockets environment, Sprockets::Helpers will fallback to looking for the file in the application's public directory. It will also append the cache busting timestamp of the file. For example:
|
127
|
+
|
128
|
+
Given an image, `public/images/logo.jpg`:
|
129
|
+
|
130
|
+
``` erb
|
131
|
+
<img src="<%= image_path "logo.jpg" %>">
|
132
|
+
```
|
133
|
+
|
134
|
+
Would become:
|
135
|
+
|
136
|
+
``` html
|
137
|
+
<img src="/images/logo.jpg?1320093919">
|
138
|
+
```
|
139
|
+
|
140
|
+
|
141
|
+
Copyright
|
142
|
+
---------
|
143
|
+
|
144
|
+
Copyright (c) 2011 [Peter Browne](http://petebrowne.com). See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "sprockets/helpers"
|
@@ -0,0 +1,154 @@
|
|
1
|
+
require "sprockets/helpers/version"
|
2
|
+
require "sprockets"
|
3
|
+
|
4
|
+
module Sprockets
|
5
|
+
module Helpers
|
6
|
+
autoload :AssetPath, "sprockets/helpers/asset_path"
|
7
|
+
autoload :FilePath, "sprockets/helpers/file_path"
|
8
|
+
|
9
|
+
# Pattern for checking if a given path is an external URI.
|
10
|
+
URI_MATCH = %r(^[-a-z]+://|^cid:|^//)
|
11
|
+
|
12
|
+
class << self
|
13
|
+
# When true, the asset paths will return digest paths.
|
14
|
+
attr_accessor :digest
|
15
|
+
|
16
|
+
# The base URL the Sprocket environment is mapped to.
|
17
|
+
# This defaults to "/assets".
|
18
|
+
def prefix
|
19
|
+
@prefix ||= "/assets"
|
20
|
+
end
|
21
|
+
attr_writer :prefix
|
22
|
+
|
23
|
+
# The path to the public directory, where the assets
|
24
|
+
# not managed by Sprockets will be located.
|
25
|
+
# Defaults to "./public"
|
26
|
+
def public_path
|
27
|
+
@public_path ||= "./public"
|
28
|
+
end
|
29
|
+
attr_writer :public_path
|
30
|
+
end
|
31
|
+
|
32
|
+
# Returns the path to an asset either in the Sprockets environment
|
33
|
+
# or the public directory. External URIs are untouched.
|
34
|
+
#
|
35
|
+
# ==== Options
|
36
|
+
#
|
37
|
+
# * <tt>:ext</tt> - The extension to append if the source does not have one.
|
38
|
+
# * <tt>:dir</tt> - The directory to prepend if the file is in the public directory.
|
39
|
+
# * <tt>:digest</tt> - Wether or not use the digest paths for assets. Set Sprockets::Helpers.digest for global configuration.
|
40
|
+
# * <tt>:prefix</tt> - Use a custom prefix for the Sprockets environment. Set Sprockets::Helpers.prefix for global configuration.
|
41
|
+
#
|
42
|
+
#
|
43
|
+
# ==== Examples
|
44
|
+
#
|
45
|
+
# For files within Sprockets:
|
46
|
+
#
|
47
|
+
# asset_path "xmlhr.js" # => "/assets/xmlhr.js"
|
48
|
+
# asset_path "xmlhr", :ext => "js" # => "/assets/xmlhr.js"
|
49
|
+
# asset_path "xmlhr.js", :digest => true # => "/assets/xmlhr-27a8f1f96afd8d4c67a59eb9447f45bd.js"
|
50
|
+
# asset_path "xmlhr.js", :prefix => "/themes" # => "/themes/xmlhr.js"
|
51
|
+
#
|
52
|
+
# For files outside of Sprockets:
|
53
|
+
#
|
54
|
+
# asset_path "xmlhr" # => "/xmlhr"
|
55
|
+
# asset_path "xmlhr", :ext => "js" # => "/xmlhr.js"
|
56
|
+
# asset_path "dir/xmlhr.js", :dir => "javascripts" # => "/javascripts/dir/xmlhr.js"
|
57
|
+
# asset_path "/dir/xmlhr.js", :dir => "javascripts" # => "/dir/xmlhr.js"
|
58
|
+
# asset_path "http://www.example.com/js/xmlhr" # => "http://www.example.com/js/xmlhr"
|
59
|
+
# asset_path "http://www.example.com/js/xmlhr.js" # => "http://www.example.com/js/xmlhr.js"
|
60
|
+
#
|
61
|
+
def asset_path(source, options = {})
|
62
|
+
return source if source =~ URI_MATCH
|
63
|
+
|
64
|
+
# Append extension if necessary
|
65
|
+
if options[:ext] && File.extname(source).empty?
|
66
|
+
source << ".#{options[:ext]}"
|
67
|
+
end
|
68
|
+
|
69
|
+
# If the source points to an asset in the Sprockets
|
70
|
+
# environment use AssetPath to generate the full path.
|
71
|
+
environment.resolve(source) do |path|
|
72
|
+
return AssetPath.new(environment.find_asset(path), options).to_s
|
73
|
+
end
|
74
|
+
|
75
|
+
# Use FilePath for normal files on the file system
|
76
|
+
FilePath.new(source, options).to_s
|
77
|
+
end
|
78
|
+
|
79
|
+
# Computes the path to a javascript asset either in the Sprockets
|
80
|
+
# environment or the public directory. If the +source+ filename has no extension,
|
81
|
+
# <tt>.js</tt> will be appended. External URIs are untouched.
|
82
|
+
#
|
83
|
+
# ==== Examples
|
84
|
+
#
|
85
|
+
# For files within Sprockets:
|
86
|
+
#
|
87
|
+
# javascript_path "xmlhr" # => "/assets/xmlhr.js"
|
88
|
+
# javascript_path "dir/xmlhr.js" # => "/assets/dir/xmlhr.js"
|
89
|
+
# javascript_path "/dir/xmlhr" # => "/assets/dir/xmlhr.js"
|
90
|
+
#
|
91
|
+
# For files outside of Sprockets:
|
92
|
+
#
|
93
|
+
# javascript_path "xmlhr" # => "/javascripts/xmlhr.js"
|
94
|
+
# javascript_path "dir/xmlhr.js" # => "/javascripts/dir/xmlhr.js"
|
95
|
+
# javascript_path "/dir/xmlhr" # => "/dir/xmlhr.js"
|
96
|
+
# javascript_path "http://www.example.com/js/xmlhr" # => "http://www.example.com/js/xmlhr"
|
97
|
+
# javascript_path "http://www.example.com/js/xmlhr.js" # => "http://www.example.com/js/xmlhr.js"
|
98
|
+
#
|
99
|
+
def javascript_path(source, options = {})
|
100
|
+
asset_path source, { :dir => "javascripts", :ext => "js" }.merge(options)
|
101
|
+
end
|
102
|
+
|
103
|
+
# Computes the path to a stylesheet asset either in the Sprockets
|
104
|
+
# environment or the public directory. If the +source+ filename has no extension,
|
105
|
+
# <tt>.css</tt> will be appended. External URIs are untouched.
|
106
|
+
#
|
107
|
+
# ==== Examples
|
108
|
+
#
|
109
|
+
# For files within Sprockets:
|
110
|
+
#
|
111
|
+
# stylesheet_path "style" # => "/assets/style.css"
|
112
|
+
# stylesheet_path "dir/style.css" # => "/assets/dir/style.css"
|
113
|
+
# stylesheet_path "/dir/style.css" # => "/assets/dir/style.css"
|
114
|
+
#
|
115
|
+
# For files outside of Sprockets:
|
116
|
+
#
|
117
|
+
# stylesheet_path "style" # => "/stylesheets/style.css"
|
118
|
+
# stylesheet_path "dir/style.css" # => "/stylesheets/dir/style.css"
|
119
|
+
# stylesheet_path "/dir/style.css" # => "/dir/style.css"
|
120
|
+
# stylesheet_path "http://www.example.com/css/style" # => "http://www.example.com/css/style"
|
121
|
+
# stylesheet_path "http://www.example.com/css/style.css" # => "http://www.example.com/css/style.css"
|
122
|
+
#
|
123
|
+
def stylesheet_path(source, options = {})
|
124
|
+
asset_path source, { :dir => "stylesheets", :ext => "css" }.merge(options)
|
125
|
+
end
|
126
|
+
|
127
|
+
# Computes the path to an image asset either in the Sprockets environment
|
128
|
+
# or the public directory. External URIs are untouched.
|
129
|
+
#
|
130
|
+
# ==== Examples
|
131
|
+
#
|
132
|
+
# With files within Sprockets:
|
133
|
+
#
|
134
|
+
# image_path "edit.png" # => "/assets/edit.png"
|
135
|
+
# image_path "icons/edit.png" # => "/assets/icons/edit.png"
|
136
|
+
# image_path "/icons/edit.png" # => "/assets/icons/edit.png"
|
137
|
+
#
|
138
|
+
# With files outside of Sprockets:
|
139
|
+
#
|
140
|
+
# image_path "edit" # => "/images/edit"
|
141
|
+
# image_path "edit.png" # => "/images/edit.png"
|
142
|
+
# image_path "icons/edit.png" # => "/images/icons/edit.png"
|
143
|
+
# image_path "/icons/edit.png" # => "/icons/edit.png"
|
144
|
+
# image_path "http://www.example.com/img/edit.png" # => "http://www.example.com/img/edit.png"
|
145
|
+
#
|
146
|
+
def image_path(source, options = {})
|
147
|
+
asset_path source, { :dir => "images" }.merge(options)
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
class Context
|
152
|
+
include Helpers
|
153
|
+
end
|
154
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Sprockets
|
2
|
+
module Helpers
|
3
|
+
# `AssetPath` generates a full path for an asset
|
4
|
+
# that exists in Sprockets environment.
|
5
|
+
class AssetPath < FilePath
|
6
|
+
#
|
7
|
+
def initialize(asset, options = {})
|
8
|
+
@options = {
|
9
|
+
:body => false,
|
10
|
+
:digest => Helpers.digest,
|
11
|
+
:prefix => Helpers.prefix
|
12
|
+
}.merge options
|
13
|
+
|
14
|
+
@source = @options[:digest] ? asset.digest_path : asset.logical_path
|
15
|
+
end
|
16
|
+
|
17
|
+
protected
|
18
|
+
|
19
|
+
# Prepends the base path if the path is not
|
20
|
+
# already an absolute path.
|
21
|
+
def rewrite_base_path(path) # :nodoc:
|
22
|
+
File.join @options[:prefix], path
|
23
|
+
end
|
24
|
+
|
25
|
+
# Rewrite the query string to inlcude body flag if necessary.
|
26
|
+
def rewrite_query(path)
|
27
|
+
options[:body] ? "#{path}?body=1" : path
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
module Sprockets
|
2
|
+
module Helpers
|
3
|
+
# `FilePath` generates a full path for a regular file
|
4
|
+
# in the output path. It's used by #asset_path to generate
|
5
|
+
# paths when using asset tags like #javascript_include_tag,
|
6
|
+
# #stylesheet_link_tag, and #image_tag
|
7
|
+
class FilePath
|
8
|
+
# The path from which to generate the full path to the asset.
|
9
|
+
attr_reader :source
|
10
|
+
|
11
|
+
# The various options used when generating the path.
|
12
|
+
attr_reader :options
|
13
|
+
|
14
|
+
# The base directory the file would be found in
|
15
|
+
attr_reader :dir
|
16
|
+
|
17
|
+
#
|
18
|
+
def initialize(source, options = {})
|
19
|
+
@source = source.to_s
|
20
|
+
@options = options
|
21
|
+
@dir = options[:dir].to_s
|
22
|
+
end
|
23
|
+
|
24
|
+
# Returns the full path to the asset, complete with
|
25
|
+
# timestamp.
|
26
|
+
def to_s
|
27
|
+
path = rewrite_base_path(source)
|
28
|
+
path = rewrite_query(path)
|
29
|
+
path
|
30
|
+
end
|
31
|
+
|
32
|
+
protected
|
33
|
+
|
34
|
+
# Prepends the base path if the path is not
|
35
|
+
# already an absolute path.
|
36
|
+
def rewrite_base_path(path) # :nodoc:
|
37
|
+
if path !~ %r(^/)
|
38
|
+
File.join("/", dir, path)
|
39
|
+
else
|
40
|
+
path
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# Appends an asset timestamp based on the
|
45
|
+
# modification time of the asset.
|
46
|
+
def rewrite_query(path) # :nodoc:
|
47
|
+
if timestamp = mtime(path)
|
48
|
+
"#{path}?#{timestamp.to_i}"
|
49
|
+
else
|
50
|
+
path
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
# Returns the mtime for the given path (relative to
|
55
|
+
# the output path). Returns nil if the file doesn't exist.
|
56
|
+
def mtime(path) # :nodoc:
|
57
|
+
public_path = File.join(Helpers.public_path, path)
|
58
|
+
|
59
|
+
if File.exist?(public_path)
|
60
|
+
File.mtime(public_path)
|
61
|
+
else
|
62
|
+
nil
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require "sprockets"
|
2
|
+
require "sprockets-helpers"
|
3
|
+
require "construct"
|
4
|
+
require "pathname"
|
5
|
+
|
6
|
+
# Requires supporting files with custom matchers and macros, etc,
|
7
|
+
# in ./support/ and its subdirectories.
|
8
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
config.include Construct::Helpers
|
12
|
+
|
13
|
+
# Returns a Sprockets environment. Automatically
|
14
|
+
# appends the "assets" path if available.
|
15
|
+
def env
|
16
|
+
@env ||= Sprockets::Environment.new.tap do |env|
|
17
|
+
env.append_path "assets" if File.directory?("assets")
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
# Returns a fresh context, that can be used to test helpers.
|
22
|
+
def context(logical_path = "application.js", pathname = nil)
|
23
|
+
pathname ||= Pathname.new(File.join("assets", logical_path)).expand_path
|
24
|
+
env.context_class.new env, logical_path, pathname
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,122 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Sprockets::Helpers do
|
4
|
+
describe "#asset_path" do
|
5
|
+
context "with URIs" do
|
6
|
+
it "returns URIs untouched" do
|
7
|
+
context.asset_path("https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js").should ==
|
8
|
+
"https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"
|
9
|
+
context.asset_path("http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js").should ==
|
10
|
+
"http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"
|
11
|
+
context.asset_path("//ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js").should ==
|
12
|
+
"//ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
context "with regular files" do
|
17
|
+
it "returns absolute paths" do
|
18
|
+
context.asset_path("/path/to/file.js").should == "/path/to/file.js"
|
19
|
+
context.asset_path("/path/to/file.jpg").should == "/path/to/file.jpg"
|
20
|
+
end
|
21
|
+
|
22
|
+
it "appends the extension for javascripts and stylesheets" do
|
23
|
+
context.asset_path("/path/to/file", :ext => "js").should == "/path/to/file.js"
|
24
|
+
context.asset_path("/path/to/file", :ext => "css").should == "/path/to/file.css"
|
25
|
+
end
|
26
|
+
|
27
|
+
it "prepends a base dir" do
|
28
|
+
context.asset_path("main", :dir => "stylesheets", :ext => "css").should == "/stylesheets/main.css"
|
29
|
+
context.asset_path("main", :dir => "javascripts", :ext => "js").should == "/javascripts/main.js"
|
30
|
+
context.asset_path("logo.jpg", :dir => "images").should == "/images/logo.jpg"
|
31
|
+
end
|
32
|
+
|
33
|
+
it "appends a timestamp if the file exists in the output path" do
|
34
|
+
within_construct do |c|
|
35
|
+
file1 = c.file "public/main.js"
|
36
|
+
file2 = c.file "public/favicon.ico"
|
37
|
+
|
38
|
+
context.asset_path("main", :ext => "js").should =~ %r(/main.js\?\d+)
|
39
|
+
context.asset_path("/favicon.ico").should =~ %r(/favicon.ico\?\d+)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
context "with assets" do
|
45
|
+
it "returns URLs to the assets" do
|
46
|
+
within_construct do |c|
|
47
|
+
c.file "assets/logo.jpg"
|
48
|
+
c.file "assets/main.js"
|
49
|
+
c.file "assets/main.css"
|
50
|
+
|
51
|
+
context.asset_path("main", :ext => "css").should == "/assets/main.css"
|
52
|
+
context.asset_path("main", :ext => "js").should == "/assets/main.js"
|
53
|
+
context.asset_path("logo.jpg").should == "/assets/logo.jpg"
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
it "prepends the assets prefix" do
|
58
|
+
within_construct do |c|
|
59
|
+
c.file "assets/logo.jpg"
|
60
|
+
|
61
|
+
context.asset_path("logo.jpg").should == "/assets/logo.jpg"
|
62
|
+
context.asset_path("logo.jpg", :prefix => "/images").should == "/images/logo.jpg"
|
63
|
+
Sprockets::Helpers.prefix = "/images"
|
64
|
+
context.asset_path("logo.jpg").should == "/images/logo.jpg"
|
65
|
+
Sprockets::Helpers.prefix = "/assets"
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
it "uses the digest path if configured" do
|
70
|
+
within_construct do |c|
|
71
|
+
c.file "assets/main.js"
|
72
|
+
|
73
|
+
context.asset_path("main", :ext => "js").should == "/assets/main.js"
|
74
|
+
context.asset_path("main", :ext => "js", :digest => true).should =~ %r(/assets/main-[0-9a-f]+.js)
|
75
|
+
Sprockets::Helpers.digest = true
|
76
|
+
context.asset_path("main", :ext => "js").should =~ %r(/assets/main-[0-9a-f]+.js)
|
77
|
+
Sprockets::Helpers.digest = false
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
it "returns a body parameter" do
|
82
|
+
within_construct do |c|
|
83
|
+
c.file "assets/main.js"
|
84
|
+
|
85
|
+
context.asset_path("main", :ext => "js", :body => true).should == "/assets/main.js?body=1"
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
describe "#javascript_path" do
|
92
|
+
context "with regular files" do
|
93
|
+
it "appends the js extension" do
|
94
|
+
context.javascript_path("/path/to/file").should == "/path/to/file.js"
|
95
|
+
end
|
96
|
+
|
97
|
+
it "prepends the javascripts dir" do
|
98
|
+
context.javascript_path("main").should == "/javascripts/main.js"
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
describe "#stylesheet_path" do
|
104
|
+
context "with regular files" do
|
105
|
+
it "appends the css extension" do
|
106
|
+
context.stylesheet_path("/path/to/file").should == "/path/to/file.css"
|
107
|
+
end
|
108
|
+
|
109
|
+
it "prepends the stylesheets dir" do
|
110
|
+
context.stylesheet_path("main").should == "/stylesheets/main.css"
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
describe "#image_path" do
|
116
|
+
context "with regular files" do
|
117
|
+
it "prepends the images dir" do
|
118
|
+
context.image_path("logo.jpg").should == "/images/logo.jpg"
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "sprockets/helpers/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "sprockets-helpers"
|
7
|
+
s.version = Sprockets::Helpers::VERSION
|
8
|
+
s.authors = ["Pete Browne"]
|
9
|
+
s.email = ["me@petebrowne.com"]
|
10
|
+
s.homepage = "https://github.com/petebrowne/sprockets-sass"
|
11
|
+
s.summary = "Asset path helpers for Sprockets 2.0 applications"
|
12
|
+
s.description = "Asset path helpers for Sprockets 2.0 applications"
|
13
|
+
|
14
|
+
s.rubyforge_project = "sprockets-helpers"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.add_dependency "sprockets", "~> 2.0"
|
22
|
+
s.add_development_dependency "rspec", "~> 2.6"
|
23
|
+
s.add_development_dependency "test-construct", "~> 1.2"
|
24
|
+
s.add_development_dependency "rake"
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,137 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sprockets-helpers
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Pete Browne
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-10-31 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
22
|
+
none: false
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
hash: 3
|
27
|
+
segments:
|
28
|
+
- 2
|
29
|
+
- 0
|
30
|
+
version: "2.0"
|
31
|
+
name: sprockets
|
32
|
+
version_requirements: *id001
|
33
|
+
prerelease: false
|
34
|
+
type: :runtime
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ~>
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
hash: 15
|
42
|
+
segments:
|
43
|
+
- 2
|
44
|
+
- 6
|
45
|
+
version: "2.6"
|
46
|
+
name: rspec
|
47
|
+
version_requirements: *id002
|
48
|
+
prerelease: false
|
49
|
+
type: :development
|
50
|
+
- !ruby/object:Gem::Dependency
|
51
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ~>
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
hash: 11
|
57
|
+
segments:
|
58
|
+
- 1
|
59
|
+
- 2
|
60
|
+
version: "1.2"
|
61
|
+
name: test-construct
|
62
|
+
version_requirements: *id003
|
63
|
+
prerelease: false
|
64
|
+
type: :development
|
65
|
+
- !ruby/object:Gem::Dependency
|
66
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
67
|
+
none: false
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
hash: 3
|
72
|
+
segments:
|
73
|
+
- 0
|
74
|
+
version: "0"
|
75
|
+
name: rake
|
76
|
+
version_requirements: *id004
|
77
|
+
prerelease: false
|
78
|
+
type: :development
|
79
|
+
description: Asset path helpers for Sprockets 2.0 applications
|
80
|
+
email:
|
81
|
+
- me@petebrowne.com
|
82
|
+
executables: []
|
83
|
+
|
84
|
+
extensions: []
|
85
|
+
|
86
|
+
extra_rdoc_files: []
|
87
|
+
|
88
|
+
files:
|
89
|
+
- .gitignore
|
90
|
+
- Gemfile
|
91
|
+
- LICENSE
|
92
|
+
- README.md
|
93
|
+
- Rakefile
|
94
|
+
- lib/sprockets-helpers.rb
|
95
|
+
- lib/sprockets/helpers.rb
|
96
|
+
- lib/sprockets/helpers/asset_path.rb
|
97
|
+
- lib/sprockets/helpers/file_path.rb
|
98
|
+
- lib/sprockets/helpers/version.rb
|
99
|
+
- spec/spec_helper.rb
|
100
|
+
- spec/sprockets-helpers_spec.rb
|
101
|
+
- sprockets-helpers.gemspec
|
102
|
+
homepage: https://github.com/petebrowne/sprockets-sass
|
103
|
+
licenses: []
|
104
|
+
|
105
|
+
post_install_message:
|
106
|
+
rdoc_options: []
|
107
|
+
|
108
|
+
require_paths:
|
109
|
+
- lib
|
110
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
111
|
+
none: false
|
112
|
+
requirements:
|
113
|
+
- - ">="
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
hash: 3
|
116
|
+
segments:
|
117
|
+
- 0
|
118
|
+
version: "0"
|
119
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
120
|
+
none: false
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
hash: 3
|
125
|
+
segments:
|
126
|
+
- 0
|
127
|
+
version: "0"
|
128
|
+
requirements: []
|
129
|
+
|
130
|
+
rubyforge_project: sprockets-helpers
|
131
|
+
rubygems_version: 1.8.5
|
132
|
+
signing_key:
|
133
|
+
specification_version: 3
|
134
|
+
summary: Asset path helpers for Sprockets 2.0 applications
|
135
|
+
test_files:
|
136
|
+
- spec/spec_helper.rb
|
137
|
+
- spec/sprockets-helpers_spec.rb
|