Sha256: f197b1a121f2a097c9301158f6aee07388a178eb1b3a1465aba3d51f81006228

Contents?: true

Size: 1.78 KB

Versions: 21

Compression:

Stored size: 1.78 KB

Contents

# frozen_string_literal: true

module ActiveRecord
  class PredicateBuilder
    class AssociationQueryValue # :nodoc:
      def initialize(associated_table, value)
        @associated_table = associated_table
        @value = value
      end

      def queries
        if associated_table.join_foreign_key.is_a?(Array)
          id_list = ids
          id_list = id_list.pluck(primary_key) if id_list.is_a?(Relation)

          id_list.map { |ids_set| associated_table.join_foreign_key.zip(ids_set).to_h }
        else
          [ associated_table.join_foreign_key => ids ]
        end
      end

      private
        attr_reader :associated_table, :value

        def ids
          case value
          when Relation
            relation = value
            relation = relation.select(primary_key) if select_clause?
            relation = relation.where(primary_type => polymorphic_name) if polymorphic_clause?
            relation
          when Array
            value.map { |v| convert_to_id(v) }
          else
            [convert_to_id(value)]
          end
        end

        def primary_key
          associated_table.join_primary_key
        end

        def primary_type
          associated_table.join_primary_type
        end

        def polymorphic_name
          associated_table.polymorphic_name_association
        end

        def select_clause?
          value.select_values.empty?
        end

        def polymorphic_clause?
          primary_type && !value.where_values_hash.has_key?(primary_type)
        end

        def convert_to_id(value)
          return primary_key.map { |pk| value.public_send(pk) } if primary_key.is_a?(Array)

          if value.respond_to?(primary_key)
            value.public_send(primary_key)
          else
            value
          end
        end
    end
  end
end

Version data entries

21 entries across 21 versions & 4 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/activerecord-7.1.3.4/lib/active_record/relation/predicate_builder/association_query_value.rb
activerecord-7.1.5.1 lib/active_record/relation/predicate_builder/association_query_value.rb
activerecord-7.1.5 lib/active_record/relation/predicate_builder/association_query_value.rb
activerecord-7.1.4.2 lib/active_record/relation/predicate_builder/association_query_value.rb
activerecord-7.1.4.1 lib/active_record/relation/predicate_builder/association_query_value.rb
activerecord-7.1.4 lib/active_record/relation/predicate_builder/association_query_value.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/activerecord-7.1.3.4/lib/active_record/relation/predicate_builder/association_query_value.rb
activerecord-7.2.0.beta3 lib/active_record/relation/predicate_builder/association_query_value.rb
tinymce-rails-7.1.2 vendor/bundle/ruby/3.3.0/gems/activerecord-7.1.3.4/lib/active_record/relation/predicate_builder/association_query_value.rb
activerecord-7.2.0.beta2 lib/active_record/relation/predicate_builder/association_query_value.rb
activerecord-7.1.3.4 lib/active_record/relation/predicate_builder/association_query_value.rb
activerecord-7.2.0.beta1 lib/active_record/relation/predicate_builder/association_query_value.rb
activerecord-7.1.3.2 lib/active_record/relation/predicate_builder/association_query_value.rb
activerecord-7.1.3.1 lib/active_record/relation/predicate_builder/association_query_value.rb
activerecord-7.1.3 lib/active_record/relation/predicate_builder/association_query_value.rb
activerecord-7.1.2 lib/active_record/relation/predicate_builder/association_query_value.rb
activerecord-7.1.1 lib/active_record/relation/predicate_builder/association_query_value.rb
activerecord-7.1.0 lib/active_record/relation/predicate_builder/association_query_value.rb
activerecord-7.1.0.rc2 lib/active_record/relation/predicate_builder/association_query_value.rb
activerecord-7.1.0.rc1 lib/active_record/relation/predicate_builder/association_query_value.rb