<commit_before>require "redcarpet/render_strip"

module Houston
  module Feedback
    class Comment < ActiveRecord::Base
      self.table_name = "feedback_comments"
      
      before_save :update_plain_text, :if => :text_changed?
      after_save :update_search_vector, :if => :search_vector_should_change?
      
      belongs_to :project
      belongs_to :user
      
      has_many :user_flags, class_name: "Houston::Feedback::CommentUserFlags"
      
      versioned only: [:customer, :text, :tags]
      
      class << self
        def for_project(project)
          where(project_id: project.id)
        end
        
        def with_flags_for(user)
          joins(<<-SQL).select("feedback_comments.*", "flags.read")
            LEFT OUTER JOIN feedback_comments_user_flags \"flags\"
            ON flags.comment_id=feedback_comments.id AND flags.user_id=#{user.id}
          SQL
        end
        
        def unread_by(user)
          with_flags_for(user).where("flags.read IS FALSE OR flags.read IS NULL")
        end
        
        def since(time)
          where arel_table[:created_at].gteq(time)
        end
        
        # http://blog.lostpropertyhq.com/postgres-full-text-search-is-good-enough/
        def search(query_string)
          tags = []
          not_tags = []
          flags = []
          reporter_id = nil
          created_at = nil
          query_string = query_string
            .gsub(/\/(read|unread|untagged)/) { |arg| flags << $1; "" }
            .gsub(/\-\#([a-z\-\?0-9\|]+)/) { |arg| not_tags << $1; "" }
            .gsub(/\#([a-z\-\?0-9\|]+)/) { |arg| tags << $1; "" }
            .gsub(/by:([A-Za-z0-9]+)/) { |arg|
              reporter_id = User.where(["lower(concat(first_name, last_name)) = ?", $1])
                .limit(1).pluck(:id)[0] || reporter_id
              "" }
            .gsub(/added:(\d{8})(?:[-–—](\d{8}))?/) { |arg|
              min = Date.strptime($1, "%Y%m%d").beginning_of_day
              max = Date.strptime($2 || $1, "%Y%m%d").end_of_day
              created_at = min..max
              "" }<commit_msg>[skip] Changed the syntax of the `by:` query to support before and after (8m)<commit_after>require "redcarpet/render_strip"

module Houston
  module Feedback
    class Comment < ActiveRecord::Base
      self.table_name = "feedback_comments"
      
      before_save :update_plain_text, :if => :text_changed?
      after_save :update_search_vector, :if => :search_vector_should_change?
      
      belongs_to :project
      belongs_to :user
      
      has_many :user_flags, class_name: "Houston::Feedback::CommentUserFlags"
      
      versioned only: [:customer, :text, :tags]
      
      class << self
        def for_project(project)
          where(project_id: project.id)
        end
        
        def with_flags_for(user)
          joins(<<-SQL).select("feedback_comments.*", "flags.read")
            LEFT OUTER JOIN feedback_comments_user_flags \"flags\"
            ON flags.comment_id=feedback_comments.id AND flags.user_id=#{user.id}
          SQL
        end
        
        def unread_by(user)
          with_flags_for(user).where("flags.read IS FALSE OR flags.read IS NULL")
        end
        
        def since(time)
          where arel_table[:created_at].gteq(time)
        end
        
        # http://blog.lostpropertyhq.com/postgres-full-text-search-is-good-enough/
        def search(query_string)
          tags = []
          not_tags = []
          flags = []
          reporter_id = nil
          created_at = nil
          query_string = query_string
            .gsub(/\/(read|unread|untagged)/) { flags << $1; "" }
            .gsub(/\-\#([a-z\-\?0-9\|]+)/) { not_tags << $1; "" }
            .gsub(/\#([a-z\-\?0-9\|]+)/) { tags << $1; "" }
            .gsub(/by:([A-Za-z0-9]+)/) {
              reporter_id = User.where(["lower(concat(first_name, last_name)) = ?", $1])
                .limit(1).pluck(:id)[0] || reporter_id
              "" }
            .gsub(/added:(\d{8}?)\.\.(\d{8}?)/) {
              min, max = $1, $2
              min = "20000101" if min.blank?
              max = "20991231" if max.blank?
              min = Date.strptime(min, "%Y%m%d").beginning_of_day
              max = Date.strptime(max, "%Y%m%d").end_of_day
              created_at = min..max
              "" }
            .gsub(/added:(\d{8})/) {
              date = Date.strptime($1, "%Y%m%d")
              min = date.beginning_of_day
              max = date.end_of_day
              created_at = min..max
              "" }
{'fraction_non_alphanumeric': 0.1510015408320493, 'fraction_numerical': 0.01144618093770636, 'mean_word_length': 1.7792048929663609, 'pattern_counts': {'":': 0, '<': 25, '<?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': '6121277', 'n_tokens_mistral': 1517, 'n_tokens_neox': 1483, 'n_words': 253}