module Whitehall
  class AdminLinkLookup
    include Govspeak::EmbeddedContentPatterns

    def self.find_edition(admin_url)
      case admin_url
      when ADMIN_ORGANISATION_CIP_PATH
        organisation = Organisation.find_by(slug: $1)
        corporate_info_page(organisation: organisation, corporate_info_slug: $2)
      when ADMIN_WORLDWIDE_ORGANISATION_CIP_PATH
        organisation = WorldwideOrganisation.find_by(slug: $1)
        corporate_info_page(organisation: organisation, corporate_info_slug: $2)
      when ADMIN_EDITION_PATH
        Edition.unscoped.find_by(id: $1)
      end
    end

    def self.corporate_info_page(organisation:, corporate_info_slug:)
      organisation.corporate_information_pages.find(corporate_info_slug)
    end

    private_class_method :corporate_info_page
  end
end

module Whitehall
  class AdminLinkLookup
    include Govspeak::EmbeddedContentPatterns

    def self.find_edition(admin_url)
      case admin_url
      when ADMIN_ORGANISATION_CIP_PATH
        organisation = Organisation.find_by(slug: $1)
        corporate_info_page(organisation: organisation, corporate_info_slug: $2)
      when ADMIN_WORLDWIDE_ORGANISATION_CIP_PATH
        organisation = WorldwideOrganisation.find_by(slug: $1)
        corporate_info_page(organisation: organisation, corporate_info_slug: $2)
      when ADMIN_EDITION_PATH
        Edition.unscoped.find_by(id: $1)
      end
    end

    def self.corporate_info_page(organisation:, corporate_info_slug:)
      return nil unless organisation
      organisation.corporate_information_pages.find(corporate_info_slug)
    end

    private_class_method :corporate_info_page
  end
end

Return nil if the organisation link doesn't exist
Return nil if the organisation link doesn't exist

We expect it to return nil if the organisation doesn't exist, but
this isn't the case because we try and call corporate_info_page
with a nil organisation which fails.
