Sha256: d9c5ae9e0e51c15384187233e93a354e81e670ca90a6b036ce7375764c16a6f7

Contents?: true

Size: 848 Bytes

Versions: 11

Compression:

Stored size: 848 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
	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

11 entries across 11 versions & 1 rubygems

Version Path
passive_record-0.4.14 lib/passive_record/associations/belongs_to.rb
passive_record-0.4.13 lib/passive_record/associations/belongs_to.rb
passive_record-0.4.12 lib/passive_record/associations/belongs_to.rb
passive_record-0.4.11 lib/passive_record/associations/belongs_to.rb
passive_record-0.4.10 lib/passive_record/associations/belongs_to.rb
passive_record-0.4.8 lib/passive_record/associations/belongs_to.rb
passive_record-0.4.7 lib/passive_record/associations/belongs_to.rb
passive_record-0.4.6 lib/passive_record/associations/belongs_to.rb
passive_record-0.4.5 lib/passive_record/associations/belongs_to.rb
passive_record-0.4.4 lib/passive_record/associations/belongs_to.rb
passive_record-0.4.3 lib/passive_record/associations/belongs_to.rb