Sha256: fee84438c9e3fab4bbeb4bc302f6278a974f5ee1d3bfbb09cf8728e8d7758c37

Contents?: true

Size: 1003 Bytes

Versions: 5

Compression:

Stored size: 1003 Bytes

Contents

class User < ApplicationRecord
  has_many :user_teams
  has_many :teams, through: :user_teams
  has_many :users, through: :teams
  has_many :projects
  has_many :pipelines, through: :projects
  has_many :jobs, through: :pipelines

  def self.from_omniauth(auth)
    new_user = false
    user = User.find_by(provider: auth.provider, provider_id: auth.uid)
    unless user.present?
      user = User.create!(provider: auth.provider, provider_id: auth.uid)
      new_user = true
    end

    user.username = auth.info.nickname
    user.email = auth.info.email
    user.name = auth.info.name
    user.token = auth.credentials.token
    user.refresh_token = auth.credentials.refresh_token
    user.save!

    user.sync if new_user

    user
  end

  def sync
    client.teams.each do |team|
      teams.create!(provider: provider, provider_id: team[:id], name: team[:name])
    end
  end

  def client
    if provider == 'github'
      GithubClient.new(username, token)
    else
      nil
    end
  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
jobs-api-0.2.1 app/models/user.rb
jobs-api-0.2.0 app/models/user.rb
jobs-api-0.1.2 app/models/user.rb
jobs-api-0.1.1 app/models/user.rb
jobs-api-0.1.0 app/models/user.rb