Module that adds same class & instance methods

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

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:innew’
from (irb):1

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

Thanks,
–Dean

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:innew’
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

On Sep 3, 8:56 am, Mark Reginald J. [email protected]
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

Thanks Mark. Overlooked that constructor.

–Dean

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs