Topic: Share same code with multiple models

I have multiple models that need to share the same piece of code, and I cannot find a way to DRY the whole thing. Of course I can cut and paste the same lines of code in every single model but there has to be a way to put this code in a single place and use it.
A brief description:
I have a Document model using attachment_fu for storing attachments.
In each model where i need to link an attached document (ie Offer, Product, etc) i need to put the code used to save the attachment and link it to the main module (Fat Model/Skinny Controller).
The shared code is the has_many declaration, the attr_writer definition, two callbacks (after_create and after_update) and two private methods used to save the Document after linking it to the model.

Any hint on where to put all these things?

Francesco

Re: Share same code with multiple models

something like this, though no promises that it's 100% correct

#/lib/attachment_extensions.rb

Module AttachmentExtensions
    def has_attachment_extensions
      has_many ....
      attr_writer ...
      after_create :something
      after_update :something_else
    end

    def something
      #...
    end
   
    def something_else
      #...
    end
   
   #more methods...
end

#in any model, e.g. Offer
class Offer < ActiveRecord::Base
  include AttachmentExtensions
  has_attachmend_extensions

#..your individual model code
end

Re: Share same code with multiple models

Thanks for the suggestion; i was trying to do this way, but i was missing to wrap the declarations in a def ... end block.
The other thing i do not know is that the methods have to be private, so i fear that including the private code before all the other model code, also all that follows becomes private.

I'll try in brief

Re: Share same code with multiple models

the following is really just a guess from the top of my head as i think i saw it in some plugins source code:

#/lib/attachment_extensions.rb

Module AttachmentExtensions
    def has_attachment_extensions
      has_many ....
      attr_writer ...
      after_create :something
      after_update :something_else
    end

    def something
      #...
    end
   
    def something_else
      #...
    end
   
   #more methods...

   class << self
   private
     def my_rivate_method
   
     end
   end
end


I'll see if i can find it again, as i wanted to look into that stuff again anyways ...

Re: Share same code with multiple models

Even with this setup it still says

has_attachment_extensions  no such method

when rails tries to execute the code in the model where has_attachment_extensions is invoked.

Seems that the code is not included.

I think that there are concepts that still need clarification for me, but i cannot find a good reference in order to well understand the differences: include, require, extend, load are strange beasts for me at the moment...

Re: Share same code with multiple models

That code would need to get included into the "class scope". Try calling "extend" instead of "include".