Sha256: e444c152df1c6c828c7395a84e90a9b8c966619bee1094989298d0f6184a4a6c

Contents?: true

Size: 975 Bytes

Versions: 1

Compression:

Stored size: 975 Bytes

Contents

module Reflections
  module Remappers
    class BelongsTo < Reflections::Remapper
      REMAPPERS << 'belongs_to'
      attr_reader :remapper

      def remap(ar_classes, &block)
        ar_classes.each do |ar_class|
          associations_for_class(ar_class).each do |association|
            foreign_key = association.foreign_key || "#{association.name}_id"
            ar_class.where(foreign_key => from_obj).each do |record|
              update_record_or_yield record, association, &block
            end
          end
        end
      end

      private

      def filter_for_class(klass)
        ->(assoc) {
          assoc.name == klass.to_s.underscore.to_sym || assoc.options[:class_name] == klass.model_name
        }
      end

      def update_record(record, association)
        record.update_attribute association.name, to_obj
      end

      def associations(ar_class)
        ar_class.reflect_on_all_associations :belongs_to
      end

    end
  end
end



Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
reflections-1.2.0 lib/reflections/remappers/belongs_to.rb