Class: ArQueryMatchers::Queries::LoadCounter::LoadQueryFilter

Inherits:
QueryFilter
  • Object
show all
Defined in:
lib/ar_query_matchers/queries/load_counter.rb

Constant Summary collapse

MODEL_LOAD_PATTERN =

Matches named SQL operations like the following: ‘User Load’

/\A(?<model_name>[\w:]+) (Load|Exists)\Z/
MODEL_SQL_PATTERN =

Matches unnamed SQL operations like the following: “SELECT COUNT(*) FROM ‘users` …”

/SELECT (?:(?!SELECT).)* FROM [`"](?<table_name>[^`"]+)[`"]/

Instance Method Summary collapse

Instance Method Details

#filter_map(name, sql) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ar_query_matchers/queries/load_counter.rb', line 26

def filter_map(name, sql)
  # First check for a `SELECT * FROM` query that ActiveRecord has
  # helpfully named for us in the payload
  match = name.match(MODEL_LOAD_PATTERN)
  return ModelName.new(match[:model_name]) if match

  # Fall back to pattern-matching on the table name in a COUNT and looking
  # up the table name from ActiveRecord's loaded descendants.
  select_from_table = sql.match(MODEL_SQL_PATTERN)
  TableName.new(select_from_table[:table_name]) if select_from_table
end