<commit_before>class IncomingLink < ActiveRecord::Base
  belongs_to :post
  belongs_to :user
  belongs_to :incoming_referer

  validate :referer_valid
  validate :post_id, presence: true

  after_create :update_link_counts

  attr_accessor :url

  def self.add(opts)
    user_id, host, referer = nil
    current_user = opts[:current_user]

    if username = opts[:username]
      u = User.select(:id).find_by(username_lower: username.downcase)
      user_id = u.id if u
    end

    if opts[:referer].present?
      begin
        host = URI.parse(opts[:referer]).host
        referer = opts[:referer][0..999]
      rescue URI::InvalidURIError
        # bad uri, skip
      end
    end

    if host != opts[:host] && (user_id || referer)

      post_id = opts[:post_id]
      post_id ||= Post.where(topic_id: opts[:topic_id],
                             post_number: opts[:post_number] || 1)
                            .pluck(:id).first

      cid = current_user ? (current_user.id) : (nil)


      unless cid && cid == user_id

        IncomingLink.create(referer: referer,
                            user_id: user_id,
                            post_id: post_id,
                            current_user_id: cid,
                            ip_address: opts[:ip_address]) if post_id

      end
    end

  end


  def referer=(referer)<commit_msg>Fixed ActiveRecord validation for post_id in incoming_link.rb<commit_after>class IncomingLink < ActiveRecord::Base
  belongs_to :post
  belongs_to :user
  belongs_to :incoming_referer

  validate :referer_valid
  validates :post_id, presence: true

  after_create :update_link_counts

  attr_accessor :url

  def self.add(opts)
    user_id, host, referer = nil
    current_user = opts[:current_user]

    if username = opts[:username]
      u = User.select(:id).find_by(username_lower: username.downcase)
      user_id = u.id if u
    end

    if opts[:referer].present?
      begin
        host = URI.parse(opts[:referer]).host
        referer = opts[:referer][0..999]
      rescue URI::InvalidURIError
        # bad uri, skip
      end
    end

    if host != opts[:host] && (user_id || referer)

      post_id = opts[:post_id]
      post_id ||= Post.where(topic_id: opts[:topic_id],
                             post_number: opts[:post_number] || 1)
                            .pluck(:id).first

      cid = current_user ? (current_user.id) : (nil)


      unless cid && cid == user_id

        create(referer: referer,
               user_id: user_id,
               post_id: post_id,
               current_user_id: cid,
               ip_address: opts[:ip_address]) if post_id

      end
    end

  end


  def referer=(referer)
{'fraction_non_alphanumeric': 0.09903201787043932, 'fraction_numerical': 0.0037230081906180195, 'mean_word_length': 2.1135573580533022, 'pattern_counts': {'":': 0, '<': 5, '<?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': '7120181', 'n_tokens_mistral': 873, 'n_tokens_neox': 857, 'n_words': 187}