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:in `new'' from (irb):1 Can anyone help me out with the module or my initialize method. Thanks, --Dean --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Mark Reginald James
2008-Sep-03 13:56 UTC
Re: Module that adds same class & instance methods
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Sep 3, 8:56 am, Mark Reginald James <m...-ejQy8aU/RJumpXlQD71PVDSf8X3wrgjD@public.gmane.org> wrote:> 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.comThanks Mark. Overlooked that constructor. --Dean --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---