Module: InvalidRecordFinder::ModelPicker

Defined in:
lib/invalid_record_finder/model_picker.rb

Class Method Summary collapse

Class Method Details

.call(given_models: [], ignored_models: [], ignored_namespaces: []) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/invalid_record_finder/model_picker.rb', line 3

def self.call(given_models: [], ignored_models: [], ignored_namespaces: [])
  given_models = parse_given_models(given_models)
  return given_models if given_models.any?

  ignored_models     = to_string_array(ignored_models)
  ignored_namespaces = to_string_array(ignored_namespaces)

  Rails.configuration.cache_classes || Rails.application.eager_load!

  models = defined?(ApplicationRecord) && ApplicationRecord
    .descendants
    .reject(&:abstract_class?)
    .map(&:name)
  models.blank? && raise(Error, 'No models inherit from ApplicationRecord.')

  if ignored_namespaces.any?
    models = models.grep_v(/\A(?:#{Regexp.union(ignored_namespaces)})/)
  end
  models.empty? && raise(Error, 'All models are ignored through namespaces.')

  if ignored_models.any?
    models -= ignored_models.map(&:to_s)
  end
  models.empty? && raise(Error, 'All models are ignored.')

  models.map(&:constantize)
end

.parse_given_models(arg) ⇒ Object



31
32
33
# File 'lib/invalid_record_finder/model_picker.rb', line 31

def self.parse_given_models(arg)
  Array(arg).map { |el| el.respond_to?(:constantize) ? el.constantize : el }.compact
end

.to_string_array(arg) ⇒ Object



35
36
37
# File 'lib/invalid_record_finder/model_picker.rb', line 35

def self.to_string_array(arg)
  Array(arg).map { |obj| obj.respond_to?(:name) ? obj.name : obj&.to_s }.compact
end