Class: AnnotateRb::ModelAnnotator::AnnotationDecider

Inherits:
Object
  • Object
show all
Defined in:
lib/annotate_rb/model_annotator/annotation_decider.rb

Overview

Class that encapsulates the logic to decide whether to annotate a model file and its related files or not.

Constant Summary collapse

SKIP_ANNOTATION_PREFIX =
'# -\*- SkipSchemaAnnotations'

Instance Method Summary collapse

Constructor Details

#initialize(file, options) ⇒ AnnotationDecider

Returns a new instance of AnnotationDecider.



9
10
11
12
# File 'lib/annotate_rb/model_annotator/annotation_decider.rb', line 9

def initialize(file, options)
  @file = file
  @options = options
end

Instance Method Details

#annotate?Boolean

Returns:

  • (Boolean)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/annotate_rb/model_annotator/annotation_decider.rb', line 14

def annotate?
  return false if file_contains_skip_annotation

  begin
    klass = ModelClassGetter.call(@file, @options)

    klass_is_a_class = klass.is_a?(Class)
    # Methods such as #superclass only exist on a class. Because of how the code is structured, `klass` could be a
    #  module that does not support the #superclass method, so we want to return early.
    return false if !klass_is_a_class

    klass_inherits_active_record_base = klass < ActiveRecord::Base
    klass_is_not_abstract = klass.respond_to?(:abstract_class?) && !klass.abstract_class?
    klass_table_exists = klass.respond_to?(:table_exists?) && klass.table_exists?

    not_sure_this_conditional = (!@options[:exclude_sti_subclasses] || !(klass.superclass < ActiveRecord::Base && klass.table_name == klass.superclass.table_name))

    annotate_conditions = [
      klass_is_a_class,
      klass_inherits_active_record_base,
      not_sure_this_conditional,
      klass_is_not_abstract,
      klass_table_exists
    ]

    to_annotate = annotate_conditions.all?

    return to_annotate
  rescue BadModelFileError => e
    unless @options[:ignore_unknown_models]
      warn "Unable to process #{@file}: #{e.message}"
      warn "\t" + e.backtrace.join("\n\t") if @options[:trace]
    end
  rescue => e
    warn "Unable to process #{@file}: #{e.message}"
    warn "\t" + e.backtrace.join("\n\t") if @options[:trace]
  end

  false
end