Sha256: 60cd8416681d477fa27d6fec4784b47a0ce34157b5dc01bc51be0c43b2468e52

Contents?: true

Size: 1.91 KB

Versions: 38

Compression:

Stored size: 1.91 KB

Contents

require 'active_type/nested_attributes/association'

module ActiveType

  module NestedAttributes

    class NestsManyAssociation < Association

      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

      def add_child(parent, child)
        add_children(parent, [child])
      end

      def add_children(parent, children)
        parent[@target_name] = assigned_children(parent) + children
      end

      def assign_children(parent, children)
        parent[@target_name] = children
      end

      def derive_class_name
        @target_name.to_s.classify
      end

      def valid_options
        super + [:index_errors]
      end

    end

  end

end

Version data entries

38 entries across 38 versions & 1 rubygems

Version Path
active_type-2.6.1 lib/active_type/nested_attributes/nests_many_association.rb
active_type-2.6.0 lib/active_type/nested_attributes/nests_many_association.rb
active_type-2.5.1 lib/active_type/nested_attributes/nests_many_association.rb
active_type-2.5.0 lib/active_type/nested_attributes/nests_many_association.rb
active_type-2.4.1 lib/active_type/nested_attributes/nests_many_association.rb
active_type-2.4.0 lib/active_type/nested_attributes/nests_many_association.rb
active_type-2.3.4 lib/active_type/nested_attributes/nests_many_association.rb
active_type-2.3.3 lib/active_type/nested_attributes/nests_many_association.rb
active_type-2.3.2 lib/active_type/nested_attributes/nests_many_association.rb
active_type-2.3.1 lib/active_type/nested_attributes/nests_many_association.rb
active_type-2.3.0 lib/active_type/nested_attributes/nests_many_association.rb
active_type-2.2.0 lib/active_type/nested_attributes/nests_many_association.rb
active_type-2.1.2 lib/active_type/nested_attributes/nests_many_association.rb
active_type-2.1.1 lib/active_type/nested_attributes/nests_many_association.rb
active_type-2.1.0 lib/active_type/nested_attributes/nests_many_association.rb
active_type-2.0 lib/active_type/nested_attributes/nests_many_association.rb
active_type-1.10.1 lib/active_type/nested_attributes/nests_many_association.rb
active_type-1.10.0 lib/active_type/nested_attributes/nests_many_association.rb
active_type-1.9.1 lib/active_type/nested_attributes/nests_many_association.rb
active_type-1.9.0 lib/active_type/nested_attributes/nests_many_association.rb