public
Description: A Rails plugin for including JS and stylesheets in templates but ensuring they appear in the page header (in the layout).
Homepage: http://blog.withoutincident.com/
Clone URL: git://github.com/Shadowfiend/headerize.git
Refactored common code into LinkAndIndentHelpers module, added 
documentation to
the files.
shadowfiend (author)
Sat Jun 07 17:17:29 -0700 2008
commit  067804b4f8c778ab1c7d81ccb4cb825ad2b6e618
tree    a85d2433b9380519f5962f927261673d1c36db01
parent  491e068f573954116ba8b83d021d7160ff3680ac
...
 
 
1
 
 
 
 
 
 
 
2
 
 
 
 
 
 
 
3
4
5
...
7
8
9
 
 
 
10
11
12
13
14
15
16
17
 
 
18
19
20
21
22
23
24
25
26
27
28
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
...
23
24
25
26
27
28
29
 
 
 
 
 
 
 
30
31
32
 
 
 
 
 
 
 
33
34
35
0
@@ -1,5 +1,21 @@
0
+require File.dirname(__FILE__) + '/link_and_indent_helpers'
0
+
0
 module Headerize
0
+ # Includes helpers for placing javascript in the head of a page while still
0
+ # allowing the addition of scripts in any template file.
0
+ #
0
+ # See the main README for more information. +add_javascript+ is the main
0
+ # interface, used to add scripts to the list of includes anywhere. Then,
0
+ # +javascript_includes+ can be used to retrieve the full list of links to
0
+ # place in the head of the page (probably in a layout file).
0
   module JavascriptHelper
0
+ include LinkAndIndentHelpers
0
+
0
+ # Adds all given javascript files to the list of javascript include tags
0
+ # that will be returned by +javascript_includes+. See the
0
+ # +javascript_include_tag+ documentation for more on what arguments can be
0
+ # passed (an invocation to +add_javascript+ can be exactly the same as one
0
+ # to +javascript_include_tag+).
0
     def add_javascript(*args)
0
       @javascripts ||= []
0
       @javascripts << args
0
@@ -7,22 +23,13 @@ module Headerize
0
       nil
0
     end
0
 
0
+ # Returns all Javascript includes added by +add_javascript+. These includes
0
+ # are indented by 4 spaces by default. Pass the <tt>:indent</tt> option with
0
+ # a different number for a different indentation level.
0
     def javascript_includes(opts = {})
0
- return '' if @javascripts.nil?
0
-
0
- includes = @javascripts.inject('') do |str, files_and_opts|
0
- str << javascript_include_tag(*files_and_opts)
0
- end
0
-
0
- indent(includes, opts[:indent] || 4)
0
+ collect_and_indent @javascripts, :javascript_include_tag,
0
+ opts[:indent] || 4
0
     end
0
-
0
- private
0
- # Indents the given string by the given amount. Does not indent the first
0
- # line.
0
- def indent(string, amt)
0
- string.gsub /\n *([^\n]+)/m, "\n#{' ' * amt}\\1"
0
- end
0
   end
0
 end
0
 
...
 
 
1
 
 
 
 
 
 
 
2
 
 
 
 
 
 
3
4
5
...
7
8
9
 
 
 
10
11
12
13
14
15
16
17
 
 
18
19
20
21
22
23
24
25
26
27
28
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
...
22
23
24
25
26
27
28
 
 
 
 
 
 
 
29
30
31
 
 
 
 
 
 
 
32
33
34
0
@@ -1,5 +1,20 @@
0
+require File.dirname(__FILE__) + '/link_and_indent_helpers'
0
+
0
 module Headerize
0
+ # Includes helpers for placing stylesheets in the head of a page while still
0
+ # allowing the addition of stylesheets in any template file.
0
+ #
0
+ # See the main README for more information. +add_stylesheet+ is the main
0
+ # interface, used to add stylesheets to the list of links anywhere. Then,
0
+ # +stylesheet_links+ can be used to retrieve the full list of links to place
0
+ # in the head of the page (probably in a layout file).
0
   module StylesheetHelper
0
+ include LinkAndIndentHelpers
0
+
0
+ # Adds all given stylesheets to the list of stylesheet link tags that will
0
+ # be returned by +stylesheet_links+. See the +stylesheet_link_tag+
0
+ # documentation for more on what arguments can be passed (an invocation to
0
+ # +add_stylesheet+ can be exactly the same as one to +stylesheet_link_tag+).
0
     def add_stylesheet(*args)
0
       @stylesheets ||= []
0
       @stylesheets << args
0
@@ -7,22 +22,13 @@ module Headerize
0
       nil
0
     end
0
 
0
+ # Returns all stylesheet links added by +add_stylesheet+. These links are
0
+ # indented by 4 spaces by default. Pass the <tt>:indent</tt> option with a
0
+ # different number for a different indentation level.
0
     def stylesheet_links(opts = {})
0
- return '' if @stylesheets.nil?
0
-
0
- links = @stylesheets.inject('') do |str, files_and_opts|
0
- str << stylesheet_link_tag(*files_and_opts)
0
- end
0
-
0
- indent(links, opts[:indent] || 4)
0
+ collect_and_indent @stylesheets, :stylesheet_link_tag,
0
+ opts[:indent] || 4
0
     end
0
-
0
- private
0
- # Indents the given string by the given amount. Does not indent the first
0
- # line.
0
- def indent(string, amt)
0
- string.gsub /\n *([^\n]+)/m, "\n#{' ' * amt}\\1"
0
- end
0
   end
0
 end
0
 

Comments

    No one has commented yet.