sprockets-helpers 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -43,18 +43,22 @@ class App < Sinatra::Base
43
43
  sprockets.append_path File.join(root, "assets", "images")
44
44
 
45
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
46
+ Sprockets::Helpers.configure do |config|
47
+ config.environment = sprockets
48
+ config.prefix = assets_prefix
49
+ config.digest = digest_assets
50
+ config.public_path = public_folder
51
+ end
49
52
  end
50
53
 
51
54
  helpers do
52
55
  include Sprockets::Helpers
53
56
 
54
- # required for Sprockets::Helpers to work!
55
- def environment
56
- settings.sprockets
57
- end
57
+ # Alternative method for telling Sprockets::Helpers which
58
+ # Sprockets environment to use.
59
+ # def assets_environment
60
+ # settings.sprockets
61
+ # end
58
62
  end
59
63
 
60
64
  get "/" do
@@ -83,7 +87,7 @@ var Paths = { railsImage: "/assets/rails.png" };
83
87
  Usage in the App
84
88
  ----------------
85
89
 
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).
90
+ The helpers can also be used in the app itself. You just include the `Sprockets::Helpers` module and set Sprockets::Helpers.environment to the Sprockets environment to search for the assets. Alternatively you can define an #assets_environment method in the context of #asset_path, which returns a reference to the Sprockets environment (see above).
87
91
 
88
92
  Now the following index file:
89
93
 
@@ -13,6 +13,10 @@ module Sprockets
13
13
  # When true, the asset paths will return digest paths.
14
14
  attr_accessor :digest
15
15
 
16
+ # Set the Sprockets environment to search for assets.
17
+ # This defaults to the context's #environment method.
18
+ attr_accessor :environment
19
+
16
20
  # The base URL the Sprocket environment is mapped to.
17
21
  # This defaults to "/assets".
18
22
  def prefix
@@ -27,6 +31,11 @@ module Sprockets
27
31
  @public_path ||= "./public"
28
32
  end
29
33
  attr_writer :public_path
34
+
35
+ # Convience method for configuring Sprockets::Helpers.
36
+ def configure
37
+ yield self
38
+ end
30
39
  end
31
40
 
32
41
  # Returns the path to an asset either in the Sprockets environment
@@ -38,7 +47,7 @@ module Sprockets
38
47
  # * <tt>:dir</tt> - The directory to prepend if the file is in the public directory.
39
48
  # * <tt>:digest</tt> - Wether or not use the digest paths for assets. Set Sprockets::Helpers.digest for global configuration.
40
49
  # * <tt>:prefix</tt> - Use a custom prefix for the Sprockets environment. Set Sprockets::Helpers.prefix for global configuration.
41
- #
50
+ # * <tt>:body</tt> - Adds a ?body=1 flag that tells Sprockets to return only the body of the asset.
42
51
  #
43
52
  # ==== Examples
44
53
  #
@@ -68,8 +77,8 @@ module Sprockets
68
77
 
69
78
  # If the source points to an asset in the Sprockets
70
79
  # 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
80
+ assets_environment.resolve(source) do |path|
81
+ return AssetPath.new(assets_environment[path], options).to_s
73
82
  end
74
83
 
75
84
  # Use FilePath for normal files on the file system
@@ -146,6 +155,16 @@ module Sprockets
146
155
  def image_path(source, options = {})
147
156
  asset_path source, { :dir => "images" }.merge(options)
148
157
  end
158
+
159
+ protected
160
+
161
+ # Returns the Sprockets environment #asset_path uses to search for
162
+ # assets. This can be overridden for more control, if necessary.
163
+ # Defaults to Sprockets::Helpers.environment or the envrionment
164
+ # returned by #environment.
165
+ def assets_environment
166
+ Helpers.environment || environment
167
+ end
149
168
  end
150
169
 
151
170
  class Context
@@ -1,5 +1,5 @@
1
1
  module Sprockets
2
2
  module Helpers
