<commit_before>class Action < ApplicationRecord

  attr_accessor :automatic_modification, :apply_action

  belongs_to :user
  enum action_type: { standard: 0, notifier: 1 }

  validate do |action|
    if computed_feed_ids.empty? && self.automatic_modification.blank?
      self.errors[:base] << "Please select at least one feed or tag"
    end
  end

  before_validation :compute_tag_ids
  before_validation :compute_feed_ids

  validate :query_valid

  after_destroy :percolate_remove
  after_commit :percolate_setup, on: [:create, :update]
  after_commit :bulk_actions, on: [:create, :update]

  def percolate_setup
    percolator_query = self.query
    percolator_ids = self.computed_feed_ids
    if percolator_ids.empty?
      percolate_remove
    elsif empty_notifier_action?
      percolate_remove
    else
      options = {
        index: Entry.index_name,
        type: '.percolator',
        id: self.id,
        body: body(percolator_query, percolator_ids)
      }
      $search.each do |_, client|
        client.index(options)
      end
    end
  rescue Elasticsearch::Transport::Transport::Errors::InternalServerError => exception
    Honeybadger.notify(exception)
  end

  def body(percolator_query, percolator_ids)
    Hash.new.tap do |hash|
      hash[:feed_id] = percolator_ids
      hash[:query] = {
        bool: {
          filter: {
            bool: {
              must: { terms: { feed_id: percolator_ids } }
            }
          }
        }
      }
      if percolator_query.present?
        hash[:query][:bool][:must] = {
          query_string: {
            query: percolator_query,
            default_operator: "AND"
          }
        }
      end
    end
  end

  def bulk_actions
    ActionsBulk.perform_async(self.id, self.user.id) if apply_action
  end

  def empty_notifier_action?
    self.all_feeds && self.notifier? && (self.query.nil? || self.query == "")
  end

  def percolate_remove<commit_msg>Check value of apply_action.<commit_after>class Action < ApplicationRecord

  attr_accessor :automatic_modification, :apply_action

  belongs_to :user
  enum action_type: { standard: 0, notifier: 1 }

  validate do |action|
    if computed_feed_ids.empty? && self.automatic_modification.blank?
      self.errors[:base] << "Please select at least one feed or tag"
    end
  end

  before_validation :compute_tag_ids
  before_validation :compute_feed_ids

  validate :query_valid

  after_destroy :percolate_remove
  after_commit :percolate_setup, on: [:create, :update]
  after_commit :bulk_actions, on: [:create, :update]

  def percolate_setup
    percolator_query = self.query
    percolator_ids = self.computed_feed_ids
    if percolator_ids.empty?
      percolate_remove
    elsif empty_notifier_action?
      percolate_remove
    else
      options = {
        index: Entry.index_name,
        type: '.percolator',
        id: self.id,
        body: body(percolator_query, percolator_ids)
      }
      $search.each do |_, client|
        client.index(options)
      end
    end
  rescue Elasticsearch::Transport::Transport::Errors::InternalServerError => exception
    Honeybadger.notify(exception)
  end

  def body(percolator_query, percolator_ids)
    Hash.new.tap do |hash|
      hash[:feed_id] = percolator_ids
      hash[:query] = {
        bool: {
          filter: {
            bool: {
              must: { terms: { feed_id: percolator_ids } }
            }
          }
        }
      }
      if percolator_query.present?
        hash[:query][:bool][:must] = {
          query_string: {
            query: percolator_query,
            default_operator: "AND"
          }
        }
      end
    end
  end

  def bulk_actions
    ActionsBulk.perform_async(self.id, self.user.id) if apply_action == "1"
  end

  def empty_notifier_action?
    self.all_feeds && self.notifier? && (self.query.nil? || self.query == "")
  end

  def percolate_remove
{'fraction_non_alphanumeric': 0.09302921578677602, 'fraction_numerical': 0.0012813941568426447, 'mean_word_length': 2.61054579093432, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, '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': '3352042', 'n_tokens_mistral': 1272, 'n_tokens_neox': 1223, 'n_words': 255}