Class: Lazer::ScopesController

Inherits:
ApplicationController show all
Defined in:
app/controllers/lazer/scopes_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#authenticate!

Instance Method Details

#get_scopes_for_model(model) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/controllers/lazer/scopes_controller.rb', line 38

def get_scopes_for_model(model)
  return nil unless model.respond_to?(:defined_scopes)

  result = {}
  all_sql = model.all.to_sql
  model.defined_scopes.each do |name, block|
    begin

      # for each scope, check the arity. don't handle params for now
      if block.respond_to?(:arity) && block.arity != 0
        puts "skipped #{name} because arity was #{block.arity}"
        next
      end

      scope_sql = model.send(name).to_sql

      # return scopes that have a where or order only... skip joins for now
      if !scope_sql.include?(all_sql)
        puts "skipped #{name} because sql wasn't included"
        next
      end

      new_sql = scope_sql.gsub(all_sql, "")&.strip
      result[name] = new_sql
    rescue => e
      Rails.logger.warn "Failed processing scope #{name} on #{model.name}: #{e.message}"
    end
  end

  result
end

#showObject



6
7
8
# File 'app/controllers/lazer/scopes_controller.rb', line 6

def show
  render json: try_all_models.to_json
end

#try_all_modelsObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/controllers/lazer/scopes_controller.rb', line 10

def try_all_models
  Rails.application.eager_load!
  models = ActiveRecord::Base.descendants
  worked = []
  failed = []
  data = {}

  models.each do |model|
    begin
      scopes = get_scopes_for_model(model)
      if scopes.present?
        data[model.name] = {
          scopes: scopes,
          table_name: model.table_name
        }
      end
      worked << (model.respond_to?(:class_name) ? model.class_name : model.name)
    rescue => e
      failed << {
        model: (model.respond_to?(:class_name) ? model.class_name : model.name),
        error: e.message
      }
    end
  end

  { success: worked, failure: failed, data: data }
end