Sha256: ee6b861261f1e72f74f1768632d22897b8c26f31e00ee3d147b4d83109cfb400

Contents?: true

Size: 941 Bytes

Versions: 13

Compression:

Stored size: 941 Bytes

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

      def all
        child_class.where(parent_model_id_field => parent_model.id).all
      end
      def_delegators :all, :each

      def all?(*args)
        all.all?(*args)
      end

      def empty?
        all.empty?
      end

      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
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
passive_record-0.3.13 lib/passive_record/associations/has_many.rb
passive_record-0.3.12 lib/passive_record/associations/has_many.rb
passive_record-0.3.11 lib/passive_record/associations/has_many.rb
passive_record-0.3.10 lib/passive_record/associations/has_many.rb
passive_record-0.3.9 lib/passive_record/associations/has_many.rb
passive_record-0.3.8 lib/passive_record/associations/has_many.rb
passive_record-0.3.7 lib/passive_record/associations/has_many.rb
passive_record-0.3.6 lib/passive_record/associations/has_many.rb
passive_record-0.3.5 lib/passive_record/associations/has_many.rb
passive_record-0.3.4 lib/passive_record/associations/has_many.rb
passive_record-0.3.3 lib/passive_record/associations/has_many.rb
passive_record-0.3.2 lib/passive_record/associations/has_many.rb
passive_record-0.3.1 lib/passive_record/associations/has_many.rb