app/models/openstax/accounts/account.rb in openstax_accounts-7.2.0 vs app/models/openstax/accounts/account.rb in openstax_accounts-7.3.0
- old
+ new
@@ -18,19 +18,21 @@
primary_key: :openstax_uid,
foreign_key: :user_id,
inverse_of: :user
has_many :groups_as_member, through: :group_members, source: :group
- enum faculty_status: [:no_faculty_info, :pending_faculty, :confirmed_faculty, :rejected_faculty]
- after_initialize { self.faculty_status ||= :no_faculty_info }
+ enum faculty_status: [:no_faculty_info, :pending_faculty,
+ :confirmed_faculty, :rejected_faculty]
+
+ after_initialize :set_default_faculty_status
+
validates :faculty_status, presence: true
- validates :openstax_uid, presence: true, uniqueness: true
- validates :username, presence: true, uniqueness: true,
- unless: :syncing_or_stubbing
+ validates :openstax_uid, uniqueness: { allow_nil: true }
+ validates :username, uniqueness: { allow_nil: true }
- before_update :update_openstax_accounts, unless: :syncing_or_stubbing
+ 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
@@ -44,13 +46,25 @@
def has_authenticated?
!access_token.nil?
end
+ def valid_openstax_uid?
+ !openstax_uid.nil? && openstax_uid > 0
+ end
+
protected
- def syncing_or_stubbing
+ def set_default_faculty_status
+ self.faculty_status ||= :no_faculty_info
+ end
+
+ 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