# Engines.current small useful patch

**URL:** <https://www.ruby-forum.com/t/engines-current-small-useful-patch/82467>\
**Category:** Engines Development\
**Created:** [January 19, 2007, 4:30pm UTC](https://www.ruby-forum.com/t/engines-current-small-useful-patch/82467 "2007-01-19T16:30:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Samuel\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9adbd/32.png) [@Samuel\_W](https://www.ruby-forum.com/u/Samuel_W)\
**Post date:** [January 19, 2007, 4:30pm UTC](https://www.ruby-forum.com/t/engines-current-small-useful-patch/82467/1 "2007-01-19T16:30:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Samuel\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9adbd/32.png) [@Samuel\_W](https://www.ruby-forum.com/u/Samuel_W)\
**Post date:** [September 25, 2007, 10:30pm UTC](https://www.ruby-forum.com/t/engines-current-small-useful-patch/82467/2 "2007-09-25T22:30:21Z")

</div>

Hi Samuel,

While I like the ideas behind this, having view helpers which  
automatically detect which plugin to load content from could be  
problematic. For instance, say you want to override your plugin icon  
with one from your application? With your current icon\_tag method,  
this would not be possible, as I understand it.

In this case, I think a little bit more work by the plugin developer  
(to specify their plugin name in view helper calls) pays dividends by  
making it simpler to track down where the asset originates from.

- James

---

<div class="post-metadata">

**Author:** ![Samuel\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9adbd/32.png) [@Samuel\_W](https://www.ruby-forum.com/u/Samuel_W)\
**Post date:** [September 25, 2007, 10:34pm UTC](https://www.ruby-forum.com/t/engines-current-small-useful-patch/82467/3 "2007-09-25T22:34:31Z")

</div>

I don’t really think your answer is very well thought out.

The image\_tag call is in many places, multiple times per view. Why  
make the call engine specific when it can be introspected? Otherwise,  
you make your views non-portable between engines and any other location.

If you look at the code you can see it is easily possible to override  
icons, but the order is very specific. Multiple directories are  
checked for icons.

My solution means that ultimately you end up repeating yourself less  
and don’t have to write engine-specific views, which is exactly what  
I want.

As long as the code works as expected, it is very simple to track  
down which assets are being used. Writing it explicitly in code is  
exactly what this function avoids.

Samuel
