Sha256: a47713c38d10ed9a57b7548421718d3a36c9fe83ae4b7a7ba70ff964860a4163
Contents?: true
Size: 1.53 KB
Versions: 7
Compression:
Stored size: 1.53 KB
Contents
module PassiveRecord module Associations class HasManyThroughAssociation < Struct.new(:parent_class, :child_class_name, :target_name_symbol, :through_class, :base_association) def to_relation(parent_model) HasManyThroughRelation.new(self, parent_model) end end class HasManyThroughRelation < HasManyRelation def intermediary_relation association.base_association.to_relation(parent_model) end def results intermediary_relation.lookup end def lookup if target_sym && results results.flat_map(&target_sym) else [] end end def target_sym name_str = association.target_name_symbol.to_s singular_target_sym = name_str.singularize.to_sym plural_target_sym = name_str.pluralize.to_sym singular_class_name_sym = association.child_class_name.underscore.singularize.to_sym plural_class_name_sym = association.child_class_name.underscore.pluralize.to_sym if !results.empty? if results.first.respond_to?(singular_target_sym) singular_target_sym elsif results.first.respond_to?(plural_target_sym) plural_target_sym elsif results.first.respond_to?(singular_class_name_sym) singular_class_name_sym elsif results.first.respond_to?(plural_class_name_sym) plural_class_name_sym end end end def create(attrs={}) child_class.create(attrs) end end end end
Version data entries
7 entries across 7 versions & 1 rubygems