Sha256: 6f5f8025f7dcacfbde482fd885978edc57b884f2877c1e47f3ce3dfe5d4ae905

Contents?: true

Size: 1.82 KB

Versions: 2

Compression:

Stored size: 1.82 KB

Contents

module Torque
  module PostgreSQL
    module Reflection
      class BelongsToManyReflection < ::ActiveRecord::Reflection::AssociationReflection
        def macro
          :belongs_to_many
        end

        def connected_through_array?
          true
        end

        def belongs_to?
          true
        end

        def collection?
          true
        end

        def association_class
          Associations::BelongsToManyAssociation
        end

        def foreign_key
          @foreign_key ||= options[:foreign_key] || derive_foreign_key.freeze
        end

        def association_foreign_key
          @association_foreign_key ||= foreign_key
        end

        def active_record_primary_key
          @active_record_primary_key ||= options[:primary_key] || derive_primary_key
        end

        unless PostgreSQL::AR510
          def join_keys(*)
            JoinKeys.new(join_pk, join_fk)
          end
        end

        if PostgreSQL::AR520
          def join_primary_key(*)
            active_record_primary_key
          end

          def join_foreign_key
            foreign_key
          end
        else
          def join_id_for(owner)
            owner[foreign_key]
          end
        end

        private

          unless PostgreSQL::AR520
            def join_pk(*)
              active_record_primary_key
            end

            def join_fk
              foreign_key
            end
          end

          def derive_primary_key
            klass.primary_key
          end

          def derive_foreign_key
            "#{name.to_s.singularize}_ids"
          end
      end

      ::ActiveRecord::Reflection.const_set(:BelongsToManyReflection, BelongsToManyReflection)
      ::ActiveRecord::Reflection::AssociationReflection::VALID_AUTOMATIC_INVERSE_MACROS
        .push(:belongs_to_many)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
torque-postgresql-1.1.8 lib/torque/postgresql/reflection/belongs_to_many_reflection.rb
torque-postgresql-1.1.7 lib/torque/postgresql/reflection/belongs_to_many_reflection.rb