# Rails acts\_as\_\* plugins, with parameters

**URL:** <https://www.ruby-forum.com/t/rails-acts-as-plugins-with-parameters/184119>\
**Category:** Rails\
**Created:** [February 25, 2010, 1:32pm UTC](https://www.ruby-forum.com/t/rails-acts-as-plugins-with-parameters/184119 "2010-02-25T13:32:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![extempore](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/6bbea6/32.png) [@extempore](https://www.ruby-forum.com/u/extempore)\
**Post date:** [February 25, 2010, 1:32pm UTC](https://www.ruby-forum.com/t/rails-acts-as-plugins-with-parameters/184119/1 "2010-02-25T13:32:26Z")

</div>

Hi all,  
I’m trying to write a plugin which will allow me to specify:  
acts\_as\_item :option=\>:value

i need to be able to access the :options=\>:value hash from instances of  
the model also.  
I’ve tried with class\_eval in the plugin, but can’t get it working.  
I’m calling acts\_as\_item(options), and that method can see the options  
passed from a model. Then inside the class\_eval, I’m adding a method  
(def self.configuration) which returns the options hash. I thought from  
an instance, I’d be able to do instance.class.configuration which would  
then return said options hash.

Any ideas?  
thanks for reading.

module Item  
#called by active record for us  
def self.included(base)  
base.extend ClassMethods  
end

```
module ClassMethods
  def acts_as_item(options = {})
    configuration = {:option=>:value}
    class_eval {
      include InstanceMethods

      def self.configuration
        puts "in def acts_as_item (#{configuration})\n\n"
      end
    }
  end
end

module InstanceMethods
  def item_name
    puts self.class.configuration
  end
end

```

end

---

<div class="post-metadata">

**Author:** ![extempore](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/6bbea6/32.png) [@extempore](https://www.ruby-forum.com/u/extempore)\
**Post date:** [February 25, 2010, 2:09pm UTC](https://www.ruby-forum.com/t/rails-acts-as-plugins-with-parameters/184119/2 "2010-02-25T14:09:08Z")

</div>

On Feb 25, 12:32 pm, Paul PH [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi all,  
> I’m trying to write a plugin which will allow me to specify:  
> acts\_as\_item :option=\>:value
> 
> i need to be able to access the :options=\>:value hash from instances of  
> the model also.  
> I’ve tried with class\_eval in the plugin, but can’t get it working.

> 

I’d probably add accessor methods for a class variable (or something  
like class\_inheriting\_accessor) and then call that accessor method  
from your acts\_as\_item method

Fred

---

<div class="post-metadata">

**Author:** ![extempore](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/6bbea6/32.png) [@extempore](https://www.ruby-forum.com/u/extempore)\
**Post date:** [February 25, 2010, 2:10pm UTC](https://www.ruby-forum.com/t/rails-acts-as-plugins-with-parameters/184119/3 "2010-02-25T14:10:53Z")

</div>

On Thu, Feb 25, 2010 at 7:32 AM, Paul PH [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> an instance, I’d be able to do instance.class.configuration which would
> 
> module ClassMethods  
> def acts\_as\_item(options = {})  
> configuration = {:option=\>:value}  
> class\_eval {  
> include InstanceMethods
> 
> ```
> def self.configuration
> 
> ```

I think you want this to be  
def configuration

Since this is in a module extended by the class.

–  
Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)

---

<div class="post-metadata">

**Author:** ![extempore](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/6bbea6/32.png) [@extempore](https://www.ruby-forum.com/u/extempore)\
**Post date:** [February 25, 2010, 3:24pm UTC](https://www.ruby-forum.com/t/rails-acts-as-plugins-with-parameters/184119/4 "2010-02-25T15:24:25Z")

</div>

Hi Rick, Fred,  
I got the behavior i was looking for by using class variables in the  
class\_eval block:  
class\_eval {  
include InstanceMethods  
@@config = options  
def conf  
@@config  
end  
}

instances can then do instance.conf and returns the options hash.  
I tried using class methods, and used def configuration, rather than def  
self., but I still couldnt’ get that working. I’m still going to try and  
get it working that way as well, as I’d like to understand it a bit  
more.

I’ve looked at the code for the acts\_as\_tree module, and they’re  
achieving this

> <https://github.com/rails/acts_as_tree/blob/master/lib/active_record/acts/tree.rb>

from line 42. the configuration hash is available from within the class  
methods (self.roots and self.root), and on line 91 there’s  
“self.class.roots” - which is what I was trying to do initially.

hrmmmm 🙂

Frederick C. wrote:

> On Feb 25, 12:32ï¿½pm, Paul PH [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Hi all,  
> > I’m trying to write a plugin which will allow me to specify:  
> > acts\_as\_item :option=\>:value
> > 
> > i need to be able to access the :options=\>:value hash from instances of  
> > the model also.  
> > I’ve tried with class\_eval in the plugin, but can’t get it working.
> 
> > 
> 
> I’d probably add accessor methods for a class variable (or something  
> like class\_inheriting\_accessor) and then call that accessor method  
> from your acts\_as\_item method
> 
> Fred

Rick Denatale wrote:

> On Thu, Feb 25, 2010 at 7:32 AM, Paul PH [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > an instance, I’d be able to do instance.class.configuration which would
> > 
> > ï¿½ ï¿½module ClassMethods  
> > ï¿½ ï¿½ ï¿½def acts\_as\_item(options = {})  
> > ï¿½ ï¿½ ï¿½ ï¿½configuration = {:option=\>:value}  
> > ï¿½ ï¿½ ï¿½ ï¿½class\_eval {  
> > ï¿½ ï¿½ ï¿½ ï¿½ ï¿½include InstanceMethods
> > 
> > ï¿½ ï¿½ ï¿½ ï¿½ ï¿½def self.configuration  
> > I think you want this to be  
> > def configuration
> 
> Since this is in a module extended by the class.
> 
> –  
> Rick DeNatale
> 
> Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
> Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
> WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
> LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)
