<commit_before>class Admin::EditionsController < Admin::BaseController
  before_filter :remove_blank_parameters
  before_filter :clear_scheduled_publication_if_not_activated, only: [:create, :update]
  before_filter :find_edition, only: [:show, :edit, :update, :submit, :revise, :reject, :destroy, :confirm_unpublish]
  before_filter :limit_edition_access!, only: [:show, :edit, :update, :submit, :revise, :reject, :destroy, :confirm_unpublish]
  before_filter :prevent_modification_of_unmodifiable_edition, only: [:edit, :update]
  before_filter :default_arrays_of_ids_to_empty, only: [:update]
  before_filter :delete_absent_edition_organisations, only: [:create, :update]
  before_filter :build_edition, only: [:new, :create]
  before_filter :build_edition_organisations, only: [:new, :edit]
  before_filter :detect_other_active_editors, only: [:edit]
  before_filter :redirect_to_controller_for_type, only: [:show]
  before_filter :set_default_world_locations, only: :index

  def index
    if filter && filter.valid?
      session[:document_filters] = params_filters
      render :index
    elsif session_filters.any?
      redirect_to session_filters
    else
      redirect_to default_filters
    end
  rescue ActionController::RoutingError
    redirect_to state: :draft
  end

  def show
  end

  def new
  end

  def create
    if @edition.save
      redirect_to admin_edition_path(@edition), notice: "The document has been saved"
    else
      flash.now[:alert] = "There are some problems with the document"
      extract_edition_information_from_errors
      build_edition_dependencies
      render action: "new"
    end
  end

  def edit
    @edition.open_for_editing_as(current_user)
    render :edit
  end

  def update
    if @edition.edit_as(current_user, params[:edition])
      if params[:speed_save_convert]
        @edition.convert_to_draft!
        next_edition = EditionFilter.new(edition_class, current_user, session_filters.merge(state: :imported)).editions.first
        if next_edition
          redirect_to admin_edition_path(next_edition)
        else
          redirect_to admin_editions_path(session_filters.merge(state: :imported))
        end
      else
        redirect_to admin_edition_path(@edition), notice: "The document has been saved"
      end
    else
      flash.now[:alert] = "There are some problems with the document"
      extract_edition_information_from_errors
      build_edition_dependencies
      render action: "edit"
    end
  rescue ActiveRecord::StaleObjectError
    flash.now[:alert] = "This document has been saved since you opened it"
    @conflicting_edition = Edition.find(params[:id])
    @edition.lock_version = @conflicting_edition.lock_version
    build_edition_dependencies
    render action: "edit"
  end

  def revise
    edition = @edition.create_draft(current_user)
    if edition.persisted?
      redirect_to edit_admin_edition_path(edition)
    else
      redirect_to edit_admin_edition_path(@edition.document.unpublished_edition),
        alert: edition.errors.full_messages.to_sentence
    end
  end

  def confirm_unpublish
    @unpublishing = Unpublishing.new(document_type: @edition.type, slug: @edition.slug)
  end

  def destroy
    @edition.delete!
    redirect_to admin_editions_path, notice: "The document '#{@edition.title}' has been deleted"
  end

  private

  def edition_class
    Edition
  end

  def edition_params
    (params[:edition] || {}).merge(creator: current_user)
  end

  def build_edition
    @edition = edition_class.new(edition_params)
  end

  def find_edition
    @edition = edition_class.find(params[:id])
  end

  def extract_edition_information_from_errors
    information = @edition.errors.delete(:information)
    @information = information ? information.first : nil
  end

  def prevent_modification_of_unmodifiable_edition
    if @edition.unmodifiable?
      notice = "You cannot modify a #{@edition.state} #{@edition.type.titleize}"
      redirect_to admin_edition_path(@edition), notice: notice
    end
  end

  def default_arrays_of_ids_to_empty
    unless params[:edition][:organisation_ids]
      params[:edition][:lead_organisation_ids] ||= []
      params[:edition][:supporting_organisation_ids] ||= []
    end
    if @edition.can_be_associated_with_topics?
      params[:edition][:topic_ids] ||= []
    end
    if @edition.can_be_associated_with_ministers?
      params[:edition][:ministerial_role_ids] ||= []
    end
    if @edition.can_be_associated_with_role_appointments?
      params[:edition][:role_appointment_ids] ||= []
    end
    if @edition.can_be_associated_with_statistical_data_sets?
      params[:edition][:statistical_data_set_document_ids] ||= []
    end
    if @edition.can_be_grouped_in_series?
      params[:edition][:document_series_ids] ||= []
    end
    if @edition.can_be_related_to_policies?
      params[:edition][:related_document_ids] ||= []
    end
    if @edition.can_be_associated_with_world_locations?
      params[:edition][:world_location_ids] ||= []
    end
    if @edition.can_be_associated_with_mainstream_categories?
      params[:edition][:other_mainstream_category_ids] ||= []
    end
    if @edition.can_be_associated_with_topical_events?
      params[:edition][:topical_event_ids] ||= []
    end
    if @edition.can_be_associated_with_worldwide_organisations?
      params[:edition][:worldwide_organisation_ids] ||= []
    end
    if @edition.can_be_associated_with_worldwide_priorities?
      params[:edition][:worldwide_priority_ids] ||= []
    end
  end

  def build_edition_dependencies
    build_image
    build_edition_organisations
  end

  def build_edition_organisations
    n = @edition.edition_organisations.select { |eo| eo.lead? }.count
    (n...4).each do |i|
      @edition.edition_organisations.build(lead_ordering: i, lead: true)
    end<commit_msg>Fix: Should set related_policy_ids not related_document_ids<commit_after>class Admin::EditionsController < Admin::BaseController
  before_filter :remove_blank_parameters
  before_filter :clear_scheduled_publication_if_not_activated, only: [:create, :update]
  before_filter :find_edition, only: [:show, :edit, :update, :submit, :revise, :reject, :destroy, :confirm_unpublish]
  before_filter :limit_edition_access!, only: [:show, :edit, :update, :submit, :revise, :reject, :destroy, :confirm_unpublish]
  before_filter :prevent_modification_of_unmodifiable_edition, only: [:edit, :update]
  before_filter :default_arrays_of_ids_to_empty, only: [:update]
  before_filter :delete_absent_edition_organisations, only: [:create, :update]
  before_filter :build_edition, only: [:new, :create]
  before_filter :build_edition_organisations, only: [:new, :edit]
  before_filter :detect_other_active_editors, only: [:edit]
  before_filter :redirect_to_controller_for_type, only: [:show]
  before_filter :set_default_world_locations, only: :index

  def index
    if filter && filter.valid?
      session[:document_filters] = params_filters
      render :index
    elsif session_filters.any?
      redirect_to session_filters
    else
      redirect_to default_filters
    end
  rescue ActionController::RoutingError
    redirect_to state: :draft
  end

  def show
  end

  def new
  end

  def create
    if @edition.save
      redirect_to admin_edition_path(@edition), notice: "The document has been saved"
    else
      flash.now[:alert] = "There are some problems with the document"
      extract_edition_information_from_errors
      build_edition_dependencies
      render action: "new"
    end
  end

  def edit
    @edition.open_for_editing_as(current_user)
    render :edit
  end

  def update
    if @edition.edit_as(current_user, params[:edition])
      if params[:speed_save_convert]
        @edition.convert_to_draft!
        next_edition = EditionFilter.new(edition_class, current_user, session_filters.merge(state: :imported)).editions.first
        if next_edition
          redirect_to admin_edition_path(next_edition)
        else
          redirect_to admin_editions_path(session_filters.merge(state: :imported))
        end
      else
        redirect_to admin_edition_path(@edition), notice: "The document has been saved"
      end
    else
      flash.now[:alert] = "There are some problems with the document"
      extract_edition_information_from_errors
      build_edition_dependencies
      render action: "edit"
    end
  rescue ActiveRecord::StaleObjectError
    flash.now[:alert] = "This document has been saved since you opened it"
    @conflicting_edition = Edition.find(params[:id])
    @edition.lock_version = @conflicting_edition.lock_version
    build_edition_dependencies
    render action: "edit"
  end

  def revise
    edition = @edition.create_draft(current_user)
    if edition.persisted?
      redirect_to edit_admin_edition_path(edition)
    else
      redirect_to edit_admin_edition_path(@edition.document.unpublished_edition),
        alert: edition.errors.full_messages.to_sentence
    end
  end

  def confirm_unpublish
    @unpublishing = Unpublishing.new(document_type: @edition.type, slug: @edition.slug)
  end

  def destroy
    @edition.delete!
    redirect_to admin_editions_path, notice: "The document '#{@edition.title}' has been deleted"
  end

  private

  def edition_class
    Edition
  end

  def edition_params
    (params[:edition] || {}).merge(creator: current_user)
  end

  def build_edition
    @edition = edition_class.new(edition_params)
  end

  def find_edition
    @edition = edition_class.find(params[:id])
  end

  def extract_edition_information_from_errors
    information = @edition.errors.delete(:information)
    @information = information ? information.first : nil
  end

  def prevent_modification_of_unmodifiable_edition
    if @edition.unmodifiable?
      notice = "You cannot modify a #{@edition.state} #{@edition.type.titleize}"
      redirect_to admin_edition_path(@edition), notice: notice
    end
  end

  def default_arrays_of_ids_to_empty
    unless params[:edition][:organisation_ids]
      params[:edition][:lead_organisation_ids] ||= []
      params[:edition][:supporting_organisation_ids] ||= []
    end
    if @edition.can_be_associated_with_topics?
      params[:edition][:topic_ids] ||= []
    end
    if @edition.can_be_associated_with_ministers?
      params[:edition][:ministerial_role_ids] ||= []
    end
    if @edition.can_be_associated_with_role_appointments?
      params[:edition][:role_appointment_ids] ||= []
    end
    if @edition.can_be_associated_with_statistical_data_sets?
      params[:edition][:statistical_data_set_document_ids] ||= []
    end
    if @edition.can_be_grouped_in_series?
      params[:edition][:document_series_ids] ||= []
    end
    if @edition.can_be_related_to_policies?
      params[:edition][:related_policy_ids] ||= []
    end
    if @edition.can_be_associated_with_world_locations?
      params[:edition][:world_location_ids] ||= []
    end
    if @edition.can_be_associated_with_mainstream_categories?
      params[:edition][:other_mainstream_category_ids] ||= []
    end
    if @edition.can_be_associated_with_topical_events?
      params[:edition][:topical_event_ids] ||= []
    end
    if @edition.can_be_associated_with_worldwide_organisations?
      params[:edition][:worldwide_organisation_ids] ||= []
    end
    if @edition.can_be_associated_with_worldwide_priorities?
      params[:edition][:worldwide_priority_ids] ||= []
    end
  end

  def build_edition_dependencies
    build_image
    build_edition_organisations
  end

  def build_edition_organisations
    n = @edition.edition_organisations.select { |eo| eo.lead? }.count
    (n...4).each do |i|
      @edition.edition_organisations.build(lead_ordering: i, lead: true)
    end
{'fraction_non_alphanumeric': 0.0928901042556828, 'fraction_numerical': 0.00017091095539224064, 'mean_word_length': 4.271621621621621, 'pattern_counts': {'":': 0, '<': 5, '<?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': '6335250', 'n_tokens_mistral': 3865, 'n_tokens_neox': 3772, 'n_words': 714}