Sha256: 8c862a8d2d3bfa9c6afb0a1cf58a3829941aa27f966e9245ee17b904ba42cc69

Contents?: true

Size: 1.09 KB

Versions: 3

Compression:

Stored size: 1.09 KB

Contents

module PassiveRecord
  module Associations
    class HasManyAssociation < Struct.new(:parent_class, :child_class_name, :children_name_sym)
      def to_relation(parent_model)
        HasManyRelation.new(self, parent_model)
      end

      def target_name_symbol
        children_name_sym
      end
    end

    class HasManyRelation < HasOneRelation
      include Enumerable
      extend Forwardable

      include PassiveRecord::ArithmeticHelpers

      def all
        child_class.where(parent_model_id_field => parent_model.id).all
      end

      def_delegators :all, :each, :last, :all?, :empty?, :sample

      def where(conditions={})
        child_class.where(conditions.merge(parent_model_id_field.to_sym => parent_model.id))
      end

      def <<(child)
        child.send(parent_model_id_field + "=", parent_model.id)
        all
      end

      def singular?
        false
      end

      def method_missing(meth,*args,&blk)
        if child_class.methods.include?(meth)
          where.send(meth,*args,&blk)
        else
          super(meth,*args,&blk)
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
passive_record-0.4.10 lib/passive_record/associations/has_many.rb
passive_record-0.4.8 lib/passive_record/associations/has_many.rb
passive_record-0.4.7 lib/passive_record/associations/has_many.rb