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



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/activerecord-analyze/main.rb', line 71

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