Sha256: 92258edd9451ee920400448d717fbfe49fd753f7a0afa13c5f675286921e21c8
Contents?: true
Size: 1.25 KB
Versions: 9
Compression:
Stored size: 1.25 KB
Contents
module PassiveRecord module Associations class HasOneAssociation < Struct.new(:parent_class, :child_class_name, :child_name_sym) def to_relation(parent_model) HasOneRelation.new(self, parent_model) end def target_name_symbol child_name_sym end end class Relation < Struct.new(:association, :parent_model) def singular? true end end class HasOneRelation < Relation def lookup child_class.find_by(parent_model_id_field => parent_model.id) end def create(attrs={}) child_class.create( attrs.merge( parent_model_id_field => parent_model.id ) ) end def lookup_or_create lookup || create end def parent_model_id_field association.parent_class.name.demodulize.underscore + "_id" end def child_class # look in same module as parent... module_name = association.parent_class.name.deconstantize module_name = "Object" if module_name.empty? (module_name.constantize). const_get(association.child_class_name.singularize) end def id parent_model.id end def child_class_name child_class.name end end end end
Version data entries
9 entries across 9 versions & 1 rubygems