Sha256: 447df91af2a984335a87b0dffce751a3e219dd6d39a7a3fbb84bd05be8bb0241

Contents?: true

Size: 1.3 KB

Versions: 1

Compression:

Stored size: 1.3 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|
          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))
        end
      end
    end

    def to_a
      map {|v| v}
    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.0 lib/active_record_schema_scrapper/associations.rb