Factoring code out of a model but not plugin


#1

Hi,

I have been trying for hours to figure out how to factor the following
code
out of my Product model so that I can share it with other models. Since
my
method_missing method is dependent on another model a plugin would not
be
stand alone. I’ve tried to create a module and require, include, extend
Product but nothing I have tried works. Any suggestions?

Thanks!
Peter

require ‘free_form.rb’

class Product < ActiveRecord::Base
has_and_belongs_to_many :categories

belongs_to :manufacturer

has_many :variations

has_many :images,
:order => :position,
:foreign_key => ‘owner_id’,
:conditions => “owner_class = ‘#{self.to_s}’”

the following two has_and_belongs_to_many statements

create a self-referential many-to-many relationship for cross sell

products
has_and_belongs_to_many :cross_sell_products,
:join_table => ‘cross_sells’,
:foreign_key => ‘product_id’,
:association_foreign_key => ‘cross_sell_product_id’,
:class_name => ‘Product’,
:order => ‘cs_position’

has_and_belongs_to_many :cross_seller_products,
:join_table => ‘cross_sells’,
:foreign_key => ‘cross_sell_product_id’,
:association_foreign_key => ‘product_id’,
:class_name => ‘Product’

:TRICKY: pm : The next statement works because only distinguishing

virtual
attributes

are linked in the join table.

That is, the nondistinguising virtual attributes are not ‘seen’

through
the join table.
has_and_belongs_to_many :distinguishing_virtual_attributes,
:class_name => ‘VirtualAttribute’

has_and_belongs_to_many :optional_variation_groups

stuff below is for the free_form table concept

turns out it is very intertwined in the app and probably

should not be a plugin

however I would like to factor out the code below so that

it can be mixed in to any model

module_eval do

extend FreeForm

end

require ‘free_form.rb’

extend FreeForm

#module_eval do

has_many :virtual_attribute_values,

:foreign_key => ‘owner_id’,

:conditions => “owner_class = ‘#{self.to_s}’”,

:dependent => true

#end

has_many :virtual_attribute_values,
:foreign_key => ‘owner_id’,
:conditions => “owner_class = ‘#{self.to_s}’”,
:dependent => true

def method_missing(method_id, *args, &block)

begin

Try to execute the message with all superclass or module

method_missing
methods
super
rescue NoMethodError

end # rescue NoMethodError
end # method_missing

end # class Product


#2

Sorry for the double post. Please ignore this tread
-Peter