class ActiveType::NestedAttributes::NestsManyAssociation

Public Instance Methods

assign_attributes(parent, attributes_collection) click to toggle source
# File lib/active_type/nested_attributes/nests_many_association.rb, line 9
def assign_attributes(parent, attributes_collection)
  return if attributes_collection.nil?

  unless attributes_collection.is_a?(Hash) || attributes_collection.is_a?(Array)
    raise ArgumentError, "Hash or Array expected, got #{attributes_collection.class.name} (#{attributes_collection.inspect})"
  end

  new_records = []

  if attributes_collection.is_a?(Hash)
    keys = attributes_collection.keys
    attributes_collection = if keys.include?('id') || keys.include?(:id)
      Array.wrap(attributes_collection)
    else
      attributes_collection.sort_by { |i, _| i.to_i }.map { |_, attributes| attributes }
    end
  end

  attributes_collection.each do |attributes|
    attributes = attributes.with_indifferent_access
    next if reject?(parent, attributes)

    destroy = truthy?(attributes.delete(:_destroy)) && @allow_destroy

    if id = attributes.delete(:id)
      child = fetch_child(parent, id)
      if destroy
        child.mark_for_destruction
      else
        child.attributes = attributes
      end
    elsif !destroy
      new_records << build_child(parent, attributes)
    end
  end

  add_children(parent, new_records)
end

Private Instance Methods

add_child(parent, child) click to toggle source
# File lib/active_type/nested_attributes/nests_many_association.rb, line 51
def add_child(parent, child)
  add_children(parent, [child])
end
add_children(parent, children) click to toggle source
# File lib/active_type/nested_attributes/nests_many_association.rb, line 55
def add_children(parent, children)
  parent[@target_name] = assigned_children(parent) + children
end
assign_children(parent, children) click to toggle source
# File lib/active_type/nested_attributes/nests_many_association.rb, line 59
def assign_children(parent, children)
  parent[@target_name] = children
end
derive_class_name() click to toggle source
# File lib/active_type/nested_attributes/nests_many_association.rb, line 63
def derive_class_name
  @target_name.to_s.classify
end
valid_options() click to toggle source
# File lib/active_type/nested_attributes/nests_many_association.rb, line 67
def valid_options
  super + [:index_errors]
end