# Module that adds same class & instance methods

**URL:** <https://www.ruby-forum.com/t/module-that-adds-same-class-instance-methods/147528>\
**Category:** Rails\
**Created:** [September 2, 2008, 9:03pm UTC](https://www.ruby-forum.com/t/module-that-adds-same-class-instance-methods/147528 "2008-09-02T21:03:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3be4f8/32.png) [@dean](https://www.ruby-forum.com/u/dean)\
**Post date:** [September 2, 2008, 9:03pm UTC](https://www.ruby-forum.com/t/module-that-adds-same-class-instance-methods/147528/1 "2008-09-02T21:03:29Z")

</div>

I am writing a plugin that adds similar methods to a class and  
instances of that class. It is mostly a convenience and exercise to  
me.

The idea behind the module is to tell the model what the name of its  
icon should be. This way I can call

class MyModel \<\< AR::Base  
has\_icons “flower”  
end

\<%= image\_tag “/images/#{@my\_model.icon\_edit}.png” %\>

and get an image tag for /images/flower\_edit.png

Sometimes I don’t have an instance of MyModel so I want to be able to  
call EG: MyModel.icon\_add

The code is here

[http://pastie.org/264686](http://pastie.org/264686)

but there is something wrong when I use it in conjunction with  
RedCloth before\_save pre-processing.

class Script \< ActiveRecord::Base

before\_save :transform\_intro

has\_icons

private

def transform\_intro  
unless intro\_plain.blank?  
self.intro = RedCloth.new(intro\_plain, “filter\_html” ).to\_html  
end  
end

end

0 malfunction % ./script/console  
Loading development environment (Rails 2.1.0)

> > Script.icon\_add  
> > =\> “script\_add”

> > s = Script.new  
> > =\> #\<Script id: nil, creator: “”, codebase: “”, intro\_plain: nil,  
> > intro: nil, created\_at: nil, updated\_at: nil\>

> > s.icon\_add  
> > =\> “script\_add”

> > s = Script.new( :intro\_plain =\> “Hi there” )  
> > ArgumentError: wrong number of arguments (1 for 0)  
> > from (irb):1:in `initialize' from (irb):1:in `new’  
> > from (irb):1

Can anyone help me out with the module or my initialize method.

Thanks,  
–Dean

---

<div class="post-metadata">

**Author:** ![dean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3be4f8/32.png) [@dean](https://www.ruby-forum.com/u/dean)\
**Post date:** [September 3, 2008, 5:04pm UTC](https://www.ruby-forum.com/t/module-that-adds-same-class-instance-methods/147528/2 "2008-09-03T17:04:52Z")

</div>

Dean wrote:

> > > s = Script.new( :intro\_plain =\> “Hi there” )  
> > > ArgumentError: wrong number of arguments (1 for 0)  
> > > from (irb):1:in `initialize' from (irb):1:in `new’  
> > > from (irb):1
> 
> Can anyone help me out with the module or my initialize method.

All you should need is:

def initialize(attributes = nil)  
super  
# Add SharedMethods to the instance  
self.extend HasIcons::SharedMethods  
end

–  
Rails Wheels - Find Plugins, List & Sell Plugins -  
[http://railswheels.com](http://railswheels.com)

---

<div class="post-metadata">

**Author:** ![dean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3be4f8/32.png) [@dean](https://www.ruby-forum.com/u/dean)\
**Post date:** [September 4, 2008, 1:54pm UTC](https://www.ruby-forum.com/t/module-that-adds-same-class-instance-methods/147528/3 "2008-09-04T13:54:38Z")

</div>

On Sep 3, 8:56 am, Mark Reginald J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> def initialize(attributes = nil)  
> super  
> # Add SharedMethods to the instance  
> self.extend HasIcons::SharedMethods  
> end
> 
> –  
> Rails Wheels - Find Plugins, List & Sell Plugins -[http://railswheels.com](http://railswheels.com)

Thanks Mark. Overlooked that constructor.

–Dean
