class ManualDocumentAttachmentsController < ApplicationController
  def new
    manual, document, attachment = services.new(self).call

    render(:new, locals: {
      manual: ManualViewAdapter.new(manual),
      document: ManualDocumentViewAdapter.new(manual, document),
      attachment: attachment,
    })
  end

  def create
    manual, document, attachment = services.create(self).call

    redirect_to edit_manual_document_path(manual, document)
  end

  def edit
    manual, document, attachment = services.show(self).call

    render(:edit, locals: {
      manual: ManualViewAdapter.new(manual),
      document: ManualDocumentViewAdapter.new(manual, document),
      attachment: attachment,
    })
  end

  def update
    manual, document, attachment = services.update(self).call

    if attachment.persisted?
      redirect_to(edit_manual_document_path(manual, document))
    else
      render(:edit, locals: {
        manual: ManualViewAdapter.new(manual),
        document: ManualDocumentViewAdapter.new(manual, document),
        attachment: attachment,
      })
    end
  end

private
  def services
    OrganisationalManualDocumentAttachmentServiceRegistry.new(
      organisation_slug: current_organisation_slug,
    )
  end
end

class ManualDocumentAttachmentsController < ApplicationController
  def new
    manual, document, attachment = services.new(self).call

    render(:new, locals: {
      manual: ManualViewAdapter.new(manual),
      document: ManualDocumentViewAdapter.new(manual, document),
      attachment: attachment,
    })
  end

  def create
    manual, document, attachment = services.create(self).call

    redirect_to edit_manual_document_path(manual, document)
  end

  def edit
    manual, document, attachment = services.show(self).call

    render(:edit, locals: {
      manual: ManualViewAdapter.new(manual),
      document: ManualDocumentViewAdapter.new(manual, document),
      attachment: attachment,
    })
  end

  def update
    manual, document, attachment = services.update(self).call

    if attachment.persisted?
      redirect_to(edit_manual_document_path(manual, document))
    else
      render(:edit, locals: {
        manual: ManualViewAdapter.new(manual),
        document: ManualDocumentViewAdapter.new(manual, document),
        attachment: attachment,
      })
    end
  end

private
  def services
    if current_user_is_gds_editor?
      gds_editor_services
    else
      organisational_services
    end
  end

  def gds_editor_services
    ManualDocumentAttachmentServiceRegistry.new
  end

  def organisational_services
    OrganisationalManualDocumentAttachmentServiceRegistry.new(
      organisation_slug: current_organisation_slug,
    )
  end
end

Use separate services for GDS/non-GDS editors
Use separate services for GDS/non-GDS editors

When the User comes to add an attachment, if they were a GDS editor
editing a non GDS manual then this would come back with a "Manual not
found". This commit fixes that by using a new registry which isn't
scoped to an organisation for GDS editors.

This approach introduces some duplication. This is intended as it keeps
the attachment code similar to the code that deals with manuals. The
hope this is that this duplication can be removed more easily at a
later date.

Ticket:
https://trello.com/c/yo9nTcYO/85-attachments-can-t-be-added-to-manual-sections
