Sha256: 7c5407f44df9182f3f4290c4460c8ae6716a6fb91d4a502ff17f9ef9d0f94910

Contents?: true

Size: 1.83 KB

Versions: 4

Compression:

Stored size: 1.83 KB

Contents

module OpenStax::Accounts
    class Account < ActiveRecord::Base
    USERNAME_DISCARDED_CHAR_REGEX = /[^A-Za-z\d_]/
    USERNAME_MAX_LENGTH = 50
    SYNC_ATTRIBUTES = [
      :openstax_uid,
      :username,
      :first_name,
      :last_name,
      :full_name,
      :title,
      :self_reported_role,
      :faculty_status,
      :school_type,
      :salesforce_contact_id,
      :support_identifier,
      :is_test
    ]

    attr_accessor :syncing

    enum faculty_status: [:no_faculty_info, :pending_faculty, :confirmed_faculty, :rejected_faculty]
    enum role: [
      :unknown_role,
      :student,
      :instructor,
      :administrator,
      :librarian,
      :designer,
      :other,
      :adjunct,
      :homeschool
    ]
    enum school_type: [:unknown_school_type, :other_school_type, :college]

    validates :faculty_status, :role, :school_type, presence: true

    validates :uuid, presence: true, uniqueness: true
    validates :support_identifier, uniqueness: { allow_nil: true }

    before_update :update_openstax_accounts, if: :should_send_updates_to_accounts?

    def name
      (first_name || last_name) ? [first_name, last_name].compact.join(" ") : username
    end

    def casual_name
      first_name || username
    end

    def is_anonymous?
      false
    end

    def has_authenticated?
      !access_token.nil?
    end

    def valid_openstax_uid?
      !openstax_uid.nil? && openstax_uid > 0
    end

    def self_reported_role
      role
    end

    def self_reported_role=(role)
      self.role = role
    end

    protected

    def syncing_or_stubbing?
      syncing || OpenStax::Accounts.configuration.enable_stubbing?
    end

    def should_send_updates_to_accounts?
      !syncing_or_stubbing? && valid_openstax_uid?
    end

    def update_openstax_accounts
      OpenStax::Accounts::Api.update_account(self)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
openstax_accounts-9.1.0 app/models/openstax/accounts/account.rb
openstax_accounts-9.0.2 app/models/openstax/accounts/account.rb
openstax_accounts-9.0.1 app/models/openstax/accounts/account.rb
openstax_accounts-9.0.0 app/models/openstax/accounts/account.rb