Hello.

I wrote some code that I use to simplify including images. This  
function needs to know about engines as it is used within engine  
views, and can access images within the engine public repository.

I have made a minor modification to the Engines code.

     # Returns the Engine object for the current engine, i.e. the engine
     # in which the currently executing code lies.
     def current(c = caller)
       current_file = c[0]
       active.find do |engine|
         File.expand_path(current_file).index(File.expand_path 
(engine.root)) == 0
       end
     end

This allows me to get the current engine from functions which have  
been called

Here is the code that uses it, for completeness.

module ApplicationHelper

   DefaultIconCategory = "default"

   def icon_path(category, icon, root = '')
     File.join('', root, 'images', category.to_s, icon.to_s + '.png')
   end

   def icon_src(category, icon, root = '')
     paths = [icon_path(category, icon, root), icon_path 
(DefaultIconCategory, icon, root), icon_path(category, icon),  
icon_path(DefaultIconCategory, icon)]

     logger.info paths

     return paths.find { |path| File.exist? File.join(RAILS_ROOT,  
'public', path) }
   end

   def icon_tag(icon, options={})
     category = options.delete(:category) || controller.controller_name
     root = options.delete('root') || ''

     if defined? Engines
       engine ||= options.delete(:engine) || Engines.current(caller)  
if defined? Engines
       root = File.join(Engines.config(:public_dir), engine.name)  
unless engine.nil?
     end

     css_class = options.delete(:class) || 'icon'

     return tag('img', {'class' => css_class, 'src' => icon_src 
(category, icon, root)})
   end
end

I hope you can merge this into the main code as this is very useful,  
and i am sure it is useful to others too.

Kind regards, Samuel
_______________________________________________
engine-developers mailing list
[email protected]
http://lists.rails-engines.org/listinfo.cgi/engine-developers-rails-engines.org

Reply via email to