Sha256: 1f4698fb0ddcfcaea590e41b9ff670c565460484eaae9c21eabe5cb15292d441

Contents?: true

Size: 1.76 KB

Versions: 23

Compression:

Stored size: 1.76 KB

Contents

# frozen_string_literal: true

module Torque
  module PostgreSQL
    module Reflection
      module AssociationReflection

        def initialize(name, scope, options, active_record)
          super

          raise ArgumentError, <<-MSG.squish if options[:array] && options[:polymorphic]
            Associations can't be connected through an array at the same time they are
            polymorphic. Please choose one of the options.
          MSG
        end

        private

          # Check if the foreign key should be pluralized
          def derive_foreign_key
            result = super
            result = ActiveSupport::Inflector.pluralize(result) \
              if collection? && connected_through_array?
            result
          end

          # returns either +nil+ or the inverse association name that it finds.
          def automatic_inverse_of
            return super unless connected_through_array?

            if can_find_inverse_of_automatically?(self)
              inverse_name = options[:as] || active_record.name.demodulize
              inverse_name = ActiveSupport::Inflector.underscore(inverse_name)
              inverse_name = ActiveSupport::Inflector.pluralize(inverse_name)
              inverse_name = inverse_name.to_sym

              begin
                reflection = klass._reflect_on_association(inverse_name)
              rescue NameError
                # Give up: we couldn't compute the klass type so we won't be able
                # to find any associations either.
                reflection = false
              end

              return inverse_name if valid_inverse_reflection?(reflection)
            end
          end

      end

      ::ActiveRecord::Reflection::AssociationReflection.prepend(AssociationReflection)
    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
torque-postgresql-2.4.5 lib/torque/postgresql/reflection/association_reflection.rb
torque-postgresql-2.4.4 lib/torque/postgresql/reflection/association_reflection.rb
torque-postgresql-2.4.3 lib/torque/postgresql/reflection/association_reflection.rb
torque-postgresql-3.2.2 lib/torque/postgresql/reflection/association_reflection.rb
torque-postgresql-2.4.2 lib/torque/postgresql/reflection/association_reflection.rb
torque-postgresql-2.4.1 lib/torque/postgresql/reflection/association_reflection.rb
torque-postgresql-3.2.1 lib/torque/postgresql/reflection/association_reflection.rb
torque-postgresql-2.4.0 lib/torque/postgresql/reflection/association_reflection.rb
torque-postgresql-3.2.0 lib/torque/postgresql/reflection/association_reflection.rb
torque-postgresql-3.1.0 lib/torque/postgresql/reflection/association_reflection.rb
torque-postgresql-2.3.0 lib/torque/postgresql/reflection/association_reflection.rb
torque-postgresql-3.0.1 lib/torque/postgresql/reflection/association_reflection.rb
torque-postgresql-2.2.4 lib/torque/postgresql/reflection/association_reflection.rb
torque-postgresql-3.0.0 lib/torque/postgresql/reflection/association_reflection.rb
torque-postgresql-2.2.3 lib/torque/postgresql/reflection/association_reflection.rb
torque-postgresql-2.2.2 lib/torque/postgresql/reflection/association_reflection.rb
torque-postgresql-2.2.1 lib/torque/postgresql/reflection/association_reflection.rb
torque-postgresql-2.2.0 lib/torque/postgresql/reflection/association_reflection.rb
torque-postgresql-2.1.3 lib/torque/postgresql/reflection/association_reflection.rb
torque-postgresql-2.1.2 lib/torque/postgresql/reflection/association_reflection.rb