module ActsAsJoinable def self.models unless @models if defined?(Rails) @models = Dir[Dir.pwd + "/app/models/*.rb"].collect { |f| File.basename f, '.rb' } else @models = [] end end @models end def self.models=(value) @models = value end def self.included(base) base.extend ClassMethods end module ClassMethods def joinable? false end # the parent in the relationship, so to speak def acts_as_joinable_on(*args, &block) if joinable? write_inheritable_attribute(:acts_as_joinable_config, args) else write_inheritable_attribute(:acts_as_joinable_config, args) class_inheritable_reader(:acts_as_joinable_config) class_eval do has_many :parent_relationships, :class_name => 'ActsAsJoinable::Relationship', :as => :child, :dependent => :destroy has_many :child_relationships, :class_name => 'ActsAsJoinable::Relationship', :as => :parent, :dependent => :destroy def self.joinable? true end include ActsAsJoinable::Core end end end # the child in the relationship, so to speak def acts_as_joinable(*args, &block) acts_as_joinable_on(*args, &block) end def acts_as_relationship belongs_to :parent, :polymorphic => true belongs_to :child, :polymorphic => true ActsAsJoinable.models.each do |m| belongs_to "parent_#{m}".intern, :foreign_key => 'parent_id', :class_name => m.camelize belongs_to "child_#{m}".intern, :foreign_key => 'child_id', :class_name => m.camelize end end end end ActiveRecord::Base.send(:include, ActsAsJoinable) if defined?(ActiveRecord::Base) Dir["#{File.dirname(__FILE__)}/../app/models/*"].each { |c| require c if File.extname(c) == ".rb" } Dir["#{File.dirname(__FILE__)}/acts_as_joinable/*"].each { |c| require c if File.extname(c) == ".rb" }