Sha256: 0781b48678458c51191f5b953f5d146a3ea0f5e1a6ad89a0f2bd6b93a807a5df

Contents?: true

Size: 913 Bytes

Versions: 1

Compression:

Stored size: 913 Bytes

Contents

module PassiveRecord
  module Associations
    class BelongsToAssociation < Struct.new(:child_class, :parent_class_name, :target_name_symbol)
      def to_relation(child_model)
        BelongsToRelation.new(self, child_model)
      end

      def parent_class
        @parent_class ||= (
          module_name = child_class.name.deconstantize
          module_name = "Object" if module_name.empty?
          (module_name.constantize).const_get(parent_class_name)
        )
      end

      def child_class_name
        child_class.name
      end
    end

    class BelongsToRelation < Struct.new(:association, :child_model)
      def singular?
        true
      end

      def lookup
        association.parent_class.find_by(parent_model_id)
      end

      def parent_model_id
        @parent_model_id ||= nil
      end

      def parent_model_id=(id)
        @parent_model_id = id
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
passive_record-0.4.15 lib/passive_record/associations/belongs_to.rb