<commit_before>class User
  include Mongoid::Document
  include Mongoid::Timestamps
  include Mongoid::Paranoia

  GENDER = %w(male female)

  paginates_per 25

  attr_accessible :nationality, :time_zone, :locale, :vehicle_avg_consumption

  has_and_belongs_to_many :conversations

  has_many :itineraries, dependent: :destroy
  has_many :feedbacks

  embeds_many :notifications
  embeds_many :references, cascade_callbacks: true

  # Auth / Credentials
  field :provider
  field :uid
  field :oauth_token
  field :oauth_expires_at
  # Cache Permissions
  field :facebook_permissions
  # Info
  field :email
  field :name
  # Extra
  field :username
  field :gender
  field :bio
  field :languages, type: Hash

  # More info requiring special permissions
  field :birthday, type: Date
  field :work, type: Hash, default: {}
  field :education, type: Hash, default: {}

  # Icare
  field :nationality
  field :vehicle_avg_consumption, type: Float, default: (1.741*7.0/100.0).round(2)

  # Account
  field :locale
  field :time_zone, default: "UTC" # NOTE think about it
  field :telephone
  field :admin, type: Boolean, default: false
  #field :access_level, type: Integer, default: 0
  field :banned, type: Boolean, default: false

  field :send_email_messages, type: Boolean, default: true
  field :send_email_references, type: Boolean, default: true

  validates :gender, inclusion: GENDER, allow_blank: true
  validates :nationality, inclusion: Country.all.map{ |c| c.code }, allow_blank: true
  validates :time_zone, inclusion: ActiveSupport::TimeZone.zones_map.map{ |zone| zone.first }, allow_blank: true
  validates :vehicle_avg_consumption, numericality: { greater_than: 0, less_than: 10 }, presence: true
  #validates :access_level, numericality: { only_integer: true, greater_than_or_equal_to: 0, less_than_or_equal_to: 5 }
  scope :sorted, asc(:name)

  def self.from_omniauth(auth)
    if user = where(auth.slice("provider", "uid")).first
      user.update_fields_from_omniauth(auth)
      user.save!
      user
    else
      create_from_omniauth(auth)
    end
  end

  def self.create_from_omniauth(auth)
    create! do |user|

      # Auth / Credentials
      user.provider = auth.provider
      user.uid = auth.uid
      user.oauth_token = auth.credentials.token
      user.oauth_expires_at = Time.at(auth.credentials.expires_at)

      user.update_fields_from_omniauth(auth)
    end
  end

  def update_fields_from_omniauth(auth)
    # Info
    self.email = auth.info.email
    self.name = auth.info.name

    # Extra
    self.username = auth.extra.raw_info.username
    self.gender = auth.extra.raw_info.gender
    self.bio = auth.extra.raw_info.bio
    self.languages = auth.extra.raw_info.languages

    # Locale (with priority to icare)
    self.locale = auth.extra.raw_info.locale.gsub(/_/,"-") unless self.locale?

    # Extras (extra permissions required)
    self.birthday = Date.strptime(auth.extra.raw_info.birthday, "%m/%d/%Y").at_midnight
    self.work = auth.extra.raw_info.work
    self.education = auth.extra.raw_info.education

    # Cache permissions
    facebook do |fb|
      self.facebook_permissions = fb.get_connections("me", "permissions")[0]
    end
  end

  def facebook
    @facebook ||= Koala::Facebook::API.new(oauth_token)
    block_given? ? yield(@facebook) : @facebook
  rescue Koala::Facebook::APIError => e
    logger.info e.to_s
    nil # or consider a custom null object
    # TODO<commit_msg>fixing bug in user show<commit_after>class User
  include Mongoid::Document
  include Mongoid::Timestamps
  include Mongoid::Paranoia

  GENDER = %w(male female)

  paginates_per 25

  attr_accessible :nationality, :time_zone, :locale, :vehicle_avg_consumption

  has_and_belongs_to_many :conversations

  has_many :itineraries, dependent: :destroy
  has_many :feedbacks

  embeds_many :notifications
  embeds_many :references, cascade_callbacks: true

  # Auth / Credentials
  field :provider
  field :uid
  field :oauth_token
  field :oauth_expires_at
  # Cache Permissions
  field :facebook_permissions
  # Info
  field :email
  field :name
  # Extra
  field :username
  field :gender
  field :bio
  field :languages, type: Hash

  # More info requiring special permissions
  field :birthday, type: Date
  field :work, type: Hash, default: {}
  field :education, type: Hash, default: {}

  # Icare
  field :nationality
  field :vehicle_avg_consumption, type: Float, default: (1.741*7.0/100.0).round(2)

  # Account
  field :locale
  field :time_zone, default: "UTC" # NOTE think about it
  field :telephone
  field :admin, type: Boolean, default: false
  #field :access_level, type: Integer, default: 0
  field :banned, type: Boolean, default: false

  field :send_email_messages, type: Boolean, default: true
  field :send_email_references, type: Boolean, default: true

  validates :gender, inclusion: GENDER, allow_blank: true
  validates :nationality, inclusion: Country.all.map{ |c| c.code }, allow_blank: true
  validates :time_zone, inclusion: ActiveSupport::TimeZone.zones_map.map{ |zone| zone.first }, allow_blank: true
  validates :vehicle_avg_consumption, numericality: { greater_than: 0, less_than: 10 }, presence: true
  #validates :access_level, numericality: { only_integer: true, greater_than_or_equal_to: 0, less_than_or_equal_to: 5 }
  scope :sorted, asc(:name)

  def self.from_omniauth(auth)
    if user = where(auth.slice("provider", "uid")).first
      user.update_fields_from_omniauth(auth)
      user.save!
      user
    else
      create_from_omniauth(auth)
    end
  end

  def self.create_from_omniauth(auth)
    create! do |user|

      # Auth / Credentials
      user.provider = auth.provider
      user.uid = auth.uid
      user.oauth_token = auth.credentials.token
      user.oauth_expires_at = Time.at(auth.credentials.expires_at)

      user.update_fields_from_omniauth(auth)
    end
  end

  def update_fields_from_omniauth(auth)
    # Info
    self.email = auth.info.email
    self.name = auth.info.name

    # Extra
    self.username = auth.extra.raw_info.username
    self.gender = auth.extra.raw_info.gender
    self.bio = auth.extra.raw_info.bio
    self.languages = auth.extra.raw_info.languages

    # Locale (with priority to icare)
    self.locale = auth.extra.raw_info.locale.gsub(/_/,"-") unless self.locale?

    # Extras (extra permissions required)
    self.birthday = Date.strptime(auth.extra.raw_info.birthday, "%m/%d/%Y").at_midnight
    self.work = auth.extra.raw_info.work if auth.extra.raw_info.work
    self.education = auth.extra.raw_info.education if auth.extra.raw_info.education

    # Cache permissions
    facebook do |fb|
      self.facebook_permissions = fb.get_connections("me", "permissions")[0]
    end
  end

  def facebook
    @facebook ||= Koala::Facebook::API.new(oauth_token)
    block_given? ? yield(@facebook) : @facebook
  rescue Koala::Facebook::APIError => e
    logger.info e.to_s
    nil # or consider a custom null object
    # TODO
{'fraction_non_alphanumeric': 0.10116396033912918, 'fraction_numerical': 0.005747952291995977, 'mean_word_length': 3.8501742160278747, 'pattern_counts': {'":': 0, '<': 3, '<?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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2178828', 'n_tokens_mistral': 2403, 'n_tokens_neox': 2275, 'n_words': 553}