Sha256: 9aeb2895257e554165e3521a202de97e351fc91a73e0c372e13607541165d319

Contents?: true

Size: 1.39 KB

Versions: 13

Compression:

Stored size: 1.39 KB

Contents

class Kuroko2::User < Kuroko2::ApplicationRecord
  include Kuroko2::TableNameCustomizable

  GRAVATAR_URL = '//www.gravatar.com/avatar/%s?s=90&d=mm'
  GOOGLE_OAUTH2_PROVIDER = 'google_oauth2'
  GROUP_PROVIDER = 'group_mail'

  paginates_per 100

  scope :active, -> { where(suspended_at: nil) }
  scope :with, -> (ids) { where(id: ids) }
  scope :group_user, -> { where(provider: GROUP_PROVIDER) }

  has_many :stars
  has_many :job_definitions, through: :stars

  has_many :admin_assignments, dependent: :restrict_with_error
  has_many :assigned_job_definitions, through: :admin_assignments, source: :job_definition

  validates_presence_of :name
  validates_presence_of :email
  validates_uniqueness_of :name,  case_sensitive: false, unless: :google_account?
  validates_uniqueness_of :email, case_sensitive: false, unless: :google_account?

  before_create :set_gravatar_image

  def self.find_or_create_user(uid, attributes)
    find_or_create_by(uid: uid) do |user|
      user.name       = attributes[:name]
      user.email      = attributes[:email]
      user.first_name = attributes[:first_name]
      user.last_name  = attributes[:last_name]
    end
  end

  def google_account?
    self.provider == GOOGLE_OAUTH2_PROVIDER
  end

  private

  def set_gravatar_image
    self.image = gravatar_url(self.email)
  end

  def gravatar_url(email)
    GRAVATAR_URL % Digest::MD5::hexdigest(email.strip.downcase)
  end

end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
kuroko2-0.8.0 app/models/kuroko2/user.rb
kuroko2-0.7.0 app/models/kuroko2/user.rb
kuroko2-0.6.0 app/models/kuroko2/user.rb
kuroko2-0.5.2 app/models/kuroko2/user.rb
kuroko2-0.5.1 app/models/kuroko2/user.rb
kuroko2-0.5.0 app/models/kuroko2/user.rb
kuroko2-0.4.6 app/models/kuroko2/user.rb
kuroko2-0.4.5 app/models/kuroko2/user.rb
kuroko2-0.4.4 app/models/kuroko2/user.rb
kuroko2-0.4.3 app/models/kuroko2/user.rb
kuroko2-0.4.2 app/models/kuroko2/user.rb
kuroko2-0.4.1 app/models/kuroko2/user.rb
kuroko2-0.4.0 app/models/kuroko2/user.rb