Sha256: ba93bd34e6e02822a54806372294f729cddd8364b4fdfabcfff3ce5ab82f6f63

Contents?: true

Size: 1.7 KB

Versions: 1

Compression:

Stored size: 1.7 KB

Contents

module ActsAsJoinable
  
  def self.models
    @models ||= Dir[Rails.root + "/app/models/*.rb"].collect { |f| File.basename f, '.rb' }
  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
          has_many :child_relationships,  :class_name => 'ActsAsJoinable::Relationship', :as => :parent
          
          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__)}/acts_as_joinable/*"].each { |c| require c if File.extname(c) == ".rb" }

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
acts-as-joinable-0.0.1.6 lib/acts-as-joinable.rb