Sha256: 9f306e67ef28cd9708c04c3d83720f3ea1fa3cc834967ff93caee04cf388acc6
Contents?: true
Size: 1.85 KB
Versions: 3
Compression:
Stored size: 1.85 KB
Contents
module ActiveRecord::Associations class HasManyForActiveModelAssociation < HasManyAssociation # remove conditions: owner.new_record?, foreign_key_present? def find_target? !loaded? && klass end # no dependent action def null_scope? false end # not support counter_cache def empty? if loaded? size.zero? else @target.blank? && !scope.exists? end end # full replace simplely def replace(other_array) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength original_target = load_target.dup other_array.each { |val| raise_on_type_mismatch!(val) } target_ids = reflection.options[:target_ids] owner[target_ids] = other_array.map(&:id) old_records = original_target - other_array old_records.each do |record| @target.delete(record) end other_array.each do |record| if (index = @target.index(record)) @target[index] = record else @target << record end end end # no need transaction def concat(*records) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength load_target flatten_records = records.flatten flatten_records.each { |val| raise_on_type_mismatch!(val) } target_ids = reflection.options[:target_ids] owner[target_ids] ||= [] owner[target_ids].concat(flatten_records.map(&:id)) flatten_records.each do |record| if (index = @target.index(record)) @target[index] = record else @target << record end end target end private def get_records # rubocop:disable Naming/AccessorMethodName return scope.to_a if reflection.scope_chain.any?(&:any?) target_ids = reflection.options[:target_ids] klass.where(id: owner[target_ids]).to_a end end end
Version data entries
3 entries across 3 versions & 1 rubygems