<commit_before>class User
  include Mongoid::Document
  
  # authorization fields (deprecated)
  field :provider,    :type => String
  field :uid,         :type => String
  field :token,       :type => String
  field :login,       :type => String
  field :gravatar_id, :type => String

  field :name,      :type => String
  field :email,     :type => String
  field :image,     :type => String
  field :time_zone, :type => String
  
  # attr_accessible :provider, :uid, :token, :login, :gravatar_id
  attr_accessible :name, :email, :image, :time_zone
  
  embeds_many :authorizations
  has_many :tomatoes

  has_merit
  
  def tags
    tomatoes.collect(&:tags).flatten.inject(Hash.new(0)) do |hash, tag|
      hash[tag] += 1; hash
    end.sort do |a, b|
      b[1] <=> a[1]
    end
  end
  
  def self.find_by_omniauth(auth)
    logger.debug "find_by_omniauth"
    any_of(
      {:authorizations => {
        '$elemMatch' => {
          :provider => auth['provider'].to_s,
          :uid      => auth['uid'].to_s }}},
      {:provider => auth['provider'], :uid => auth['uid']}
    ).first
  end
  
  def self.create_with_omniauth!(auth)
    logger.debug "create_with_omniauth"
    begin
      user = User.new(omniauth_attributes(auth))
      user.authorizations.build(Authorization.omniauth_attributes(auth))
      user.save!
      user
    rescue Exception
      raise Exception, "Cannot create user"
    end
  end
  
  def update_omniauth_attributes!(auth)
    begin
      update_attributes!(User.omniauth_attributes(auth))

      if authorization = authorization_by_provider(auth['provider'])
        authorization.update_attributes!(Authorization.omniauth_attributes(auth))
      else
        # merge one more authorization provider
        authorizations.create!(Authorization.omniauth_attributes(auth))
      end
    rescue Exception
      raise Exception, "Cannot update user"
    end
  end
  
  def self.omniauth_attributes(auth)
    attributes = {}
    
    attributes.merge!({
      name: auth['info']['name'],
      email: auth['info']['email'],
      image: auth['info']['image']
    }) if auth['info']
    
    attributes
  end

  def authorization_by_provider(provider)
    authorizations.where(provider: provider).first
  end

  def tomatoes_after(time)
    tomatoes.where(:created_at => {'$gte' => time}).order_by([[:created_at, :desc]])
  end
end
<commit_msg>Add color field; add grant method<commit_after>class User
  include Mongoid::Document
  
  # authorization fields (deprecated)
  field :provider,    :type => String
  field :uid,         :type => String
  field :token,       :type => String
  field :login,       :type => String
  field :gravatar_id, :type => String

  field :name,      :type => String
  field :email,     :type => String
  field :image,     :type => String
  field :time_zone, :type => String
  field :color,     :type => String
  
  # attr_accessible :provider, :uid, :token, :login, :gravatar_id
  attr_accessible :name, :email, :image, :time_zone, :color
  
  embeds_many :authorizations
  has_many :tomatoes

  has_merit
  
  def tags
    tomatoes.collect(&:tags).flatten.inject(Hash.new(0)) do |hash, tag|
      hash[tag] += 1; hash
    end.sort do |a, b|
      b[1] <=> a[1]
    end
  end
  
  def self.find_by_omniauth(auth)
    logger.debug "find_by_omniauth"
    any_of(
      {:authorizations => {
        '$elemMatch' => {
          :provider => auth['provider'].to_s,
          :uid      => auth['uid'].to_s }}},
      {:provider => auth['provider'], :uid => auth['uid']}
    ).first
  end
  
  def self.create_with_omniauth!(auth)
    logger.debug "create_with_omniauth"
    begin
      user = User.new(omniauth_attributes(auth))
      user.authorizations.build(Authorization.omniauth_attributes(auth))
      user.save!
      user
    rescue Exception
      raise Exception, "Cannot create user"
    end
  end
  
  def update_omniauth_attributes!(auth)
    begin
      update_attributes!(User.omniauth_attributes(auth))

      if authorization = authorization_by_provider(auth['provider'])
        authorization.update_attributes!(Authorization.omniauth_attributes(auth))
      else
        # merge one more authorization provider
        authorizations.create!(Authorization.omniauth_attributes(auth))
      end
    rescue Exception
      raise Exception, "Cannot update user"
    end
  end
  
  def self.omniauth_attributes(auth)
    attributes = {}
    
    attributes.merge!({
      name: auth['info']['name'],
      email: auth['info']['email'],
      image: auth['info']['image']
    }) if auth['info']
    
    attributes
  end

  def authorization_by_provider(provider)
    authorizations.where(provider: provider).first
  end

  def tomatoes_after(time)
    tomatoes.where(:created_at => {'$gte' => time}).order_by([[:created_at, :desc]])
  end

  def grant(name, level)
    badges.select do |badge|
      name == badge.name && badge.level >= level
    end.size > 0
  end
end

{'fraction_non_alphanumeric': 0.1250252882864657, 'fraction_numerical': 0.001820756625531054, 'mean_word_length': 2.9806763285024154, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 42, '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': '3347702', 'n_tokens_mistral': 1574, 'n_tokens_neox': 1533, 'n_words': 355}