Sha256: 99f326596f3c54e23805447e9c5d6ce46102120134244621b83338af248230da

Contents?: true

Size: 617 Bytes

Versions: 22

Compression:

Stored size: 617 Bytes

Contents

module RailsCoreExtensions
  class TransferRecords
    def initialize(parent, klass, options = {})
      @parent = parent
      @klass = klass
      @options = options
    end

    def transfer_from(objects)
      record_ids = objects.flat_map { |o|
        o.send(@klass.name.underscore + '_ids')
      }
      unless record_ids.empty?
        @options[:foreign_key] ||= @parent.class.name.underscore + '_id'
        update_options = @options.except(:foreign_key)
        update_options[@options[:foreign_key]] = @parent.id
        @klass.where(id: record_ids).update_all(update_options)
      end
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
rails_core_extensions-0.15.0 lib/rails_core_extensions/transfer_records.rb
rails_core_extensions-0.14.0 lib/rails_core_extensions/transfer_records.rb
rails_core_extensions-0.13.2 lib/rails_core_extensions/transfer_records.rb
rails_core_extensions-0.13.1 lib/rails_core_extensions/transfer_records.rb
rails_core_extensions-0.13.0 lib/rails_core_extensions/transfer_records.rb
rails_core_extensions-0.12.0 lib/rails_core_extensions/transfer_records.rb
rails_core_extensions-0.11.3 lib/rails_core_extensions/transfer_records.rb
rails_core_extensions-0.11.2 lib/rails_core_extensions/transfer_records.rb
rails_core_extensions-0.11.1 lib/rails_core_extensions/transfer_records.rb
rails_core_extensions-0.11.0.beta.11 lib/rails_core_extensions/transfer_records.rb
rails_core_extensions-0.10.0 lib/rails_core_extensions/transfer_records.rb
rails_core_extensions-0.9.0 lib/rails_core_extensions/transfer_records.rb
rails_core_extensions-0.8.0 lib/rails_core_extensions/transfer_records.rb
rails_core_extensions-0.7.1 lib/rails_core_extensions/transfer_records.rb
rails_core_extensions-0.7.0 lib/rails_core_extensions/transfer_records.rb
rails_core_extensions-0.6.1 lib/rails_core_extensions/transfer_records.rb
rails_core_extensions-0.6.0 lib/rails_core_extensions/transfer_records.rb
rails_core_extensions-0.4.0 lib/rails_core_extensions/transfer_records.rb
rails_core_extensions-0.3.0 lib/rails_core_extensions/transfer_records.rb
rails_core_extensions-0.2.0 lib/rails_core_extensions/transfer_records.rb