Class: NoBrainer::Criteria::OrderBy::IndexFinder

Inherits:
Struct
  • Object
show all
Defined in:
lib/no_brainer/criteria/order_by.rb

Instance Method Summary collapse

Instance Method Details

#could_find_index?Boolean

Returns:



65
66
67
# File 'lib/no_brainer/criteria/order_by.rb', line 65

def could_find_index?
  !!self.index_name
end

#find_indexObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/no_brainer/criteria/order_by.rb', line 79

def find_index
  return if criteria.without_index?
  return unless first_key_indexable?

  if criteria.options[:use_index] && criteria.options[:use_index] != true
    return unless first_key.to_s == criteria.options[:use_index].to_s
  end

  # We need make sure that the where index finder has been invoked, it has priority.
  # If it doesn't find anything, we are free to go with our indexes.
  if !criteria.where_indexed? || (criteria.where_index_type == :between &&
                                  first_key.to_s == criteria.where_index_name.to_s)
    self.index_name = first_key
  end
end

#first_keyObject



69
70
71
# File 'lib/no_brainer/criteria/order_by.rb', line 69

def first_key
  @first_key ||= criteria.__send__(:effective_order).to_a.first.try(:[], 0)
end

#first_key_indexable?Boolean

Returns:



73
74
75
76
77
# File 'lib/no_brainer/criteria/order_by.rb', line 73

def first_key_indexable?
  return false unless first_key.is_a?(Symbol) || first_key.is_a?(String)
  return false unless index = criteria.model.indexes[first_key.to_sym]
  return !index.multi && !index.geo
end