JackDanger-sweet_assets 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +16 -0
- data/MIT-LICENSE +20 -0
- data/Manifest.txt +15 -0
- data/README.txt +49 -0
- data/Rakefile +15 -0
- data/init.rb +1 -0
- data/install.rb +0 -0
- data/lib/sweet_assets.rb +246 -0
- data/rails/init.rb +11 -0
- data/sweet_assets.gemspec +32 -0
- data/test/sweet_assets_test.rb +170 -0
- data/test/views/defaults/index.html.erb +7 -0
- data/test/views/shortcut/index.html.erb +7 -0
- data/test/views/web/index.html.erb +7 -0
- data/uninstall.rb +1 -0
- metadata +80 -0
data/History.txt
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
=== 1.0.0 / 2008-03-11
|
2
|
+
|
3
|
+
* 1 major enhancement
|
4
|
+
|
5
|
+
* First iteration complete.
|
6
|
+
|
7
|
+
|
8
|
+
=== 2.0.1 / 2008-05-27
|
9
|
+
|
10
|
+
* 2 major enhancements
|
11
|
+
|
12
|
+
* Completely rewritten. Doesn't use <head> parsing magic.
|
13
|
+
* Supports media selection for stylesheets
|
14
|
+
|
15
|
+
|
16
|
+
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2007 [name of plugin creator]
|
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/Manifest.txt
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
History.txt
|
2
|
+
MIT-LICENSE
|
3
|
+
Manifest.txt
|
4
|
+
README.txt
|
5
|
+
Rakefile
|
6
|
+
init.rb
|
7
|
+
install.rb
|
8
|
+
lib/sweet_assets.rb
|
9
|
+
rails/init.rb
|
10
|
+
sweet_assets.gemspec
|
11
|
+
test/sweet_assets_test.rb
|
12
|
+
test/views/defaults/index.html.erb
|
13
|
+
test/views/shortcut/index.html.erb
|
14
|
+
test/views/web/index.html.erb
|
15
|
+
uninstall.rb
|
data/README.txt
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
= SweetAssets
|
2
|
+
|
3
|
+
http://github.com/JackDanger/sweet_assets
|
4
|
+
|
5
|
+
== Description
|
6
|
+
|
7
|
+
Automate adding stylesheets and javascripts to your controller actions
|
8
|
+
|
9
|
+
== Example
|
10
|
+
|
11
|
+
Any controller will include the stylesheet and javascript source files named after named after the contorller if the file exists:
|
12
|
+
|
13
|
+
class UsersController < ApplicationController
|
14
|
+
...
|
15
|
+
end
|
16
|
+
|
17
|
+
http://mysite.com/users/ will have /stylesheets/users.css and /javascripts/users.js included if they exist.
|
18
|
+
|
19
|
+
Any controller can specify any other file to be added as a stylesheet:
|
20
|
+
|
21
|
+
class UsersController < ApplicationController
|
22
|
+
style_like :homes
|
23
|
+
script_like :gargantuan
|
24
|
+
end
|
25
|
+
|
26
|
+
http://mysite.com/users/ will have users.css, homes.css and gargantuan.js included
|
27
|
+
|
28
|
+
Any asset that needs to take precedence over others (i.e. should be linked after the other stylesheets) can be used with a bang (!)
|
29
|
+
|
30
|
+
class UsersController < ApplicationController
|
31
|
+
style_like :distort_reality!, :homes, :trees
|
32
|
+
script_like :basic_script, :super_enhancement!
|
33
|
+
end
|
34
|
+
|
35
|
+
http://mysite.com/users/ will have homes.css, and trees.css and basic_script.js linked before the <title> tag.
|
36
|
+
distort_reality.css and super_enhancement.js will be linked after the <title> tag.
|
37
|
+
Note: the controller-named assets (users.css and users.js in this case) will always
|
38
|
+
be applied with precedence.
|
39
|
+
|
40
|
+
If you need better control over where these assets appear you can use the same options as you would for a before_filter
|
41
|
+
|
42
|
+
class UsersController < ApplicationController
|
43
|
+
style_like :homes!, :only => :show
|
44
|
+
script_like :trees, :except => [:index, :show]
|
45
|
+
end
|
46
|
+
|
47
|
+
All linked assets will be cached into a single asset if caching is enabled.
|
48
|
+
|
49
|
+
Copyright (c) 2007 Jack Danger Canty of adPickles Inc, released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# -*- ruby -*-
|
2
|
+
|
3
|
+
$:.unshift(File.dirname(__FILE__) + '/lib')
|
4
|
+
|
5
|
+
|
6
|
+
require 'rubygems'
|
7
|
+
require 'hoe'
|
8
|
+
require "sweet_assets"
|
9
|
+
|
10
|
+
Hoe.new('sweet_assets', SweetAssets::VERSION) do |p|
|
11
|
+
p.rubyforge_name = 'objectproxy' # if different than lowercase project name
|
12
|
+
p.developer('Jack Danger Canty', 'sweet_assets_gem@6brand.com')
|
13
|
+
end
|
14
|
+
|
15
|
+
# vim: syntax=Ruby
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rails/init.rb
|
data/install.rb
ADDED
File without changes
|
data/lib/sweet_assets.rb
ADDED
@@ -0,0 +1,246 @@
|
|
1
|
+
require 'cgi'
|
2
|
+
require 'active_support'
|
3
|
+
require 'action_view/helpers/url_helper'
|
4
|
+
require 'action_view/helpers/asset_tag_helper'
|
5
|
+
require 'action_view/helpers/tag_helper'
|
6
|
+
require 'digest/md5'
|
7
|
+
|
8
|
+
# USAGE:
|
9
|
+
# style_like :dolphins # assumes :media => 'all'
|
10
|
+
# style_like :home, :media => 'print'
|
11
|
+
# style_like :users, :media => ['print', 'screen']
|
12
|
+
# script_like :boats
|
13
|
+
|
14
|
+
module SweetAssets
|
15
|
+
VERSION = '1.2.0'
|
16
|
+
DEFAULT_JAVASCRIPTS = ['prototype', 'effects', 'dragdrop', 'controls']
|
17
|
+
SCRIPT_PLACEHOLDER = '<!--SWEET_JAVASCRIPTS-->'
|
18
|
+
STYLE_PLACEHOLDER = '<!--SWEET_STYLESHEETS-->'
|
19
|
+
module ActionController
|
20
|
+
def self.included(base)
|
21
|
+
base.class_eval do
|
22
|
+
extend ClassMethods
|
23
|
+
include InstanceMethods
|
24
|
+
include AppendAssetsAfterRescue
|
25
|
+
after_filter :apply_sweet_assets
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
module ClassMethods
|
30
|
+
def style_like(*assets)
|
31
|
+
prepend_before_filter do |controller|
|
32
|
+
controller.send :style_like, *assets
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def script_like(*assets)
|
37
|
+
prepend_before_filter do |controller|
|
38
|
+
controller.send :script_like, *assets
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
module InstanceMethods
|
44
|
+
protected
|
45
|
+
# Apply the assets even if the filter chain was aborted
|
46
|
+
def call_filters(*args)
|
47
|
+
ret = super
|
48
|
+
apply_sweet_assets
|
49
|
+
ret
|
50
|
+
end
|
51
|
+
|
52
|
+
def style_like(*assets)
|
53
|
+
sweet_assets.style_like(*assets)
|
54
|
+
end
|
55
|
+
|
56
|
+
def script_like(*assets)
|
57
|
+
sweet_assets.script_like(*assets)
|
58
|
+
end
|
59
|
+
|
60
|
+
def sweet_assets
|
61
|
+
@sweet_assets ||= SweetAssets::Request.new(self)
|
62
|
+
end
|
63
|
+
|
64
|
+
def apply_sweet_assets
|
65
|
+
return if @sweet_assets_applied
|
66
|
+
if response.body.respond_to?(:gsub!)
|
67
|
+
response.body.gsub! SweetAssets::STYLE_PLACEHOLDER, "\n#{sweet_assets.tags.stylesheets}"
|
68
|
+
response.body.gsub! SweetAssets::SCRIPT_PLACEHOLDER, "\n#{sweet_assets.tags.javascripts}"
|
69
|
+
end
|
70
|
+
@sweet_assets_applied = true
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
module ActionView
|
76
|
+
module Helpers
|
77
|
+
def style_like(*assets)
|
78
|
+
sweet_assets.style_like(*assets)
|
79
|
+
end
|
80
|
+
|
81
|
+
def script_like(*assets)
|
82
|
+
sweet_assets.script_like(*assets)
|
83
|
+
end
|
84
|
+
|
85
|
+
def sweet_stylesheets
|
86
|
+
SweetAssets::STYLE_PLACEHOLDER
|
87
|
+
end
|
88
|
+
|
89
|
+
def sweet_javascripts
|
90
|
+
SweetAssets::SCRIPT_PLACEHOLDER
|
91
|
+
end
|
92
|
+
|
93
|
+
protected
|
94
|
+
|
95
|
+
def sweet_assets
|
96
|
+
controller.send :sweet_assets
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
class Request
|
102
|
+
def initialize(controller)
|
103
|
+
@stylesheets = Array.new
|
104
|
+
@javascripts = Array.new
|
105
|
+
@controller = controller
|
106
|
+
|
107
|
+
# use the assets named 'application'
|
108
|
+
style_like :application
|
109
|
+
script_like :application
|
110
|
+
|
111
|
+
# use the assets named after the controller at higher priority
|
112
|
+
style_like "#{@controller.class.controller_name}!"
|
113
|
+
script_like "#{@controller.class.controller_name}!"
|
114
|
+
end
|
115
|
+
|
116
|
+
def style_like(*assets)
|
117
|
+
options = HashWithIndifferentAccess.new(assets.extract_options!)
|
118
|
+
media = [options[:media]].flatten.compact
|
119
|
+
media = ['all'] if media.blank?
|
120
|
+
media.each do |medium|
|
121
|
+
medium = medium.to_s
|
122
|
+
assets.each do |asset|
|
123
|
+
asset = asset.to_s
|
124
|
+
if asset.ends_with?('!')
|
125
|
+
asset.gsub!(/!$/, '')
|
126
|
+
# remove a lower-priority entry if it exists for this medium
|
127
|
+
@stylesheets.delete({:file => asset, :medium => medium, :priority => 'low'})
|
128
|
+
@stylesheets << {:file => asset, :medium => medium, :priority => 'high'}
|
129
|
+
else
|
130
|
+
# don't add this entry if it's already set to high-priority
|
131
|
+
unless @stylesheets.include?({:file => asset, :medium => medium, :priority => 'high'})
|
132
|
+
@stylesheets << {:file => asset, :medium => medium, :priority => 'low'}
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
def script_like(*assets)
|
140
|
+
assets.each do |asset|
|
141
|
+
asset = asset.to_s
|
142
|
+
if 'defaults' == asset
|
143
|
+
DEFAULT_JAVASCRIPTS.each do |default|
|
144
|
+
@javascripts << {:file => default, :priority => 'first' }
|
145
|
+
end
|
146
|
+
elsif asset.ends_with?('!')
|
147
|
+
asset.gsub!(/!$/, '')
|
148
|
+
# remove a lower-priority entry if it exists
|
149
|
+
@javascripts.delete({:file => asset, :priority => 'low'})
|
150
|
+
@javascripts << {:file => asset, :priority => 'high'}
|
151
|
+
else
|
152
|
+
# don't add this entry if it's already set to high-priority
|
153
|
+
unless @javascripts.include?({:file => asset, :priority => 'high'})
|
154
|
+
@javascripts << {:file => asset, :priority => 'low'}
|
155
|
+
end
|
156
|
+
end
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
def tags
|
161
|
+
@tag_generator ||= TagGenerator.new(@stylesheets, @javascripts, @controller)
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
class TagGenerator
|
166
|
+
|
167
|
+
include ::ActionView::Helpers::TagHelper
|
168
|
+
include ::ActionView::Helpers::AssetTagHelper
|
169
|
+
|
170
|
+
def initialize(stylesheets, javascripts, controller)
|
171
|
+
@stylesheets = stylesheets
|
172
|
+
@javascripts = javascripts
|
173
|
+
@controller = controller
|
174
|
+
end
|
175
|
+
|
176
|
+
def stylesheets
|
177
|
+
media = @stylesheets.map {|asset| asset[:medium] }.uniq
|
178
|
+
|
179
|
+
media.map do |medium|
|
180
|
+
files_in_order = ['low', 'high'].map do |priority|
|
181
|
+
|
182
|
+
files = @stylesheets.select do |asset|
|
183
|
+
asset[:medium] == medium && asset[:priority] == priority
|
184
|
+
end
|
185
|
+
files.map! {|asset| asset[:file] }
|
186
|
+
files.map! {|file| file =~ /\.css$/ ? file : "#{file}.css" }
|
187
|
+
|
188
|
+
# check that the files exist
|
189
|
+
unless RAILS_ENV.eql?('test')
|
190
|
+
files = files.select {|file| File.exists?( file.starts_with?('/') ? "#{RAILS_ROOT}/public#{file}" : "#{STYLESHEETS_DIR}/#{file}") }
|
191
|
+
end
|
192
|
+
|
193
|
+
files
|
194
|
+
end.flatten.uniq.compact
|
195
|
+
|
196
|
+
stylesheet_link_tag(*(files_in_order << {:media => medium, :cache => cached_file_name(files_in_order)}))
|
197
|
+
|
198
|
+
end.join("\n")
|
199
|
+
end
|
200
|
+
|
201
|
+
def javascripts
|
202
|
+
files_in_order = ['first', 'low', 'high'].map do |priority|
|
203
|
+
|
204
|
+
files = @javascripts.select {|asset| asset[:priority] == priority }
|
205
|
+
files.map! {|asset| asset[:file] }
|
206
|
+
files.map! {|file| file =~ /\.js$/ ? file : "#{file}.js" }
|
207
|
+
|
208
|
+
# check that the files exist
|
209
|
+
unless RAILS_ENV.eql?('test')
|
210
|
+
files = files.select {|file| File.exists?(file.starts_with?('/') ? "#{RAILS_ROOT}/public#{file}" : "#{JAVASCRIPTS_DIR}/#{file}") }
|
211
|
+
end
|
212
|
+
files
|
213
|
+
end.flatten.uniq.compact
|
214
|
+
|
215
|
+
files_in_order << {:cache => cached_file_name(files_in_order)}
|
216
|
+
|
217
|
+
javascript_include_tag *files_in_order
|
218
|
+
end
|
219
|
+
|
220
|
+
protected
|
221
|
+
|
222
|
+
def cached_file_name(filenames)
|
223
|
+
"sweet_assets_#{Digest::MD5.hexdigest(filenames.join('_'))}"
|
224
|
+
end
|
225
|
+
end
|
226
|
+
|
227
|
+
|
228
|
+
# we're going to wrap our rescue_action around the top-level rescue_action method chain
|
229
|
+
# this ensures that we can apply the stylesheets even if the after filter was aborted due to an exception
|
230
|
+
module AppendAssetsAfterRescue
|
231
|
+
def self.included(base)
|
232
|
+
base.class_eval do
|
233
|
+
def initialize_with_append_sweet_assets_rescue_action(*args)
|
234
|
+
initialize_without_append_sweet_assets_rescue_action(*args)
|
235
|
+
self.class.send :alias_method_chain, :rescue_action, :append_sweet_assets unless respond_to?(:rescue_action_without_append_sweet_assets)
|
236
|
+
end
|
237
|
+
alias_method_chain :initialize, :append_sweet_assets_rescue_action unless respond_to?(:initialize_without_append_sweet_assets_rescue_action)
|
238
|
+
end
|
239
|
+
end
|
240
|
+
|
241
|
+
def rescue_action_with_append_sweet_assets(*args)
|
242
|
+
rescue_action_without_append_sweet_assets(*args)
|
243
|
+
apply_sweet_assets
|
244
|
+
end
|
245
|
+
end
|
246
|
+
end
|
data/rails/init.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = %q{sweet_assets}
|
3
|
+
s.version = "1.2.0"
|
4
|
+
|
5
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
6
|
+
s.authors = ["Jack Danger Canty"]
|
7
|
+
s.date = %q{2009-03-19}
|
8
|
+
s.description = %q{Automate adding stylesheets and javascripts to your controller actions}
|
9
|
+
s.email = ["sweet_assets_gem@6brand.com"]
|
10
|
+
s.extra_rdoc_files = ["History.txt", "Manifest.txt", "README.txt"]
|
11
|
+
s.files = ["History.txt", "MIT-LICENSE", "Manifest.txt", "README.txt", "Rakefile", "init.rb", "install.rb", "lib/sweet_assets.rb", "rails/init.rb", "sweet_assets.gemspec", "test/sweet_assets_test.rb", "test/views/defaults/index.html.erb", "test/views/shortcut/index.html.erb", "test/views/web/index.html.erb", "uninstall.rb"]
|
12
|
+
s.has_rdoc = true
|
13
|
+
s.homepage = %q{http://github.com/JackDanger/sweet_assets}
|
14
|
+
s.rdoc_options = ["--main", "README.txt"]
|
15
|
+
s.require_paths = ["lib"]
|
16
|
+
s.rubyforge_project = %q{objectproxy}
|
17
|
+
s.rubygems_version = %q{1.3.1}
|
18
|
+
s.summary = %q{Automate adding stylesheets and javascripts to your controller actions}
|
19
|
+
|
20
|
+
if s.respond_to? :specification_version then
|
21
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
22
|
+
s.specification_version = 2
|
23
|
+
|
24
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
25
|
+
s.add_development_dependency(%q<hoe>, [">= 1.11.0"])
|
26
|
+
else
|
27
|
+
s.add_dependency(%q<hoe>, [">= 1.11.0"])
|
28
|
+
end
|
29
|
+
else
|
30
|
+
s.add_dependency(%q<hoe>, [">= 1.11.0"])
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,170 @@
|
|
1
|
+
|
2
|
+
RAILS_ENV = 'test'
|
3
|
+
|
4
|
+
require File.dirname(__FILE__) + '/../../../../config/environment'
|
5
|
+
require 'test/unit'
|
6
|
+
require 'rubygems'
|
7
|
+
require 'action_controller/test_process'
|
8
|
+
require File.dirname(__FILE__) + '/../init'
|
9
|
+
|
10
|
+
ActionController::Base.view_paths = [File.dirname(__FILE__) + '/views/']
|
11
|
+
|
12
|
+
# Re-raise errors caught by the controller.
|
13
|
+
class WebController < ActionController::Base
|
14
|
+
style_like :home
|
15
|
+
style_like :users!
|
16
|
+
style_like :extra
|
17
|
+
script_like :home
|
18
|
+
script_like :users!
|
19
|
+
script_like :extra
|
20
|
+
end
|
21
|
+
|
22
|
+
class ShortcutController < ActionController::Base
|
23
|
+
style_like :home!, :users
|
24
|
+
script_like :extra!, :home
|
25
|
+
end
|
26
|
+
|
27
|
+
class DefaultsController < ActionController::Base
|
28
|
+
script_like :defaults
|
29
|
+
end
|
30
|
+
|
31
|
+
class SweetAssetsTest < Test::Unit::TestCase
|
32
|
+
|
33
|
+
def setup
|
34
|
+
ActionController::Base.perform_caching = false
|
35
|
+
@controller = WebController.new
|
36
|
+
@request = ActionController::TestRequest.new
|
37
|
+
@response = ActionController::TestResponse.new
|
38
|
+
end
|
39
|
+
|
40
|
+
def teardown
|
41
|
+
delete_empty_recent_files
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_each_stylesheet_should_appear
|
45
|
+
get :index
|
46
|
+
assert_response :success
|
47
|
+
assert_tag :link, :attributes => {:href => /stylesheets\/application.css/, :rel => 'stylesheet'}
|
48
|
+
assert_tag :link, :attributes => {:href => /stylesheets\/web.css/, :rel => 'stylesheet'}
|
49
|
+
assert_tag :link, :attributes => {:href => /stylesheets\/home.css/, :rel => 'stylesheet'}
|
50
|
+
assert_tag :link, :attributes => {:href => /stylesheets\/extra.css/, :rel => 'stylesheet'}
|
51
|
+
assert_tag :link, :attributes => {:href => /stylesheets\/users.css/, :rel => 'stylesheet'}
|
52
|
+
assert_select 'head > link', :count => 5
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_bottom_stylesheets_should_appear_last
|
56
|
+
get :index
|
57
|
+
assert_response :success
|
58
|
+
location_of_extra_css = @response.body =~ /<link href="\/stylesheets\/extra.css/
|
59
|
+
location_of_home_css = @response.body =~ /<link href="\/stylesheets\/home.css/
|
60
|
+
location_of_users_css = @response.body =~ /<link href="\/stylesheets\/users.css/
|
61
|
+
assert location_of_home_css < location_of_users_css
|
62
|
+
assert location_of_extra_css < location_of_users_css
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_caching_stylesheets
|
66
|
+
ActionController::Base.perform_caching = true
|
67
|
+
with_stylesheets :application, :home, :web, :users, :extra do
|
68
|
+
with_javascripts :application, :home, :web, :users, :extra do
|
69
|
+
get :index
|
70
|
+
end
|
71
|
+
end
|
72
|
+
assert_response :success
|
73
|
+
assert_no_tag :link, :attributes => {:href => /stylesheets\/application.css/, :rel => 'stylesheet'}
|
74
|
+
assert_no_tag :link, :attributes => {:href => /stylesheets\/web.css/, :rel => 'stylesheet'}
|
75
|
+
assert_no_tag :link, :attributes => {:href => /stylesheets\/home.css/, :rel => 'stylesheet'}
|
76
|
+
assert_no_tag :link, :attributes => {:href => /stylesheets\/extra.css/, :rel => 'stylesheet'}
|
77
|
+
assert_no_tag :link, :attributes => {:href => /stylesheets\/users.css/, :rel => 'stylesheet'}
|
78
|
+
assert_tag :link, :attributes => {:href => /sweet_assets_([\w\d]{32}).css/, :rel => 'stylesheet'}
|
79
|
+
ActionController::Base.perform_caching = false
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_caching_javascripts
|
83
|
+
ActionController::Base.perform_caching = true
|
84
|
+
with_stylesheets :home, :web, :users, :extra do
|
85
|
+
with_javascripts :home, :web, :users, :extra do
|
86
|
+
get :index
|
87
|
+
end
|
88
|
+
end
|
89
|
+
assert_response :success
|
90
|
+
assert_no_tag :script, :attributes => {:src => /javascripts\/web.js/}
|
91
|
+
assert_no_tag :script, :attributes => {:src => /javascripts\/home.js/}
|
92
|
+
assert_no_tag :script, :attributes => {:src => /javascripts\/extra.js/}
|
93
|
+
assert_no_tag :script, :attributes => {:src => /javascripts\/users.js/}
|
94
|
+
assert_tag :script, :attributes => {:src => /sweet_assets_([\w\d]{32}).js/}
|
95
|
+
ActionController::Base.perform_caching = false
|
96
|
+
end
|
97
|
+
|
98
|
+
def test_defaults_should_include_prototype_scriptaculous_and_application
|
99
|
+
@controller = DefaultsController.new
|
100
|
+
get :index
|
101
|
+
assert_tag :script, :attributes => {:src => /prototype.js/}
|
102
|
+
assert_tag :script, :attributes => {:src => /effects.js/}
|
103
|
+
assert_tag :script, :attributes => {:src => /dragdrop.js/}
|
104
|
+
assert_tag :script, :attributes => {:src => /controls.js/}
|
105
|
+
assert_tag :script, :attributes => {:src => /application.js/}
|
106
|
+
end
|
107
|
+
|
108
|
+
def test_shortcuts
|
109
|
+
@controller = ShortcutController.new
|
110
|
+
with_stylesheets :home, :web, :users, :extra do
|
111
|
+
with_javascripts :home, :web, :users, :extra do
|
112
|
+
get :index
|
113
|
+
end
|
114
|
+
end
|
115
|
+
assert_tag :link, :attributes => {:href => /home.css/, :rel => 'stylesheet'}
|
116
|
+
assert_tag :link, :attributes => {:href => /users.css/, :rel => 'stylesheet'}
|
117
|
+
assert_tag :script, :attributes => {:src => /extra.js/}
|
118
|
+
assert_tag :script, :attributes => {:src => /home.js/}
|
119
|
+
@controller = WebController.new
|
120
|
+
end
|
121
|
+
|
122
|
+
def delete_empty_recent_files
|
123
|
+
dirs = ["#{RAILS_ROOT}/public/stylesheets", "#{RAILS_ROOT}/public/javascripts"]
|
124
|
+
dirs.each do |dir|
|
125
|
+
Dir.entries(dir).each do |file|
|
126
|
+
if file =~ /(\.css|\.js)$/
|
127
|
+
filename = File.join(dir, file)
|
128
|
+
if File.exists?(filename) && File.read(filename).blank? && (File.ctime(filename) + 10.minutes) > Time.now
|
129
|
+
File.delete(filename)
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
# create some temporary css files for the duration of the block
|
137
|
+
def with_stylesheets(*files, &block)
|
138
|
+
@temp_stylesheet_files = []
|
139
|
+
files.each do |file|
|
140
|
+
filename = "#{RAILS_ROOT}/public/stylesheets/#{file}.css"
|
141
|
+
unless File.exists?(filename)
|
142
|
+
@temp_stylesheet_files << "#{RAILS_ROOT}/public/stylesheets/#{file}.css"
|
143
|
+
File.open(filename, 'w') {|f| f.write('') }
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
yield
|
148
|
+
|
149
|
+
@temp_stylesheet_files.each {|filename| File.delete(filename) }
|
150
|
+
|
151
|
+
end
|
152
|
+
|
153
|
+
# create some temporary javascript files for the duration of the block
|
154
|
+
def with_javascripts(*files, &block)
|
155
|
+
@temp_javascript_files = []
|
156
|
+
files.each do |file|
|
157
|
+
filename = "#{RAILS_ROOT}/public/javascripts/#{file}.js"
|
158
|
+
unless File.exists?(filename)
|
159
|
+
@temp_javascript_files << "#{RAILS_ROOT}/public/javascripts/#{file}.js"
|
160
|
+
File.open(filename, 'w') {|f| f.write('') }
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
yield
|
165
|
+
|
166
|
+
@temp_javascript_files.each {|filename| File.delete(filename) }
|
167
|
+
|
168
|
+
end
|
169
|
+
|
170
|
+
end
|
data/uninstall.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Uninstall hook code here
|
metadata
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: JackDanger-sweet_assets
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jack Danger Canty
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-03-19 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: hoe
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.11.0
|
24
|
+
version:
|
25
|
+
description: Automate adding stylesheets and javascripts to your controller actions
|
26
|
+
email:
|
27
|
+
- sweet_assets_gem@6brand.com
|
28
|
+
executables: []
|
29
|
+
|
30
|
+
extensions: []
|
31
|
+
|
32
|
+
extra_rdoc_files:
|
33
|
+
- History.txt
|
34
|
+
- Manifest.txt
|
35
|
+
- README.txt
|
36
|
+
files:
|
37
|
+
- History.txt
|
38
|
+
- MIT-LICENSE
|
39
|
+
- Manifest.txt
|
40
|
+
- README.txt
|
41
|
+
- Rakefile
|
42
|
+
- init.rb
|
43
|
+
- install.rb
|
44
|
+
- lib/sweet_assets.rb
|
45
|
+
- rails/init.rb
|
46
|
+
- sweet_assets.gemspec
|
47
|
+
- test/sweet_assets_test.rb
|
48
|
+
- test/views/defaults/index.html.erb
|
49
|
+
- test/views/shortcut/index.html.erb
|
50
|
+
- test/views/web/index.html.erb
|
51
|
+
- uninstall.rb
|
52
|
+
has_rdoc: true
|
53
|
+
homepage: http://github.com/JackDanger/sweet_assets
|
54
|
+
post_install_message:
|
55
|
+
rdoc_options:
|
56
|
+
- --main
|
57
|
+
- README.txt
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: "0"
|
65
|
+
version:
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: "0"
|
71
|
+
version:
|
72
|
+
requirements: []
|
73
|
+
|
74
|
+
rubyforge_project: objectproxy
|
75
|
+
rubygems_version: 1.2.0
|
76
|
+
signing_key:
|
77
|
+
specification_version: 2
|
78
|
+
summary: Automate adding stylesheets and javascripts to your controller actions
|
79
|
+
test_files: []
|
80
|
+
|