Sha256: b758adc2afd412f709d174b7141d63f6dd943a73851b0fbef8a45c0c784f030d

Contents?: true

Size: 1.55 KB

Versions: 1

Compression:

Stored size: 1.55 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 << "Missing model #{a.name.to_s.camelize} for association #{model.name}.belongs_to :#{a.name}"
          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.1 lib/active_record_schema_scrapper/associations.rb