public
Rubygem
Description: Rails Plugin - a Ruby way to manage your stylesheets and javascripts. Don't put all your assets in your layout; define what you need where you need them.
Homepage: http://6brand.com
Clone URL: git://github.com/JackDanger/sweet_assets.git
Search Repo:
added support for view helpers and refactored a bit.  Completely did away 
with the style_like_home! method_missing syntax


git-svn-id: http://svn.6brand.com/projects/plugins/sweet_assets@381 
7491b73d-821b-0410-9297-ad1f6b5b4194
studioda (author)
Sat Dec 15 15:30:20 -0800 2007
commit  cd816342befa2e136f7766cafca0efae1c9805f3
tree    4683be37430b97a4f4b374a9f42907da78b18c5b
parent  e23dc2943b447f952360504258f8d22780594e8e
...
1
2
3
 
...
1
2
3
4
0
@@ -1,4 +1,5 @@
0
 require 'sweet_assets'
0
 
0
 ActionController::Base.send :include, SweetAssets
0
+ActionView::Base.send :include, SweetAssets::AssignmentMethods
...
5
6
7
 
8
9
10
11
12
13
...
18
19
20
21
 
22
23
24
25
26
 
27
28
29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
31
32
33
34
35
36
37
 
 
38
39
40
41
...
47
48
49
50
 
51
52
53
54
 
55
56
57
...
78
79
80
 
81
82
83
...
86
87
88
 
89
90
91
...
5
6
7
8
9
10
11
12
13
14
...
19
20
21
 
22
23
24
25
26
 
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
 
 
 
 
57
58
59
60
61
62
...
68
69
70
 
71
72
73
74
 
75
76
77
78
...
99
100
101
102
103
104
105
...
108
109
110
111
112
113
114
0
@@ -5,6 +5,7 @@
0
 module SweetAssets
0
   def self.included(base)
0
     base.class_eval do
0
+ include AssignmentMethods
0
       include ClassMethods
0
       extend SweetAssetsShortcuts
0
       include AppendAssetsAfterRescue
0
0
0
0
@@ -18,23 +19,43 @@
0
   module SweetAssetsShortcuts
0
     def style_like(*assets)
0
       options = assets.extract_options!
0
- assets.each {|asset| before_filter "style_like_#{asset}".intern, options }
0
+ before_filter Proc.new {|controller| controller.style_like *assets }, options
0
     end
0
 
0
     def script_like(*assets)
0
       options = assets.extract_options!
0
- assets.each {|asset| before_filter "script_like_#{asset}".intern, options}
0
+ before_filter Proc.new {|controller| controller.script_like *assets }, options
0
     end
0
   end
0
   
0
+ module AssignmentMethods
0
+ def style_like(*styles)
0
+ styles.each do |style|
0
+ style = style.to_s
0
+ sweet_assets[:stylesheets][style.ends_with?('!') ? :bottom : :top] << style.gsub(/!$/, '')
0
+ end
0
+ end
0
+
0
+ def script_like(*scripts)
0
+ scripts.each do |script|
0
+ script = script.to_s
0
+ sweet_assets[:javascripts][script.ends_with?('!') ? :bottom : :top] << script.gsub(/!$/, '')
0
+ end
0
+ end
0
+
0
+ def sweet_assets
0
+ self.is_a?(ActionView::Base) ?
0
+ controller.instance_variable_get("@sweet_assets") :
0
+ @sweet_assets
0
+ end
0
+ end
0
+
0
   module ClassMethods
0
     
0
     def method_missing_with_sweet_assets(method_name, *args, &block)
0
       
0
- if match = method_name.to_s.match(/^style_like_(\w+)(\!)?$/)
0
- @sweet_assets[:stylesheets][match[2] ? :bottom : :top] << match[1]
0
- elsif match = method_name.to_s.match(/^script_like_(\w+)(\!)?$/)
0
- @sweet_assets[:javascripts][match[2] ? :bottom : :top] << match[1]
0
+ if match = method_name.to_s.match(/^(style_like|script_like)_(\w+!?)$/)
0
+ send match[1], match[2]
0
       else
0
         method_missing_without_sweet_assets(method_name, *args, &block)
0
       end
0
0
@@ -47,11 +68,11 @@
0
     end
0
     
0
     def style_like_current_controller
0
- @sweet_assets[:stylesheets][:bottom] << controller_name
0
+ style_like "#{controller_name}!"
0
     end
0
 
0
     def script_like_current_controller
0
- @sweet_assets[:javascripts][:bottom] << controller_name
0
+ script_like "#{controller_name}!"
0
     end
0
 
0
     def apply_sweet_assets
0
@@ -78,6 +99,7 @@
0
 
0
       def stylesheet_tags(placement)
0
         files = @assets[:stylesheets][placement].dup
0
+ files.uniq!
0
         files = files.select {|file| File.exists?("#{STYLESHEETS_DIR}/#{file}.css") } unless RAILS_ENV.eql?('test')
0
         return '' if files.blank?
0
         files << {:cache => "sweet_stylesheets_#{files.join(',')}" } if ActionController::Base.perform_caching
0
@@ -86,6 +108,7 @@
0
 
0
       def javascript_tags(placement)
0
         files = @assets[:javascripts][placement].dup
0
+ files.uniq!
0
         files = files.select {|file| File.exists?("#{JAVASCRIPTS_DIR}/#{file}.js") } unless RAILS_ENV.eql?('test')
0
         return '' if files.blank?
0
         files << {:cache => "sweet_javascripts_#{files.join(',')}" } if ActionController::Base.perform_caching
...
118
119
120
121
122
123
124
125
126
 
 
 
 
 
 
 
 
127
128
129
...
118
119
120
 
 
 
 
 
 
121
122
123
124
125
126
127
128
129
130
131
0
@@ -118,12 +118,14 @@
0
   end
0
 
0
   def delete_empty_recent_files
0
- dir = "#{RAILS_ROOT}/public/stylesheets"
0
- Dir.entries(dir).each do |file|
0
- if file =~ /.css$/
0
- filename = File.join(dir, file)
0
- if File.exists?(filename) && File.read(filename).blank? && (File.ctime(filename) + 10.minutes) > Time.now
0
- File.delete(filename)
0
+ dirs = ["#{RAILS_ROOT}/public/stylesheets", "#{RAILS_ROOT}/public/javascripts"]
0
+ dirs.each do |dir|
0
+ Dir.entries(dir).each do |file|
0
+ if file =~ /(\.css|\.js)$/
0
+ filename = File.join(dir, file)
0
+ if File.exists?(filename) && File.read(filename).blank? && (File.ctime(filename) + 10.minutes) > Time.now
0
+ File.delete(filename)
0
+ end
0
         end
0
       end
0
     end

Comments

    No one has commented yet.