Class: ActiveRecord::Relation

Inherits:
Object
  • Object
show all
Defined in:
lib/activerecord-analyze/main.rb

Instance Method Summary collapse

Instance Method Details

#analyze(opts = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/activerecord-analyze/main.rb', line 18

def analyze(opts = {})
  res = exec_analyze(collecting_queries_for_explain { exec_queries }, opts)
  if [:json, :hash, :pretty_json].include?(opts[:format])
    start = res.index("[\n")
    finish = res.rindex("]")
    raw_json = res.slice(start, finish - start + 1)

    if opts[:format] == :json
      JSON.parse(raw_json).to_json
    elsif opts[:format] == :hash
      JSON.parse(raw_json)
    elsif opts[:format] == :pretty_json
      JSON.pretty_generate(JSON.parse(raw_json))
    end
  else
    res
  end
end