<commit_before>class Discussion < ActiveRecord::Base
  PER_PAGE = 50
  paginates_per PER_PAGE

  # default_scope -> {where(is_deleted: false)}
  scope :active_since, lambda {|some_time| where('created_at >= ? or last_comment_at >= ?', some_time, some_time)}
  scope :order_by_latest_comment, order('last_comment_at DESC')

  validates_presence_of :title, :group, :author
  validates :title, :length => { :maximum => 150 }
  validates_inclusion_of :uses_markdown, :in => [true,false]

  has_paper_trail :only => [:title, :description]

  belongs_to :group, :counter_cache => true
  belongs_to :author, class_name: 'User'
  has_many :motions, :dependent => :destroy
  has_many :votes, through: :motions
  has_many :comments, :dependent => :destroy
  has_many :comment_likes, :through => :comments, :source => :comment_votes
  has_many :commenters, :through => :comments, :source => :user, :uniq => true
  has_many :events, :as => :eventable, :dependent => :destroy
  has_many :items, class_name: 'Event', include: :eventable, order: 'created_at ASC'
  has_many :discussion_readers

  include PgSearch
  pg_search_scope :search, against: [:title, :description],
    using: {tsearch: {dictionary: "english"}}

  delegate :users, :to => :group, :prefix => :group
  delegate :full_name, :to => :group, :prefix => :group
  delegate :email, :to => :author, :prefix => :author
  delegate :name_and_email, :to => :author, prefix: :author

  after_create :populate_last_comment_at
  after_create :fire_new_discussion_event

  def as_read_by(user)
    if user.blank?
      new_discussion_reader_for(nil)
    elsif joined_to_discussion_reader?
      joined_or_new_discussion_reader_for(user)
    else
      find_or_new_discussion_reader_for(user)
    end
  end

  def voting_motions
    motions.voting
  end

  def closed_motions
    motions.closed
  end

  def group_users_without_discussion_author
    group.users.where(User.arel_table[:id].not_eq(author_id))
  end

  def current_motion_closing_at
    current_motion.closing_at
  end

  def current_motion
    motion = voting_motions.last
    if motion
      motion.close_if_expired
      motion if motion.voting?
    end
  end

  def number_of_comments_since(time)
    comments.where('comments.created_at > ?', time).count<commit_msg>add add_comment back to discussion<commit_after>class Discussion < ActiveRecord::Base
  PER_PAGE = 50
  paginates_per PER_PAGE

  # default_scope -> {where(is_deleted: false)}
  scope :active_since, lambda {|some_time| where('created_at >= ? or last_comment_at >= ?', some_time, some_time)}
  scope :order_by_latest_comment, order('last_comment_at DESC')

  validates_presence_of :title, :group, :author
  validates :title, :length => { :maximum => 150 }
  validates_inclusion_of :uses_markdown, :in => [true,false]

  has_paper_trail :only => [:title, :description]

  belongs_to :group, :counter_cache => true
  belongs_to :author, class_name: 'User'
  has_many :motions, :dependent => :destroy
  has_many :votes, through: :motions
  has_many :comments, :dependent => :destroy
  has_many :comment_likes, :through => :comments, :source => :comment_votes
  has_many :commenters, :through => :comments, :source => :user, :uniq => true
  has_many :events, :as => :eventable, :dependent => :destroy
  has_many :items, class_name: 'Event', include: :eventable, order: 'created_at ASC'
  has_many :discussion_readers

  include PgSearch
  pg_search_scope :search, against: [:title, :description],
    using: {tsearch: {dictionary: "english"}}

  delegate :users, :to => :group, :prefix => :group
  delegate :full_name, :to => :group, :prefix => :group
  delegate :email, :to => :author, :prefix => :author
  delegate :name_and_email, :to => :author, prefix: :author

  after_create :populate_last_comment_at
  after_create :fire_new_discussion_event

  def as_read_by(user)
    if user.blank?
      new_discussion_reader_for(nil)
    elsif joined_to_discussion_reader?
      joined_or_new_discussion_reader_for(user)
    else
      find_or_new_discussion_reader_for(user)
    end
  end

  def add_comment(comment)
    comment.discussion = self
    DiscussionService.add_comment(comment)
  end

  def voting_motions
    motions.voting
  end

  def closed_motions
    motions.closed
  end

  def group_users_without_discussion_author
    group.users.where(User.arel_table[:id].not_eq(author_id))
  end

  def current_motion_closing_at
    current_motion.closing_at
  end

  def current_motion
    motion = voting_motions.last
    if motion
      motion.close_if_expired
      motion if motion.voting?
    end
  end

  def number_of_comments_since(time)
    comments.where('comments.created_at > ?', time).count
{'fraction_non_alphanumeric': 0.10837964942283027, 'fraction_numerical': 0.0021376656690893546, 'mean_word_length': 4.685297691373026, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 53, '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': '2097841', 'n_tokens_mistral': 1620, 'n_tokens_neox': 1552, 'n_words': 357}