Ok, so, I have a method that I need to use in several models and two controllers. I don''t want to duplicate code, and initially, rail''s concept of a helper seemed like a good fit, but it looks like maybe it only works with controllers, so maybe not. I''m a pretty much a nuby, but I think maybe I want to do a mixin? If so, where would the module with my method go within the rails application folder structure? And how do I do a mixin? -- Bob Aman
On Tue, 29 Mar 2005 19:42:22 -0500, Bob Aman <vacindak-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Ok, so, I have a method that I need to use in several models and two > controllers. I don''t want to duplicate code, and initially, rail''s > concept of a helper seemed like a good fit, but it looks like maybe it > only works with controllers, so maybe not. I''m a pretty much a nuby, > but I think maybe I want to do a mixin? If so, where would the module > with my method go within the rails application folder structure? And > how do I do a mixin? > -- > Bob AmanHi Bob I would suggest creating a module in the ''lib'' directory. For example, you create a file "testmodule.rb" in the lib directory. In your model file, you would do: require_dependency "testmodule" class MyModule < ActiveRecord::Base include TestModule ... end Hope that helps, Dave -- Dave Goodlad dgoodlad-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org or dave-eHfbeeWWzZOw5LPnMra/2Q@public.gmane.org http://david.goodlad.ca/
> Hi Bob > > I would suggest creating a module in the ''lib'' directory. For > example, you create a file "testmodule.rb" in the lib directory. In > your model file, you would do: > > require_dependency "testmodule" > > class MyModule < ActiveRecord::Base > include TestModule > > ... > end > > Hope that helps, > DaveHmm, ok, what if the method being mixed in is a class method? It seems to give me a method missing exception. -- Bob Aman
On Tuesday 29 March 2005 22:51, Bob Aman wrote:> Hmm, ok, what if the method being mixed in is a class method? It > seems to give me a method missing exception.A module''s methods can be added to a class using SomeClass.extend. For example module ValidationsLayer def field(name, *args={}) options = args.last.kind_of?(Hash) ? args.pop : {} args.each {|name| options[name] = true} validates_uniqueness_of(name, :allow_nil => true) if options[:unique] validates_numericality_of(name, :allow_nil => true) if options[:number] validates_length_of(name, options[:length].merge(:allow_nil => true)) \ if options[:length] validates_associated(name, :allow_nil => true) if options[:validate] validates_presence_of(name) if options[:required] end def at_least(n) {:minimum => n} end def at_most(n) {:maximum => n} end def exactly(n) {:is => n} end def within(a, b=nil) a = (a..b) if b {:within => a} end end class User extend ValidationsLayer field :login, :required, :unique, :length => at_most(64) field :email, :required, :unique, :length => within(8..255) field :age, :required, :number end -- Nicholas Seckar aka. Ulysses
> A module''s methods can be added to a class using SomeClass.extend. For exampleWhat if I have both class and instance methods in my module? -- Bob Aman
On Wednesday 30 March 2005 00:11, Bob Aman wrote:> What if I have both class and instance methods in my module?As was documented in another thread, you will often see this snipped in Rails code: module SomeModule def append_features(base) # this is called when a class include''s a module super(base) # do the typical behavior, base.extend(ClassMethods) # automatically extend the classmethods end module ClassMethods def some_class_method end end def some_instance_method end end In a future release, modules will automatically perform the extend if ClassMethods is present. So, the code then will be module SomeModule instance methods here module ClassMethods ... end end -- Nicholas Seckar aka. Ulysses