# Alternatives to mixins

**URL:** <https://www.ruby-forum.com/t/alternatives-to-mixins/161945>\
**Category:** Engines\
**Created:** [March 4, 2009, 3:58am UTC](https://www.ruby-forum.com/t/alternatives-to-mixins/161945 "2009-03-04T03:58:52Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![arjackson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/57b2e6/32.png) [@arjackson](https://www.ruby-forum.com/u/arjackson)\
**Post date:** [March 4, 2009, 3:58am UTC](https://www.ruby-forum.com/t/alternatives-to-mixins/161945/1 "2009-03-04T03:58:52Z")

</div>

Hi all,

I was reading James’ summary of engines in rails 2.3 (  
[http://rails-engines.org/news/2009/02/02/engines-in-rails-2-3/](http://rails-engines.org/news/2009/02/02/engines-in-rails-2-3/)) and at  
the  
bottom there is

“While the code mixing mechanism is quite neat, there are other ways of  
overriding the implementation of methods which are more typically  
Rubyish,  
and involve less magic. Iâ€™ll try and post some examples here soon.”

I’m wondering what those more Rubyish ways are. I rely on mixins in a  
few  
of my apps, so I’m eager to learn.

thanks,  
-Andrew R.

---

<div class="post-metadata">

**Author:** ![arjackson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/57b2e6/32.png) [@arjackson](https://www.ruby-forum.com/u/arjackson)\
**Post date:** [March 4, 2009, 4:08am UTC](https://www.ruby-forum.com/t/alternatives-to-mixins/161945/2 "2009-03-04T04:08:14Z")

</div>

I _think_ he meant that a better solution would be to do it manually,  
and  
not have the engines automagically mixin stuff for you. For example,  
instead  
of putting all of your methods right in your  
app/controllers/controller.rb  
file, put them in a module (lib/controller\_methods.rb etc.) then  
“include”  
and/or “extend” that module in your plugins controller class.

When a parent app uses the plugin, and overrides the  
app/controllers/controller.rb file, simply “include” or “extend” the  
module  
coming from the plugins’ lib/controller\_methods.rb file.

That might not be the way James meant, but it’s something I did in my  
own  
apps before I even knew about Engines. It works, and it’s very clear as  
to  
what is happening.

Hope that helps?

Matt

---

<div class="post-metadata">

**Author:** ![arjackson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/57b2e6/32.png) [@arjackson](https://www.ruby-forum.com/u/arjackson)\
**Post date:** [March 4, 2009, 4:18am UTC](https://www.ruby-forum.com/t/alternatives-to-mixins/161945/3 "2009-03-04T04:18:46Z")

</div>

Here’s an example. We have a questionnaire engine, which has models

RadioField, Selectfield \< QuestionWithOptions \< Question \< Element  
Datefield, Textarea, Textfield \< Question \< Element

Actually, it’s on github.

> **[questionnaire\_engine/app/models at 2405f96ef12aa7c6eb84ca21370660e7bfbdcaa4 ·...](https://github.com/twinge/questionnaire_engine/tree/2405f96ef12aa7c6eb84ca21370660e7bfbdcaa4/app/models)**
>
> 2405f96ef12aa7c6eb84ca21370660e7bfbdcaa4/app/models

I extend that in my application at the Element level to have tags  
(‘confidential’, ‘in\_summary\_view’, etc). Ideas how this can be done  
without mixins? Require the engines element.rb in my element.rb?

-Andrew

---

<div class="post-metadata">

**Author:** ![arjackson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/57b2e6/32.png) [@arjackson](https://www.ruby-forum.com/u/arjackson)\
**Post date:** [March 5, 2009, 3:58am UTC](https://www.ruby-forum.com/t/alternatives-to-mixins/161945/4 "2009-03-05T03:58:45Z")

</div>

Hi Andrew,

What I’d do is put all of the methods that your plugin’s classes use, in  
modules. So your Element class would look like:

class Element  
include Core::Element  
end

Your Question class:

class Question  
include Core::Element  
include Core::Question  
end

etc…, that way, your app would simply do the same, without having to  
worry  
about the same file name problem. Does that help?

Matt

---

<div class="post-metadata">

**Author:** ![arjackson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/57b2e6/32.png) [@arjackson](https://www.ruby-forum.com/u/arjackson)\
**Post date:** [March 5, 2009, 10:04am UTC](https://www.ruby-forum.com/t/alternatives-to-mixins/161945/5 "2009-03-05T10:04:26Z")

</div>

Probably the simplest way, given a my\_app/vendor/plugins/my\_plugin/app/  
models/thing.rb, is to create the file my\_app/app/models/thing.rb:

require File.join(RAILS\_ROOT, ‘vendor/plugins/my\_plugin/app/models/  
thing.rb’)

class Thing \< ActiveRecord::Base  
def override\_something  
end  
end

This is basically an equivalent of what the code-mixing was doing  
anyway.

James

---

<div class="post-metadata">

**Author:** ![arjackson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/57b2e6/32.png) [@arjackson](https://www.ruby-forum.com/u/arjackson)\
**Post date:** [March 5, 2009, 1:03pm UTC](https://www.ruby-forum.com/t/alternatives-to-mixins/161945/6 "2009-03-05T13:03:49Z")

</div>

I think it’s better using “require\_dependency” instead of “require” to  
avoid  
reloading errors in development environment, isn’t it? Or that behaviour  
changed in Rails 2.3?

---

<div class="post-metadata">

**Author:** ![arjackson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/57b2e6/32.png) [@arjackson](https://www.ruby-forum.com/u/arjackson)\
**Post date:** [March 5, 2009, 1:17pm UTC](https://www.ruby-forum.com/t/alternatives-to-mixins/161945/7 "2009-03-05T13:17:58Z")

</div>

You’re absolutely right - I meant to write that, and then completely  
forgot 🙂

---

<div class="post-metadata">

**Author:** ![arjackson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/57b2e6/32.png) [@arjackson](https://www.ruby-forum.com/u/arjackson)\
**Post date:** [March 16, 2009, 6:07pm UTC](https://www.ruby-forum.com/t/alternatives-to-mixins/161945/8 "2009-03-16T18:07:59Z")

</div>

Brian Johnson wrote:

> James A. wrote:
> 
> > You’re absolutely right - I meant to write that, and then completely  
> > forgot 🙂
> 
> Wouldn’t it also be possible to still use the engines plugin along with  
> Rails 2.3 engines for the other features it provides by using:  
> disable\_application\_view\_loading = false  
> disable\_application\_code\_loading = false  
> and leaving disable\_code\_mixing = true  
> ? Then you would still have code mixing, plugin migrations and copying  
> of asset files but Rails 2.3 would handle the load paths? Or is that a  
> bad idea?

Sorry, I had those settings reversed, should be:

disable\_application\_view\_loading = true

> disable\_application\_code\_loading = true  
> and leaving disable\_code\_mixing = false

---

<div class="post-metadata">

**Author:** ![arjackson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/57b2e6/32.png) [@arjackson](https://www.ruby-forum.com/u/arjackson)\
**Post date:** [March 16, 2009, 6:07pm UTC](https://www.ruby-forum.com/t/alternatives-to-mixins/161945/9 "2009-03-16T18:07:26Z")

</div>

James A. wrote:

> You’re absolutely right - I meant to write that, and then completely  
> forgot 🙂

Wouldn’t it also be possible to still use the engines plugin along with  
Rails 2.3 engines for the other features it provides by using:  
disable\_application\_view\_loading = false  
disable\_application\_code\_loading = false  
and leaving disable\_code\_mixing = true  
? Then you would still have code mixing, plugin migrations and copying  
of asset files but Rails 2.3 would handle the load paths? Or is that a  
bad idea?
