]> projects.mako.cc - selectricity-live/blob - vendor/plugins/engines/lib/engines/action_view_extensions.rb
bdfa11a03d597c1f37d67b4e5fe1e267dc19f4b4
[selectricity-live] / vendor / plugins / engines / lib / engines / action_view_extensions.rb
1 require 'fileutils'
2
3 module ::ActionView
4   class Base
5     
6     private
7       def full_template_path(template_path, extension)
8
9         unless Engines.disable_app_views_loading
10           # If the template exists in the normal application directory,
11           # return that path
12           default_template = "#{@base_path}/#{template_path}.#{extension}"
13           return default_template if File.exist?(default_template)
14         end
15
16         # Otherwise, check in the engines to see if the template can be found there.
17         # Load this in order so that more recently started Engines will take priority.
18         Engines.each(:precidence_order) do |engine|
19           site_specific_path = File.join(engine.root, 'app', 'views',  
20                                          template_path.to_s + '.' + extension.to_s)
21           return site_specific_path if File.exist?(site_specific_path)
22         end
23
24         # If it cannot be found anywhere, return the default path, where the
25         # user *should* have put it.  
26         return "#{@base_path}/#{template_path}.#{extension}" 
27       end
28   end
29
30
31   # add methods to handle including javascripts and stylesheets
32   module Helpers
33     module AssetTagHelper
34       # Returns a stylesheet link tag to the named stylesheet(s) for the given
35       # engine. A stylesheet with the same name as the engine is included automatically.
36       # If other names are supplied, those stylesheets from within the same engine
37       # will be linked too.
38       #
39       #   engine_stylesheet "my_engine" =>
40       #   <link href="/engine_files/my_engine/stylesheets/my_engine.css" media="screen" rel="Stylesheet" type="text/css" />
41       #
42       #   engine_stylesheet "my_engine", "another_file", "one_more" =>
43       #   <link href="/engine_files/my_engine/stylesheets/my_engine.css" media="screen" rel="Stylesheet" type="text/css" />
44       #   <link href="/engine_files/my_engine/stylesheets/another_file.css" media="screen" rel="Stylesheet" type="text/css" />
45       #   <link href="/engine_files/my_engine/stylesheets/one_more.css" media="screen" rel="Stylesheet" type="text/css" />
46       #
47       # Any options supplied as a Hash as the last argument will be processed as in
48       # stylesheet_link_tag.
49       #
50       def engine_stylesheet(engine_name, *sources)
51         stylesheet_link_tag(*convert_public_sources(engine_name, :stylesheet, sources))
52       end
53
54       # Returns a javascript link tag to the named stylesheet(s) for the given
55       # engine. A javascript file with the same name as the engine is included automatically.
56       # If other names are supplied, those javascript from within the same engine
57       # will be linked too.
58       #
59       #   engine_javascript "my_engine" =>
60       #   <script type="text/javascript" src="/engine_files/my_engine/javascripts/my_engine.js"></script>
61       #
62       #   engine_javascript "my_engine", "another_file", "one_more" =>
63       #   <script type="text/javascript" src="/engine_files/my_engine/javascripts/my_engine.js"></script>
64       #   <script type="text/javascript" src="/engine_files/my_engine/javascripts/another_file.js"></script>
65       #   <script type="text/javascript" src="/engine_files/my_engine/javascripts/one_more.js"></script>
66       #
67       # Any options supplied as a Hash as the last argument will be processed as in
68       # javascript_include_tag.
69       #
70       def engine_javascript(engine_name, *sources)
71         javascript_include_tag(*convert_public_sources(engine_name, :javascript, sources))       
72       end
73
74       # Returns a image tag based on the parameters passed to it
75       # Required option is option[:engine] in order to correctly idenfity the correct engine location
76       #
77       #   engine_image 'rails-engines.png', :engine => 'my_engine', :alt => 'My Engine' =>
78       #   <img src="/engine_files/my_engine/images/rails-engines.png" alt="My Engine />
79       #
80       # Any options supplied as a Hash as the last argument will be processed as in
81       # image_tag.
82       #
83       def engine_image(src, options = {})
84         return if !src
85
86         image_src = engine_image_src(src, options)
87
88         options.delete(:engine)
89
90         image_tag(image_src, options)
91       end
92
93       # Alias for engine_image
94       def engine_image_tag(src, options = {})
95         engine_image(src, options)
96       end
97
98       # Returns a path to the image stored within the engine_files
99       # Required option is option[:engine] in order to correctly idenfity the correct engine location
100       #
101       #   engine_image_src 'rails-engines.png', :engine => 'my_engine' =>
102       #   "/engine_files/my_engine/images/rails-engines.png"
103       #
104       def engine_image_src(src, options = {})
105         File.join(Engines.get(options[:engine].to_sym).public_dir, 'images', src)
106       end
107       
108       private
109         # convert the engine public file sources into actual public paths
110         # type:
111         #   :stylesheet
112         #   :javascript
113         def convert_public_sources(engine_name, type, sources)
114           options = sources.last.is_a?(Hash) ? sources.pop.stringify_keys : { }
115           new_sources = []
116         
117           case type
118             when :javascript
119               type_dir = "javascripts"
120               ext = "js"
121             when :stylesheet
122               type_dir = "stylesheets"
123               ext = "css"
124           end
125           
126           engine = Engines.get(engine_name)
127           
128           default = "#{engine.public_dir}/#{type_dir}/#{engine_name}"
129           if defined?(RAILS_ROOT) && File.exists?(File.join(RAILS_ROOT, "public", "#{default}.#{ext}"))
130             new_sources << default
131           end
132         
133           sources.each { |name| 
134             new_sources << "#{engine.public_dir}/#{type_dir}/#{name}"
135           }
136
137           new_sources << options         
138         end
139     end
140   end
141 end

Benjamin Mako Hill || Want to submit a patch?