<commit_before>class User < ActiveRecord::Base
  has_many :articles, foreign_key: "author_id"
  
  domain_regex = /\A([\w\.%\+\-]+)@(envylabs|codeschool)\.com$\z/
  validates :email, presence: true, format: { with: domain_regex }


  def self.author
    self.select("users.*").joins(:articles).group('users.id').having('count(articles.id) > 0')
  end

  def self.find_or_create_from_omniauth(auth)
    user = where(auth.slice("provider", "uid")).first || create_from_omniauth(auth)
    # TODO: remove when all existing users have an image
    user.image = auth["info"]["image"] && user.save! if user && user.image.present?
    user
  end

  def self.create_from_omniauth(auth)
    create do |user|
      user.provider = auth["provider"]
      user.uid = auth["uid"]
      user.name = auth["info"]["name"]
      user.email = auth["info"]["email"]
      user.image = auth["info"]["image"]

      return false unless user.valid?
    end
  end

  def to_s
    self.name || self.email
  end
end<commit_msg>Update every user image when they auth in.<commit_after>class User < ActiveRecord::Base
  has_many :articles, foreign_key: "author_id"
  
  domain_regex = /\A([\w\.%\+\-]+)@(envylabs|codeschool)\.com$\z/
  validates :email, presence: true, format: { with: domain_regex }


  def self.author
    self.select("users.*").joins(:articles).group('users.id').having('count(articles.id) > 0')
  end

  def self.find_or_create_from_omniauth(auth)
    user = where(auth.slice("provider", "uid")).first || create_from_omniauth(auth)
    # TODO: remove when all existing users have an image
    update_image(auth)
    user
  end

  def self.create_from_omniauth(auth)
    create do |user|
      user.provider = auth["provider"]
      user.uid = auth["uid"]
      user.name = auth["info"]["name"]
      user.email = auth["info"]["email"]
      user.image = auth["info"]["image"]

      return false unless user.valid?
    end
  end

  def to_s
    self.name || self.email
  end

  private

  def self.update_image(auth)
    user.image = auth["info"]["image"]
    user.save!
  end
end
{'fraction_non_alphanumeric': 0.1578185328185328, 'fraction_numerical': 0.0009652509652509653, 'mean_word_length': 3.4294871794871793, 'pattern_counts': {'":': 0, '<': 5, '<?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': '6842173', 'n_tokens_mistral': 717, 'n_tokens_neox': 694, 'n_words': 150}