<commit_before>module WorkflowActor
  def record_action(edition, type, options={})
    type = Action.const_get(type.to_s.upcase)
    action = edition.new_action(self, type, options)
    messenger_topic = edition.state.to_s.downcase
    Messenger.instance.send messenger_topic, edition unless messenger_topic == "created"
    NoisyWorkflow.make_noise(action).deliver
  end

  def take_action(edition, action, details = {})
    apply_guards = respond_to?(:"can_#{action}?") ? __send__(:"can_#{action}?", edition) : true

    if apply_guards and transition = edition.send(action)
      record_action(edition, action, details)
      edition
    else
      false
    end
  end

  def record_note(edition, comment)
    edition.new_action(self, 'note', comment: comment)
  end

  def create_whole_edition(format, attributes = {})
    format = "#{format}_edition" unless format.to_s.match(/edition$/)
    publication_class = format.to_s.camelize.constantize
    item = publication_class.create(attributes)
    record_action(item, Action::CREATE) if item.persisted?
    item
  end

  def new_version(edition)
    return false unless edition.published?

    new_edition = edition.build_clone
    if new_edition
      record_action new_edition, Action::NEW_VERSION
      new_edition
    else
      false
    end
  end

  def start_work(edition)
    take_action(edition, __method__)
  end

  def send_fact_check(edition, details)
    return false if details[:email_addresses].blank?

    note_text = "\n\nResponses should be sent to: " + edition.fact_check_email_address
    if details[:comment].blank?
      details[:comment] = "Fact check requested" + note_text
    else
      details[:comment] += note_text
    end

    edition.send_fact_check
    record_action edition, __method__, details
    NoisyWorkflow.request_fact_check(edition, details).deliver
    edition
  end

  def request_review(edition, details)
    take_action(edition, __method__, details)
  end

  def receive_fact_check(edition, details)
    take_action(edition, __method__, details)
  end

  def request_amendments(edition, details)
    take_action(edition, __method__, details)
  end

  def approve_review(edition, details)
    take_action(edition, __method__, details)
  end

  def approve_fact_check(edition, details)
    take_action(edition, __method__, details)
  end

  def publish(edition, details)
    take_action(edition, __method__, details)
  end

  def can_approve_review?(edition)
    edition.latest_status_action.requester_id != self.id
  end
  alias :can_request_amendments? :can_approve_review?

  def assign(edition, recipient)<commit_msg>Now we have all these identical methods, let's generate them<commit_after>module WorkflowActor
  def record_action(edition, type, options={})
    type = Action.const_get(type.to_s.upcase)
    action = edition.new_action(self, type, options)
    messenger_topic = edition.state.to_s.downcase
    Messenger.instance.send messenger_topic, edition unless messenger_topic == "created"
    NoisyWorkflow.make_noise(action).deliver
  end

  def take_action(edition, action, details = {})
    apply_guards = respond_to?(:"can_#{action}?") ? __send__(:"can_#{action}?", edition) : true

    if apply_guards and transition = edition.send(action)
      record_action(edition, action, details)
      edition
    else
      false
    end
  end

  def record_note(edition, comment)
    edition.new_action(self, 'note', comment: comment)
  end

  def create_whole_edition(format, attributes = {})
    format = "#{format}_edition" unless format.to_s.match(/edition$/)
    publication_class = format.to_s.camelize.constantize
    item = publication_class.create(attributes)
    record_action(item, Action::CREATE) if item.persisted?
    item
  end

  def new_version(edition)
    return false unless edition.published?

    new_edition = edition.build_clone
    if new_edition
      record_action new_edition, Action::NEW_VERSION
      new_edition
    else
      false
    end
  end

  def send_fact_check(edition, details)
    return false if details[:email_addresses].blank?

    note_text = "\n\nResponses should be sent to: " + edition.fact_check_email_address
    if details[:comment].blank?
      details[:comment] = "Fact check requested" + note_text
    else
      details[:comment] += note_text
    end

    edition.send_fact_check
    record_action edition, __method__, details
    NoisyWorkflow.request_fact_check(edition, details).deliver
    edition
  end

  %W[start_work request_review receive_fact_check request_amendments approve_review approve_fact_check publish].each do |method|
    define_method(method) do |edition, details = {}|
      take_action(edition, __method__, details)
    end
  end

  def can_approve_review?(edition)
    edition.latest_status_action.requester_id != self.id
  end
  alias :can_request_amendments? :can_approve_review?

  def assign(edition, recipient)
{'fraction_non_alphanumeric': 0.08916547643338094, 'fraction_numerical': 0.0, 'mean_word_length': 4.10625, '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': '7458858', 'n_tokens_mistral': 1563, 'n_tokens_neox': 1536, 'n_words': 337}