<commit_before># == Schema Information
#
# Table name: find_out_more_types
#
#  alert_type_id :bigint(8)        not null
#  created_at    :datetime         not null
#  description   :string           not null
#  id            :bigint(8)        not null, primary key
#  rating_from   :decimal(, )      not null
#  rating_to     :decimal(, )      not null
#  updated_at    :datetime         not null
#
# Indexes
#
#  index_find_out_more_types_on_alert_type_id  (alert_type_id)
#
# Foreign Keys
#
#  fk_rails_...  (alert_type_id => alert_types.id) ON DELETE => restrict
#

class FindOutMoreType < ApplicationRecord
  belongs_to :alert_type
  has_many :content_versions, class_name: 'FindOutMoreTypeContentVersion'

  scope :for_rating, ->(rating) { where('rating_from <= ? AND rating_to >= ?', rating, rating) }

  validates :rating_from, :rating_to, :description, presence: true
  validates :rating_from, :rating_to, numericality: { greater_than_or_equal_to: 0, less_than_or_equal_to: 10 }

  def current_content
    content_versions.latest.first
  end

  def update_with_content!(attributes, content)
    to_replace = current_content
    self.attributes = attributes
    if valid? && content.valid?
      transaction do
        save!
        content.save!
        to_replace.update!(replaced_by: content) if to_replace
      end
      true
    else
      false
    end
  end
end<commit_msg>Refactor find out more model<commit_after># == Schema Information
#
# Table name: find_out_more_types
#
#  alert_type_id :bigint(8)        not null
#  created_at    :datetime         not null
#  description   :string           not null
#  id            :bigint(8)        not null, primary key
#  rating_from   :decimal(, )      not null
#  rating_to     :decimal(, )      not null
#  updated_at    :datetime         not null
#
# Indexes
#
#  index_find_out_more_types_on_alert_type_id  (alert_type_id)
#
# Foreign Keys
#
#  fk_rails_...  (alert_type_id => alert_types.id) ON DELETE => restrict
#

class FindOutMoreType < ApplicationRecord
  belongs_to :alert_type
  has_many :content_versions, class_name: 'FindOutMoreTypeContentVersion'

  scope :for_rating, ->(rating) { where('rating_from <= ? AND rating_to >= ?', rating, rating) }

  validates :rating_from, :rating_to, :description, presence: true
  validates :rating_from, :rating_to, numericality: { greater_than_or_equal_to: 0, less_than_or_equal_to: 10 }

  def current_content
    content_versions.latest.first
  end

  def update_with_content!(attributes, content)
    to_replace = current_content
    self.attributes = attributes
    if valid? && content.valid?
      save_and_replace(content, to_replace)
      true
    else
      false
    end
  end


private

  def save_and_replace(content, to_replace)
    transaction do
      save!
      content.save!
      to_replace.update!(replaced_by: content) if to_replace
    end
  end
end
{'fraction_non_alphanumeric': 0.0865650969529086, 'fraction_numerical': 0.0034626038781163434, 'mean_word_length': 3.2298682284040994, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, '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': '6178960', 'n_tokens_mistral': 957, 'n_tokens_neox': 936, 'n_words': 224}