Class: EagerEye::Detectors::CustomMethodQuery

Inherits:
Base
  • Object
show all
Includes:
EagerEye::Detectors::Concerns::VariableModelInference
Defined in:
lib/eager_eye/detectors/custom_method_query.rb

Constant Summary collapse

QUERY_METHODS =
%i[where find_by find_by! exists? find first last take pluck ids count sum average minimum
maximum].freeze
SAFE_QUERY_METHODS =
%i[first last take count sum find size length ids].freeze
SAFE_TRANSFORM_METHODS =
%i[keys values split [] params sort pluck ids to_s to_a to_i chars bytes].freeze
ARRAY_COLUMN_SUFFIXES =
%w[_ids _tags _types _codes _names _values _arr].freeze
ITERATION_METHODS =
%i[each map select find_all reject collect detect find_index flat_map
each_with_index each_with_object reduce inject
find_each find_in_batches in_batches array!].freeze

Constants included from EagerEye::Detectors::Concerns::VariableModelInference

EagerEye::Detectors::Concerns::VariableModelInference::PAGINATION_META_NAMES, EagerEye::Detectors::Concerns::VariableModelInference::RELATION_WRAPPERS

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

default_severity

Class Method Details

.detector_nameObject



19
20
21
# File 'lib/eager_eye/detectors/custom_method_query.rb', line 19

def self.detector_name
  :custom_method_query
end

Instance Method Details

#detect(ast, file_path, method_queries = {}, associations_by_model = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/eager_eye/detectors/custom_method_query.rb', line 23

def detect(ast, file_path, method_queries = {}, associations_by_model = {})
  return [] unless ast

  @issues = []
  @file_path = file_path
  @method_queries = method_queries
  @associations_by_model = associations_by_model
  @variable_models = {}

  find_iteration_blocks(ast) do |block_body, block_var, collection, definitions|
    model_name = infer_model_from_value(collection)
    check_block_for_query_methods(block_body, block_var, collection_is_array?(collection, definitions))
    check_block_for_model_query_methods(block_body, block_var, model_name)
  end

  @issues
end