<commit_before>module Queries
  module BasePathForState
    extend ArelHelpers

    def self.conflict(edition_id, state, base_path)
      return if state == "superseded"
      return if state == "unpublished" && Unpublishing.is_substitute?(edition_id)

      documents_table = Document.arel_table
      editions_table = Edition.arel_table
      unpublishings_table = Unpublishing.arel_table

      allowed_states = state == "draft" ? %w(draft) : %w(published unpublished)

      scope = editions_table
        .project(
          editions_table[:id],
          documents_table[:content_id],
          documents_table[:locale],
        )
        .where(editions_table[:id].not_eq(edition_id))
        .where(editions_table[:state].in(allowed_states))
        .where(editions_table[:base_path].eq(base_path))
        .join(documents_table).on(documents_table[:id].eq(editions_table[:document_id]))

      if %w(published unpublished).include?(state)
        unpublished_state = editions_table[:state].eq("unpublished")
        editions_join = editions_table[:id].eq(unpublishings_table[:edition_id])
        nil_unpublishing = unpublishings_table[:type].eq(nil)
        non_substitute = unpublishings_table[:type].not_eq("substitute")

        scope = scope.outer_join(unpublishings_table)
          .on(unpublished_state.and(editions_join))
          .where(nil_unpublishing.or(non_substitute))
      end

      scope = scope.order(editions_table[:created_at].desc).take(1)
      get_rows(scope).first.try(:symbolize_keys)
    end
  end
end<commit_msg>Refactor BasePathForState query<commit_after>module Queries
  module BasePathForState
    extend self

    def conflict(edition_id, state, base_path)
      return if has_no_conflicts?(state, edition_id)

      id, content_id, locale = find_conflict(
        limit_scope_to_unpublishing(
          edition_scope(edition_id, state, base_path), state
        )
      )

      { id: id, content_id: content_id, locale: locale } if id
    end

    def find_conflict(scope)
      scope
        .order(created_at: :desc)
        .pluck("editions.id", :content_id, :locale)
        .first
    end

    def has_no_conflicts?(state, edition_id)
      return true if state == "superseded"
      return true if state == "unpublished" && Unpublishing.is_substitute?(edition_id)
      false
    end

    def edition_scope(edition_id, state, base_path)
      Edition
        .with_document
        .where.not(id: edition_id)
        .where(state: allowed_states(state))
        .where(base_path: base_path)
    end

    def allowed_states(state)
      state == "draft" ? %w(draft) : %w(published unpublished)
    end

    def limit_scope_to_unpublishing(scope, state)
      return scope unless %w(published unpublished).include?(state)
      scope
        .with_unpublishing
        .where("unpublishings.type IS NULL OR unpublishings.type != 'substitute'")
    end
  end
end
{'fraction_non_alphanumeric': 0.10202679491583648, 'fraction_numerical': 0.00034352456200618345, 'mean_word_length': 3.171919770773639, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4803722', 'n_tokens_mistral': 929, 'n_tokens_neox': 903, 'n_words': 162}