# Lib folders and application controllers

**URL:** <https://www.ruby-forum.com/t/lib-folders-and-application-controllers/85850>\
**Category:** Engines\
**Created:** [February 16, 2007, 7:47pm UTC](https://www.ruby-forum.com/t/lib-folders-and-application-controllers/85850 "2007-02-16T19:47:47Z")\
**Posts on this page:** 5\
**Page:** 2

<div class="post-metadata">

**Author:** ![treybean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/47e85d/32.png) [@treybean](https://www.ruby-forum.com/u/treybean)\
**Post date:** [February 27, 2007, 9:13pm UTC](https://www.ruby-forum.com/t/lib-folders-and-application-controllers/85850/21 "2007-02-27T21:13:30Z")

</div>

On Feb 27, 2007, at 10:39 PM, James A. wrote:

> class A
> 
> module C.
> 
> Since B explicitly refers to C, when Rails tries to reload B from  
> app/models/b.rb, it re-reads the class definition and includes C as  
> you’d expect.
> 
> The point here is that the only place where it states that A should  
> include C is a file that is only ever evaluated once, and that  
> information is lost when the target class is unloaded. This is what  
> plugin developers typically send includes to ActionController::Base,  
> not ApplicationController.

Ok, I understand that I think. The explicit include works, but  
anything that might get executed from the init.rb will never get run  
other than the first time.

So the plugin is never unloaded? If it was, the init.rb would have  
to be executed again.

But then I didn’t understand your comment about the lesson learned  
with Login+User engine. Are you saying it’s not possible/advisable  
to have engines use services of other plugins/engines? I can’t have  
a User engine that gets referenced by a Newsletter engine, for  
example, or use the Liquid or Whitelist plugins from engine code?

> Is that right?

app/models/user.rb:  
class User  
Include Monkey::UserMixin  
end

vendor/plugins/monkey/model/user.rb  
class User  
Include Monkey::UserMixin  
end

vendor/plugins/monkey/model/group.rb  
class Group  
Include Monkey::GroupMixin

```
  def test
    true
  end

```

end

vendor/plugins/monkey/lib/usermixin.rb  
module Monkey  
module UserMixin  
def self.included(base)  
base.class\_eval do  
has\_and\_belongs\_to\_many :groups

```
       def all_roles
         for group in self.groups
           result << group.all_roles
         end
       end
     end
   end
 end

```

end

vendor/plugins/monkey/lib/groupmixin.rb  
module Monkey  
module GroupMixin  
def self.included(base)  
base.class\_eval do  
has\_and\_belongs\_to\_many :users

```
       def all_roles
          return true
       end
     end
   end
 end

```

end

When I call user.all\_roles for the second time, I get the

NoMethodError (undefined method `all\_roles’ for #)

If I then do a Group.find(1) from a breakpoint, I get the

ArgumentError: A copy of ActiveRbacMixins::UserMixins::Core has been  
removed from the module tree but is still active!

Also, it’s not just methods that are mixed in that are hosed.  
Group.test also is not available, that is defined directly in the class.

Could it be that on the second time through, the app version of user  
is unloaded, and thus the corresponding mixin is unloaded. But the  
version of the model in the plugin is not unloaded, but mixin is  
removed from the module. So now the engine model is there, partially  
stripped, and gets used instead of the reloaded user model in app.  
Yes, I’m grasping at straws.

> encountered this error. Very weird.  
> directly to me. I’m very keen to resolve this, but I need to be able  
> to reproduce the behaviour…

Yes, ActiveRbac was modified to satisfy the new engine requirements  
(not using config(), removing init\_engine, etc). No other logic  
changes.

I will see if I can recreate it in toy plugin.

Thanks for your help,  
Brett

---

<div class="post-metadata">

**Author:** ![treybean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/47e85d/32.png) [@treybean](https://www.ruby-forum.com/u/treybean)\
**Post date:** [March 1, 2007, 8:58am UTC](https://www.ruby-forum.com/t/lib-folders-and-application-controllers/85850/22 "2007-03-01T08:58:33Z")

</div>

Had a day of yesterday, but what James describes, exactly happens to us

> - there is a model within a plugin
> - it has, say, method “do\_something” defined directly in the class
> - on the second request, the model class is still available but the  
> method has disappeared

One of my colleages has found a sollution, but hasn’t arived yet. I’ll  
post our findings as soon as he’s in.

Regards,  
Jeroen

James A. schreef:

---

<div class="post-metadata">

**Author:** ![treybean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/47e85d/32.png) [@treybean](https://www.ruby-forum.com/u/treybean)\
**Post date:** [March 1, 2007, 9:32am UTC](https://www.ruby-forum.com/t/lib-folders-and-application-controllers/85850/23 "2007-03-01T09:32:37Z")

</div>

Hi All,

My colleage posted an entry on my blog about this problem and I didn’t  
even notice 🙂  
[http://www.jeroenvandoorn.nl/index.php/2007/02/28/too-much-magic-will-kill-you/](http://www.jeroenvandoorn.nl/index.php/2007/02/28/too-much-magic-will-kill-you/)

Hope this helps.

Kind regards,  
Jeroen van Doorn

Jeroen van Doorn schreef:

---

<div class="post-metadata">

**Author:** ![treybean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/47e85d/32.png) [@treybean](https://www.ruby-forum.com/u/treybean)\
**Post date:** [March 5, 2007, 3:40pm UTC](https://www.ruby-forum.com/t/lib-folders-and-application-controllers/85850/24 "2007-03-05T15:40:57Z")

</div>

Oh, and of course:

before\_filter :reinit\_engines\_in\_dev

---

<div class="post-metadata">

**Author:** ![treybean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/47e85d/32.png) [@treybean](https://www.ruby-forum.com/u/treybean)\
**Post date:** [March 5, 2007, 3:39pm UTC](https://www.ruby-forum.com/t/lib-folders-and-application-controllers/85850/25 "2007-03-05T15:39:06Z")

</div>

It is perhaps a little simple-minded, but this solved the problem for  
me. In my application controller I put this:

def reinit\_engines\_in\_dev  
if ENV[‘RAILS\_ENV’] == ‘development’  
Rails.plugins.each do |plgin|  
if File.basename(plgin.root) =~ /\_engine/  
IO.read("#{RAILS\_ROOT}/#{plgin.root}/init.rb").each do |line|  
eval(line) unless (line.blank? or (line =~ /^\s\*#/))  
end  
end  
end  
end  
end

=vg

[Previous page](https://www.ruby-forum.com/t/lib-folders-and-application-controllers/85850.md?page=1)
