You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: collections_membership_actor.rb
    path of file: ./repos/hyrax/app/actors/hyrax/actors
    
    the code of the file until where you have to start completion: # frozen_string_literal: true
module Hyrax
  module Actors
    # Adds membership to and removes membership from collections.
    # This decodes parameters that follow the rails nested parameters conventions:
    #
    # @example a collections attribute hash
    #   'member_of_collections_attributes' => {
    #     '0' => { 'id' = '12312412'},
    #     '1' => { 'id' = '99981228', '_destroy' => 'true' }
    #   }
    #
    class CollectionsMembershipActor < AbstractActor
      # @param [Hyrax::Actors::Environment] env
      # @return [Boolean] true if create was successful
      def create(env)
        extract_collection_id(env)
        assign_nested_attributes_for_collection(env) && next_actor.create(env)
      end

      # @param [Hyrax::Actors::Environment] env
      # @return [Boolean] true if update was successful
      def update(env)
        assign_nested_attributes_for_collection(env) && next_actor.update(env)
      end

      private

      ##
      # Attaches any unattached members.  Deletes those that are marked _delete
      #
      # @param env [Hyrax::Actors::Enviornment]
      # @return [Boolean]
      #
      # rubocop:disable Metrics
    
      def assign_nested_attributes_for_collection(env)
        attributes_collection = env.attributes.delete(:member_of_collections_attributes)
        return true unless attributes_collection

        return false unless
          valid_membership?(env, collection_ids: attributes_collection.map { |_, attributes| attributes['id'] })

        attributes_collection = attributes_collection.sort_by { |i, _| i.to_i }.map { |_, attributes| attributes }
        # checking for existing works to avoid rewriting/loading works that are already attached
        existing_collections = env.curation_concern.member_of_collection_ids
        boolean_type_caster = ActiveModel::Type::Boolean.new
        attributes_collection.each do |attributes|
          next if attributes['id'].blank?
          if boolean_type_caster.cast(attributes['_destroy'])
            # Likely someone in the UI sought to add the collection, then
            # changed their mind and checked the "delete" checkbox and posted
            # their update.
            next unless existing_collections.include?(attributes['id'])
            remove(env.curation_concern, attributes['id'])
          else
            # Let's not try to add an item already
            next if existing_collections.include?(attributes['id'])
            add(env, attributes['id'])
          end
        end