# Simple act\_as template

**URL:** <https://www.ruby-forum.com/t/simple-act-as-template/57372>\
**Category:** Rails\
**Created:** [April 10, 2006, 1:57pm UTC](https://www.ruby-forum.com/t/simple-act-as-template/57372 "2006-04-10T13:57:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gazoduc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c5a1d2/32.png) [@gazoduc](https://www.ruby-forum.com/u/gazoduc)\
**Post date:** [April 10, 2006, 1:57pm UTC](https://www.ruby-forum.com/t/simple-act-as-template/57372/1 "2006-04-10T13:57:14Z")

</div>

Hello !

I posted a simple template to show how to add ‘act\_as\_whatever’ to  
ActiveRecord.  
I think this might be useful to others as there are some trickery ruby  
things in this  
file.

Here is the template : [textsnippets.com](http://textsnippets.com/posts/show/384)

Gaspard B.

---

<div class="post-metadata">

**Author:** ![gazoduc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c5a1d2/32.png) [@gazoduc](https://www.ruby-forum.com/u/gazoduc)\
**Post date:** [April 10, 2006, 2:39pm UTC](https://www.ruby-forum.com/t/simple-act-as-template/57372/2 "2006-04-10T14:39:33Z")

</div>

Any chance of quick guide to how all that works?

---

<div class="post-metadata">

**Author:** ![gazoduc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c5a1d2/32.png) [@gazoduc](https://www.ruby-forum.com/u/gazoduc)\
**Post date:** [April 11, 2006, 2:33pm UTC](https://www.ruby-forum.com/t/simple-act-as-template/57372/3 "2006-04-11T14:33:56Z")

</div>

Thank you! That’s exactly the explanation I was looking for but haven’t  
found anywhere in any tutorials!

---

<div class="post-metadata">

**Author:** ![gazoduc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c5a1d2/32.png) [@gazoduc](https://www.ruby-forum.com/u/gazoduc)\
**Post date:** [April 10, 2006, 3:12pm UTC](https://www.ruby-forum.com/t/simple-act-as-template/57372/4 "2006-04-10T15:12:32Z")

</div>

Charlie B. wrote:

> Any chance of quick guide to how all that works?

The beggining is just to set a namespace :  
module YourSuperName  
module Acts  
module Idiot

Then there is a module method that we overwrite. This method is called  
when the module is included  
into another object (‘base’ here as it will be ActiveRecord::Base) :  
def self.included(base)  
# add all methods from the module “AddActsAsMethod” to the  
‘base’ module  
base.extend AddActsAsMethod  
end

So we extend the base object with our ‘acts\_as\_idiot’ method. This adds  
‘acts\_as\_idiot’ to the class methods of ActiveRecord::Base.

Then we have the ‘acts\_as\_idiot’ method itself :  
def acts\_as\_idiot  
# BELONGS\_TO HAS\_MANY GOES HERE

```
      class_eval <<-END
        include YourSuperName::Acts::Idiot::InstanceMethods
      END
    end

```

This method makes the following lines  
class Myobj \< ActiveRecord::Base  
acts\_as\_idiot  
end

behave like  
class Myobj \< ActiveRecord::Base  
include YourSuperName::Acts::Idiot::InstanceMethods  
end

And the last tricky part :

```
  module InstanceMethods
    def self.included(aClass)
      aClass.extend ClassMethods
    end

```

This is executed when the instance methods are added to the class.  
‘Self’ here is the Myobj class, so  
extending it with aClass.extend moduleName will add the methods in the  
module to the class instead  
of the object.

The very last line  
ActiveRecord::Base.send :include, YourSuperName::Acts::Idiot

is equivalent to  
class ActiveRecord::Base  
include YourSuperName::Acts::Idiot  
end

This effectively includes our code into ActiveRecord, making  
‘acts\_as\_idiot’ available as a class method.

I hope these explanations made it clearer…

Gaspard
