Sha256: 261761566a41499e056c0a6221874b5aee1b52513a213d06d58864de743aa400

Contents?: true

Size: 1.02 KB

Versions: 16

Compression:

Stored size: 1.02 KB

Contents

module Writefully
  class Authorship < ActiveRecord::Base
    self.table_name = "writefully_authorships"

    belongs_to :user

    has_many :posts
    has_many :owned_sites, class_name: "Writefully::Site", foreign_key: :owner_id

    store_accessor :data, :name, :email, :uid, :user_name, :auth_token, :bio

    def to_s
      data["name"] || data["login"] || data["email"] || data["uid"]
    end

    def self.find_by_uid(uid)
      where("data -> 'uid' = ?", uid.to_s).first
    end

    def select_role
      Authorship.count == 0 ? "super_admin" : "collaborator"
    end

    def self.create_from_data(auth_user)
      create! do |authorship|
        authorship.data = { email: auth_user.email,
                            uid: auth_user.id,
                            user_name: auth_user.login,
                            name: auth_user.name,
                            avatar: auth_user.avatar_url }.select { |k,v| v.present? }
                            
        authorship.role = authorship.select_role
      end 
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
writefully-0.8.2 app/models/writefully/authorship.rb
writefully-0.8.1 app/models/writefully/authorship.rb
writefully-0.8.0 app/models/writefully/authorship.rb
writefully-0.7.1 app/models/writefully/authorship.rb
writefully-0.6.12 app/models/writefully/authorship.rb
writefully-0.6.11 app/models/writefully/authorship.rb
writefully-0.6.10 app/models/writefully/authorship.rb
writefully-0.6.9 app/models/writefully/authorship.rb
writefully-0.6.7 app/models/writefully/authorship.rb
writefully-0.6.6 app/models/writefully/authorship.rb
writefully-0.6.5 app/models/writefully/authorship.rb
writefully-0.6.4 app/models/writefully/authorship.rb
writefully-0.6.3 app/models/writefully/authorship.rb
writefully-0.6.2 app/models/writefully/authorship.rb
writefully-0.5.1 app/models/writefully/authorship.rb
writefully-0.5.0 app/models/writefully/authorship.rb