3
- VERSION = "0.1.0"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
@@ -1,6 +1,76 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe Sprockets::Helpers do
4
+ describe ".configure" do
5
+ it "sets global configuration" do
6
+ within_construct do |c|
7
+ c.file "assets/main.css"
8
+
9
+ context.asset_path("main.css").should == "/assets/main.css"
10
+ Sprockets::Helpers.configure do |config|
11
+ config.digest = true
12
+ config.prefix = "/themes"
13
+ end
14
+ context.asset_path("main.css").should =~ %r(/themes/main-[0-9a-f]+.css)
15
+ Sprockets::Helpers.digest = nil
16
+ Sprockets::Helpers.prefix = nil
17
+ end
18
+ end
19
+ end
20
+
21
+ describe ".digest" do
22
+ it "globally configures digest paths" do
23
+ within_construct do |c|
24
+ c.file "assets/main.js"
25
+
26
+ context.asset_path("main", :ext => "js").should == "/assets/main.js"
27
+ Sprockets::Helpers.digest = true
28
+ context.asset_path("main", :ext => "js").should =~ %r(/assets/main-[0-9a-f]+.js)
29
+ Sprockets::Helpers.digest = nil
30
+ end
31
+ end
32
+ end
33
+
34
+ describe ".environment" do
35
+ it "sets a custom assets environment" do
36
+ within_construct do |c|
37
+ c.file "themes/main.css"
38
+
39
+ custom_env = Sprockets::Environment.new
40
+ custom_env.append_path "themes"
41
+ Sprockets::Helpers.environment = custom_env
42
+ context.asset_path("main.css").should == "/assets/main.css"
43
+ Sprockets::Helpers.environment = nil
44
+ end
45
+ end
46
+ end
47
+
48
+ describe ".prefix" do
49
+ it "sets a custom assets prefix" do
50
+ within_construct do |c|
51
+ c.file "assets/logo.jpg"
52
+
53
+ context.asset_path("logo.jpg").should == "/assets/logo.jpg"
54
+ Sprockets::Helpers.prefix = "/images"
55
+ context.asset_path("logo.jpg").should == "/images/logo.jpg"
56
+ Sprockets::Helpers.prefix = nil
57
+ end
58
+ end
59
+ end
60
+
61
+ describe ".public_path" do
62
+ it "sets a custom location for the public path" do
63
+ within_construct do |c|
64
+ c.file "output/main.js"
65
+
66
+ context.asset_path("main.js").should == "/main.js"
67
+ Sprockets::Helpers.public_path = "./output"
68
+ context.asset_path("main.js").should =~ %r(/main.js\?\d+)
69
+ Sprockets::Helpers.public_path = nil
70
+ end
71
+ end
72
+ end
73
+
4
74
  describe "#asset_path" do
5
75
  context "with URIs" do
6
76
  it "returns URIs untouched" do
@@ -32,8 +102,8 @@ describe Sprockets::Helpers do
32
102
 
33
103
  it "appends a timestamp if the file exists in the output path" do
34
104
  within_construct do |c|
35
- file1 = c.file "public/main.js"
36
- file2 = c.file "public/favicon.ico"
105
+ c.file "public/main.js"
106
+ c.file "public/favicon.ico"
37
107
 
38
108
  context.asset_path("main", :ext => "js").should =~ %r(/main.js\?\d+)
39
109
  context.asset_path("/favicon.ico").should =~ %r(/favicon.ico\?\d+)
@@ -60,9 +130,6 @@ describe Sprockets::Helpers do
60
130
 
61
131
  context.asset_path("logo.jpg").should == "/assets/logo.jpg"
62
132
  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
133
  end
67
134
  end
68
135
 
@@ -72,9 +139,6 @@ describe Sprockets::Helpers do
72
139
 
73
140
  context.asset_path("main", :ext => "js").should == "/assets/main.js"
74
141
  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
142
  end
79
143
  end
80
144
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sprockets-helpers
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 1
8
+ - 2
9
9
  - 0
10
- version: 0.1.0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Pete Browne
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-10-31 00:00:00 Z
18
+ date: 2011-11-01 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  requirement: &id001 !ruby/object:Gem::Requirement