Sha256: a830c63a07a7e9cf6fc05fea164b84c7e673574289dec25de77fae98b796f076

Contents?: true

Size: 1.01 KB

Versions: 2

Compression:

Stored size: 1.01 KB

Contents

module ArelMysqlIndexHint
  module ActiveRecordHintMethods
    extend ActiveSupport::Concern

    included do
      alias_method_chain :build_arel, :mysql_index_hint
    end

    def mysql_index_hint_value=(value)
      @values[:mysql_index_hint] = value
    end

    def mysql_index_hint_value
      @values[:mysql_index_hint]
    end

    def hint(index_hint_by_table)
      self.mysql_index_hint_value ||= {}.with_indifferent_access
      mysql_index_hint_value.update(index_hint_by_table)
      self
    end

    def build_arel_with_mysql_index_hint
      arel = build_arel_without_mysql_index_hint

      if mysql_index_hint_value.present?
        if mysql_index_hint_value.values.any? {|i| i.is_a?(Hash) }
          index_hint_by_table = mysql_index_hint_value
        else
          index_hint_by_table = Hash.new(mysql_index_hint_value)
        end

        arel.ast.select {|i| i.is_a?(Arel::Table) }.each do |node|
          node.index_hint = index_hint_by_table[node.name]
        end
      end

      arel
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
arel-mysql-index-hint-0.1.4 lib/arel-mysql-index-hint/active_record-hint_methods.rb
arel-mysql-index-hint-0.1.3 lib/arel-mysql-index-hint/active_record-hint_methods.rb