Sha256: 3aa27b08d1766c2c248c3797cef3f09e5ef6eb1563c3f95021bd4f98f1f0f876

Contents?: true

Size: 1.88 KB

Versions: 8

Compression:

Stored size: 1.88 KB

Contents

# frozen_string_literal: true

module AnnotateRb
  module ModelAnnotator
    # Class that encapsulates the logic to decide whether to annotate a model file and its related files or not.
    class AnnotationDecider
      SKIP_ANNOTATION_PREFIX = '# -\*- SkipSchemaAnnotations'

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

      def annotate?
        return false if file_contains_skip_annotation

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

          klass_is_a_class = 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?(:abstract_class) && 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

      private

      def file_contains_skip_annotation
        file_string = File.exist?(@file) ? File.read(@file) : ""

        /#{SKIP_ANNOTATION_PREFIX}.*/o.match?(file_string)
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
annotaterb-4.7.0 lib/annotate_rb/model_annotator/annotation_decider.rb
annotaterb-4.6.0 lib/annotate_rb/model_annotator/annotation_decider.rb
annotaterb-4.5.0 lib/annotate_rb/model_annotator/annotation_decider.rb
annotaterb-4.4.1 lib/annotate_rb/model_annotator/annotation_decider.rb
annotaterb-4.4.0 lib/annotate_rb/model_annotator/annotation_decider.rb
annotaterb-4.3.1 lib/annotate_rb/model_annotator/annotation_decider.rb
annotaterb-4.3.0 lib/annotate_rb/model_annotator/annotation_decider.rb
annotaterb-4.2.0 lib/annotate_rb/model_annotator/annotation_decider.rb