Sha256: 738c37608c9c2f99b5d18602af09a324174f0b918ae6cc46e8dec0633a49db01

Contents?: true

Size: 1.63 KB

Versions: 1

Compression:

Stored size: 1.63 KB

Contents

class ActiveRecordSchemaScrapper
  class Associations

    def initialize(model:, types: self.class.types)
      @model = model
      @types = types
    end

    include Enumerable

    def each
      types.each do |type|
        model.reflect_on_all_associations(type).each do |a|
          begin
            hash = if a.try(:delegate_reflection)
                     { source:    a.delegate_reflection.options[:source],
                       through:   a.delegate_reflection.options[:through],
                       dependent: a.delegate_reflection.options[:dependent],
                     }
                   else
                     { source:    a.try(:delegate_reflection).try(:name),
                       through:   a.try(:through),
                       dependent: a.options[:dependent] }
                   end.merge(name:        a.name,
                             foreign_key: a.foreign_key,
                             class_name:  a.klass.name,
                             type:        type)

            yield(ActiveRecordSchemaScrapper::Association.new(hash))
          rescue NameError => e
            errors << OpenStruct.new(class_name:    model.name,
                                     message:       "Missing model #{a.name.to_s.camelize} for association #{model.name}.belongs_to :#{a.name}",
                                     original_error: e)
          end
        end
      end
    end

    def to_a
      map { |v| v }
    end

    def errors
      @errors ||= []
    end


    def self.types
      [:has_and_belongs_to_many, :belongs_to, :has_one, :has_many]
    end

    private

    attr_reader :model, :types

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
active_record_schema_scrapper-0.1.2 lib/active_record_schema_scrapper/associations.rb