asset-resource 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,86 @@
1
+ # AssetResource
2
+
3
+ Serve up your CSS and Javascript as first-class resources.
4
+
5
+ Supports in-line rendering of engines such as Sass and Less. These files will
6
+ be served with 24-hour HTTP cache expiry.
7
+
8
+ Built-in configuration and helpers for Rails 2.x, Rails 3.x and Sinatra.
9
+
10
+ ## Usage
11
+
12
+ ### Rails 2.x
13
+
14
+ # public/stylesheets/*.{css|less|sass}
15
+ # public/javascripts/*.js
16
+
17
+ # config/environment.rb
18
+ config.gem "asset-resource"
19
+
20
+ # app/views/layouts/application.html.erb
21
+ <html>
22
+ <head>
23
+ <%= asset_resource_javascripts %>
24
+ <%= asset_resource_stylesheets %>
25
+ </head>
26
+ </html>
27
+
28
+ ### Rails 3.x
29
+
30
+ # public/stylesheets/*.{css|less|sass}
31
+ # public/javascripts/*.js
32
+
33
+ # Gemfile
34
+ gem "asset-resource"
35
+
36
+ # app/views/layouts/application.html.erb
37
+ <html>
38
+ <head>
39
+ <%= asset_resource_javascripts %>
40
+ <%= asset_resource_stylesheets %>
41
+ </head>
42
+ </html>
43
+
44
+ ### Sinatra
45
+
46
+ # assets/styles/*.{css|less|sass}
47
+ # assets/scripts/*.js
48
+
49
+ # app.rb
50
+ require "asset_resource"
51
+
52
+ class App < Sinatra::Base
53
+ register Sinatra::AssetResource
54
+ end
55
+
56
+ __END__
57
+
58
+ @@ layout
59
+ %html
60
+ %head
61
+ = asset_resource_scripts
62
+ = asset_resource_styles
63
+
64
+ ## Technical Details
65
+
66
+ `AssetResource::Middleware` serves requests at `/assets/stylesheets.css` and
67
+ `/assets/javascripts.js`.
68
+
69
+ The gem will add appropriate view helpers to your framework of choice.
70
+
71
+ You can use the middleware yourself like this:
72
+
73
+ use AssetResource::Middleware,
74
+ :base_path => "public",
75
+ :handlers => { :javascripts => "text/javascript",
76
+ :stylesheets => "text/css" }
77
+
78
+ ## Copyright
79
+
80
+ MIT License
81
+
82
+ ## Author
83
+
84
+ David Dollar
85
+
86
+ [http://daviddollar.org](http://daviddollar.org)
@@ -1,5 +1,4 @@
1
1
  module AssetResource
2
- VERSION = "0.3.1"
3
2
  end
4
3
 
5
4
  if defined?(Rails)
@@ -1,15 +1,6 @@
1
1
  require "asset_resource"
2
2
  require "asset_resource/middleware"
3
-
4
- module AssetResource::Helpers
5
- def asset_resource_stylesheets
6
- '<link rel="stylesheet" href="/assets/stylesheets.css" />'.html_safe
7
- end
8
-
9
- def asset_resource_javascripts
10
- '<script type="text/javascript" src="/assets/javascripts.js"></script>'.html_safe
11
- end
12
- end
3
+ require 'asset_resource/framework/rails_view_helpers'
13
4
 
14
5
  Rails.configuration.middleware.use AssetResource::Middleware,
15
6
  :base_path => Rails.root.join("public"),
@@ -1,23 +1,15 @@
1
1
  require "asset_resource"
2
2
  require "asset_resource/middleware"
3
3
  require "active_support"
4
+ require 'asset_resource/framework/rails_view_helpers'
4
5
 
5
- ActiveSupport.on_load(:before_configuration) do
6
- Rails.application.config.middleware.use AssetResource::Middleware,
7
- :base_path => Rails.root.join("public"),
8
- :handlers => { :javascripts => "text/javascript",
9
- :stylesheets => "text/css" }
10
- end
11
-
12
- module AssetResource::Helpers
13
- def asset_resource_stylesheets
14
- '<link rel="stylesheet" href="/assets/stylesheets.css" />'.html_safe
15
- end
6
+ # example usage: put this in your application.rb
7
+ # config.middleware.use AssetResource::Middleware,
8
+ # :base_path => Rails.root.join("public"),
9
+ # :handlers => { :javascripts => "text/javascript",
10
+ # :stylesheets => "text/css" }
11
+ # end
16
12
 
17
- def asset_resource_javascripts
18
- '<script type="text/javascript" src="/assets/javascripts.js"></script>'.html_safe
19
- end
20
- end
21
13
 
22
14
  ActiveSupport.on_load(:action_view) do
23
15
  include AssetResource::Helpers
@@ -0,0 +1,11 @@
1
+ module AssetResource
2
+ module Helpers
3
+ def asset_resource_stylesheets(resource_name = "stylesheets")
4
+ "<link rel='stylesheet' href='/assets/#{resource_name}.css' />".html_safe
5
+ end
6
+
7
+ def asset_resource_javascripts(resource_name = "javascripts")
8
+ "<script type='text/javascript' src='/assets/#{resource_name}.js'></script>".html_safe
9
+ end
10
+ end
11
+ end
@@ -42,8 +42,7 @@ class AssetResource::Middleware
42
42
  def call(env)
43
43
  if env["PATH_INFO"] =~ %r{\A/assets/(.+)\B}
44
44
  type = $1.split(".").first
45
- return app.call(env) unless handles?(type)
46
- return [200, asset_headers(type), process_files(files_for(type))]
45
+ return [200, asset_headers(type), process_files(files_for(type))] if handles?(type)
47
46
  end
48
47
  app.call(env)
49
48
  end
@@ -0,0 +1,3 @@
1
+ module AssetResource
2
+ VERSION = "0.4.0"
3
+ end
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
2
+ require 'asset_resource/framework/rails_view_helpers'
3
+
4
+
5
+ describe AssetResource::Helpers do
6
+ class TestDummy
7
+ include AssetResource::Helpers
8
+ end
9
+
10
+ class String
11
+ def html_safe
12
+ self
13
+ end
14
+ end
15
+
16
+ describe "#asset_resource_stylesheets" do
17
+ let(:helper) { TestDummy.new }
18
+ context "when you don't pass in a resource name" do
19
+ it "points to stylesheets.css" do
20
+ helper.asset_resource_stylesheets.should include("stylesheets.css")
21
+ end
22
+ end
23
+
24
+ context "when you pass in a resource name" do
25
+ it "points to the desired resource" do
26
+ helper.asset_resource_stylesheets("another_stylesheet").should include("another_stylesheet.css")
27
+ end
28
+ end
29
+ end
30
+ describe "#asset_resource_javascripts" do
31
+ let(:helper) { TestDummy.new }
32
+ context "when you don't pass in a resource name" do
33
+ it "points to javascripts.js" do
34
+ helper.asset_resource_javascripts.should include("javascripts.js")
35
+ end
36
+ end
37
+
38
+ context "when you pass in a resource name" do
39
+ it "points to the desired resource" do
40
+ helper.asset_resource_javascripts("another_resource").should include("another_resource.js")
41
+ end
42
+ end
43
+ end
44
+ end
@@ -10,6 +10,40 @@ describe AssetResource::Middleware do
10
10
  Artifice.activate_with(middleware)
11
11
  end
12
12
 
13
+ context "separating out cache" do
14
+ let(:middleware) { AssetResource::Middleware.new(app, :base_path => asset_fixture,
15
+ :handlers => {:scripts_1 => "text/javascript",
16
+ :scripts_2 => 'text/javascript'}) }
17
+ before do
18
+ middleware.options[:handlers] = { :scripts_1 => "text/javascript",
19
+ :scripts_2 => "text/javascript" }
20
+ end
21
+
22
+ context "first split" do
23
+ it "includes the files in the first split" do
24
+ data = RestClient.get("http://localhost/assets/scripts_1.js")
25
+ data.should include("console.log('I am script 1')")
26
+ end
27
+
28
+ it "does not include the files in the second split" do
29
+ data = RestClient.get("http://localhost/assets/scripts_1.js")
30
+ data.should_not include("console.log('I am script 2')")
31
+ end
32
+ end
33
+
34
+ context "second split" do
35
+ it "includes the files in the second split" do
36
+ data = RestClient.get("http://localhost/assets/scripts_1.js")
37
+ data.should include("console.log('I am script 1')")
38
+ end
39
+
40
+ it "does not include the files in the first split" do
41
+ data = RestClient.get("http://localhost/assets/scripts_1.js")
42
+ data.should_not include("console.log('I am script 2')")
43
+ end
44
+ end
45
+ end
46
+
13
47
  it "passes unhandled requests" do
14
48
  RestClient.get("http://localhost/foo").to_s.should == "bar"
15
49
  end
@@ -70,4 +104,13 @@ describe AssetResource::Middleware do
70
104
  end
71
105
  end
72
106
 
107
+ describe "with subdirs" do
108
+ let(:middleware) { AssetResource::Middleware.new(app, :base_path => asset_fixture("with_subdir")) }
109
+
110
+ it "should render files in subdirs" do
111
+ data = RestClient.get("http://localhost/assets/styles.css")
112
+ data.should == "test\n"
113
+ end
114
+ end
115
+
73
116
  end
@@ -0,0 +1,3 @@
1
+ $(document).ready(function() {
2
+ console.log('I am script 1');
3
+ });
@@ -0,0 +1,3 @@
1
+ $(document).ready(function() {
2
+ console.log('I am script 2');
3
+ });
metadata CHANGED
@@ -1,26 +1,26 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: asset-resource
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
4
+ hash: 15
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 3
9
- - 1
10
- version: 0.3.1
8
+ - 4
9
+ - 0
10
+ version: 0.4.0
11
11
  platform: ruby
12
12
  authors:
13
- - |
14
- David Dollar
15
-
13
+ - David Dollar
16
14
  autorequire:
17
15
  bindir: bin
18
16
  cert_chain: []
19
17
 
20
- date: 2010-08-23 00:00:00 -04:00
18
+ date: 2010-11-18 00:00:00 -05:00
21
19
  default_executable:
22
20
  dependencies:
23
21
  - !ruby/object:Gem::Dependency
22
+ name: rake
23
+ prerelease: false
24
24
  requirement: &id001 !ruby/object:Gem::Requirement
25
25
  none: false
26
26
  requirements:
@@ -31,40 +31,26 @@ dependencies:
31
31
  - 0
32
32
  version: "0"
33
33
  type: :development
34
- name: parka
35
- prerelease: false
36
34
  version_requirements: *id001
37
35
  - !ruby/object:Gem::Dependency
36
+ name: artifice
37
+ prerelease: false
38
38
  requirement: &id002 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
- - - ">="
41
+ - - ~>
42
42
  - !ruby/object:Gem::Version
43
- hash: 3
43
+ hash: 7
44
44
  segments:
45
45
  - 0
46
- version: "0"
46
+ - 6
47
+ version: "0.6"
47
48
  type: :development
48
- name: rake
49
- prerelease: false
50
49
  version_requirements: *id002
51
50
  - !ruby/object:Gem::Dependency
52
- requirement: &id003 !ruby/object:Gem::Requirement
53
- none: false
54
- requirements:
55
- - - ~>
56
- - !ruby/object:Gem::Version
57
- hash: 1
58
- segments:
59
- - 0
60
- - 5
61
- version: "0.5"
62
- type: :development
63
- name: artifice
51
+ name: fakefs
64
52
  prerelease: false
65
- version_requirements: *id003
66
- - !ruby/object:Gem::Dependency
67
- requirement: &id004 !ruby/object:Gem::Requirement
53
+ requirement: &id003 !ruby/object:Gem::Requirement
68
54
  none: false
69
55
  requirements:
70
56
  - - ~>
@@ -76,27 +62,27 @@ dependencies:
76
62
  - 1
77
63
  version: 0.2.1
78
64
  type: :development
79
- name: fakefs
80
- prerelease: false
81
- version_requirements: *id004
65
+ version_requirements: *id003
82
66
  - !ruby/object:Gem::Dependency
83
- requirement: &id005 !ruby/object:Gem::Requirement
67
+ name: haml
68
+ prerelease: false
69
+ requirement: &id004 !ruby/object:Gem::Requirement
84
70
  none: false
85
71
  requirements:
86
72
  - - ~>
87
73
  - !ruby/object:Gem::Version
88
- hash: 21
74
+ hash: 41
89
75
  segments:
90
76
  - 3
91
77
  - 0
92
- - 9
93
- version: 3.0.9
78
+ - 23
79
+ version: 3.0.23
94
80
  type: :development
95
- name: haml
96
- prerelease: false
97
- version_requirements: *id005
81
+ version_requirements: *id004
98
82
  - !ruby/object:Gem::Dependency
99
- requirement: &id006 !ruby/object:Gem::Requirement
83
+ name: less
84
+ prerelease: false
85
+ requirement: &id005 !ruby/object:Gem::Requirement
100
86
  none: false
101
87
  requirements:
102
88
  - - ~>
@@ -108,27 +94,27 @@ dependencies:
108
94
  - 21
109
95
  version: 1.2.21
110
96
  type: :development
111
- name: less
112
- prerelease: false
113
- version_requirements: *id006
97
+ version_requirements: *id005
114
98
  - !ruby/object:Gem::Dependency
115
- requirement: &id007 !ruby/object:Gem::Requirement
99
+ name: rest-client
100
+ prerelease: false
101
+ requirement: &id006 !ruby/object:Gem::Requirement
116
102
  none: false
117
103
  requirements:
118
104
  - - ~>
119
105
  - !ruby/object:Gem::Version
120
- hash: 1
106
+ hash: 13
121
107
  segments:
122
108
  - 1
123
- - 5
109
+ - 6
124
110
  - 1
125
- version: 1.5.1
111
+ version: 1.6.1
126
112
  type: :development
127
- name: rest-client
128
- prerelease: false
129
- version_requirements: *id007
113
+ version_requirements: *id006
130
114
  - !ruby/object:Gem::Dependency
131
- requirement: &id008 !ruby/object:Gem::Requirement
115
+ name: rcov
116
+ prerelease: false
117
+ requirement: &id007 !ruby/object:Gem::Requirement
132
118
  none: false
133
119
  requirements:
134
120
  - - ~>
@@ -140,31 +126,25 @@ dependencies:
140
126
  - 8
141
127
  version: 0.9.8
142
128
  type: :development
143
- name: rcov
144
- prerelease: false
145
- version_requirements: *id008
129
+ version_requirements: *id007
146
130
  - !ruby/object:Gem::Dependency
147
- requirement: &id009 !ruby/object:Gem::Requirement
131
+ name: rspec
132
+ prerelease: false
133
+ requirement: &id008 !ruby/object:Gem::Requirement
148
134
  none: false
149
135
  requirements:
150
136
  - - ~>
151
137
  - !ruby/object:Gem::Version
152
- hash: 62196421
138
+ hash: 15
153
139
  segments:
154
140
  - 2
155
141
  - 0
156
142
  - 0
157
- - beta
158
- - 19
159
- version: 2.0.0.beta.19
143
+ version: 2.0.0
160
144
  type: :development
161
- name: rspec
162
- prerelease: false
163
- version_requirements: *id009
164
- description: Manage CSS/JS assets as dynamic resources rather than static files
165
- email: |
166
- <ddollar@gmail.com>
167
-
145
+ version_requirements: *id008
146
+ description:
147
+ email: ddollar@gmail.com
168
148
  executables: []
169
149
 
170
150
  extensions: []
@@ -173,11 +153,15 @@ extra_rdoc_files: []
173
153
 
174
154
  files:
175
155
  - lib/asset-resource.rb
176
- - lib/asset_resource.rb
177
156
  - lib/asset_resource/framework/rails2.rb
178
157
  - lib/asset_resource/framework/rails3.rb
158
+ - lib/asset_resource/framework/rails_view_helpers.rb
179
159
  - lib/asset_resource/framework/sinatra.rb
180
160
  - lib/asset_resource/middleware.rb
161
+ - lib/asset_resource/version.rb
162
+ - lib/asset_resource.rb
163
+ - README.md
164
+ - spec/asset_resource/framework/asset_resource_helpers_spec.rb
181
165
  - spec/asset_resource/middleware_spec.rb
182
166
  - spec/asset_resource_spec.rb
183
167
  - spec/fixtures/blank_extension/styles/no_extension
@@ -186,9 +170,12 @@ files:
186
170
  - spec/fixtures/normal/styles/_no.css
187
171
  - spec/fixtures/normal/styles/sample.less
188
172
  - spec/fixtures/normal/styles/sample.sass
173
+ - spec/fixtures/scripts_1/script1.js
174
+ - spec/fixtures/scripts_2/script2.js
189
175
  - spec/fixtures/with_images/styles/image.gif
190
176
  - spec/fixtures/with_images/styles/image.png
191
177
  - spec/fixtures/with_images/styles/sample.css
178
+ - spec/fixtures/with_subdir/styles/subdir/sample.css
192
179
  - spec/spec_helper.rb
193
180
  has_rdoc: true
194
181
  homepage: http://github.com/ddollar/asset-resource
@@ -219,7 +206,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
219
206
  version: "0"
220
207
  requirements: []
221
208
 
222
- rubyforge_project: nowarning
209
+ rubyforge_project:
223
210
  rubygems_version: 1.3.7
224
211
  signing_key:
225
212
  specification_version: 3