<commit_before>require "create_section_service"
require "update_section_service"
require "manuals_republisher"
require "manual_withdrawer"

module ManualHelpers
  def manual_repository
    ScopedManualRepository.new(ManualRecord.all)
  end

  def create_manual(fields, save: true)
    visit new_manual_path
    fill_in_fields(fields)

    yield if block_given?

    save_as_draft if save
  end

  def create_manual_without_ui(fields, organisation_slug: "ministry-of-tea")
    stub_organisation_details(organisation_slug)

    user = FactoryGirl.build(:generic_editor, organisation_slug: organisation_slug)

    service = CreateManualService.new(
      attributes: fields.merge(organisation_slug: organisation_slug),
      context: double(:context, current_user: user)
    )
    manual = service.call

    manual_repository.fetch(manual.id)
  end

  def create_section(manual_title, fields)
    go_to_manual_page(manual_title)
    click_on "Add section"

    fill_in_fields(fields)

    yield if block_given?

    save_as_draft
  end

  def create_section_without_ui(manual, fields, organisation_slug: "ministry-of-tea")
    user = FactoryGirl.build(:generic_editor, organisation_slug: organisation_slug)

    create_service_context = OpenStruct.new(
      params: {
        "manual_id" => manual.id,
        "section" => fields,
      },
      current_user: user
    )

    service = CreateSectionService.new(
      context: create_service_context,
    )
    _, section = service.call

    section
  end

  def edit_manual(manual_title, new_fields)
    go_to_edit_page_for_manual(manual_title)
    fill_in_fields(new_fields)

    yield if block_given?

    save_as_draft
  end

  def edit_manual_without_ui(manual, fields, organisation_slug: "ministry-of-tea")
    stub_organisation_details(organisation_slug)

    user = FactoryGirl.build(:generic_editor, organisation_slug: organisation_slug)

    service = UpdateManualService.new(
      manual_id: manual.id,
      attributes: fields.merge(organisation_slug: organisation_slug),
      context: double(:context, current_user: user)
    )
    manual = service.call<commit_msg>Use OpenStruct vs mock object for context in ManualHelpers<commit_after>require "create_section_service"
require "update_section_service"
require "manuals_republisher"
require "manual_withdrawer"

module ManualHelpers
  def manual_repository
    ScopedManualRepository.new(ManualRecord.all)
  end

  def create_manual(fields, save: true)
    visit new_manual_path
    fill_in_fields(fields)

    yield if block_given?

    save_as_draft if save
  end

  def create_manual_without_ui(fields, organisation_slug: "ministry-of-tea")
    stub_organisation_details(organisation_slug)

    user = FactoryGirl.build(:generic_editor, organisation_slug: organisation_slug)

    service = CreateManualService.new(
      attributes: fields.merge(organisation_slug: organisation_slug),
      context: OpenStruct.new(current_user: user)
    )
    manual = service.call

    manual_repository.fetch(manual.id)
  end

  def create_section(manual_title, fields)
    go_to_manual_page(manual_title)
    click_on "Add section"

    fill_in_fields(fields)

    yield if block_given?

    save_as_draft
  end

  def create_section_without_ui(manual, fields, organisation_slug: "ministry-of-tea")
    user = FactoryGirl.build(:generic_editor, organisation_slug: organisation_slug)

    create_service_context = OpenStruct.new(
      params: {
        "manual_id" => manual.id,
        "section" => fields,
      },
      current_user: user
    )

    service = CreateSectionService.new(
      context: create_service_context,
    )
    _, section = service.call

    section
  end

  def edit_manual(manual_title, new_fields)
    go_to_edit_page_for_manual(manual_title)
    fill_in_fields(new_fields)

    yield if block_given?

    save_as_draft
  end

  def edit_manual_without_ui(manual, fields, organisation_slug: "ministry-of-tea")
    stub_organisation_details(organisation_slug)

    user = FactoryGirl.build(:generic_editor, organisation_slug: organisation_slug)

    service = UpdateManualService.new(
      manual_id: manual.id,
      attributes: fields.merge(organisation_slug: organisation_slug),
      context: OpenStruct.new(current_user: user)
    )
    manual = service.call
{'fraction_non_alphanumeric': 0.0754014428671166, 'fraction_numerical': 0.0, 'mean_word_length': 4.184559710494572, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '6181876', 'n_tokens_mistral': 1386, 'n_tokens_neox': 1333, 'n_words': 249}