Class: BlastRadius::Analyzer

Inherits:
Object
  • Object
show all
Defined in:
lib/blast_radius/analyzer.rb

Instance Method Summary collapse

Instance Method Details

#all_modelsArray<Class>

Get all models in Rails application

Returns:

  • (Array<Class>)

    array of ActiveRecord model classes



30
31
32
33
34
35
36
37
# File 'lib/blast_radius/analyzer.rb', line 30

def all_models
  if defined?(Rails)
    Rails.application.eager_load!
    ApplicationRecord.descendants
  else
    []
  end
end

#analyze(model_class) ⇒ Array<Hash>

Analyze model associations and extract those with dependent options

Parameters:

  • model_class (Class)

    ActiveRecord model class

Returns:

  • (Array<Hash>)

    array of association information

    • :name [Symbol] association name
    • :type [Symbol] :has_many, :has_one, :belongs_to
    • :dependent [Symbol] :destroy, :delete_all, etc.
    • :class_name [String] associated model name
    • :through [Symbol, nil] through association name if applicable


14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/blast_radius/analyzer.rb', line 14

def analyze(model_class)
  return [] unless valid_model?(model_class)

  associations = []

  model_class.reflect_on_all_associations.each do |reflection|
    next unless has_dependent_option?(reflection)

    associations << build_association_info(reflection)
  end

  associations
end