Class: Effective::LearndashUser
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Effective::LearndashUser
- Defined in:
- app/models/effective/learndash_user.rb
Instance Method Summary collapse
-
#assign_api_attributes(data = nil) ⇒ Object
Assign my model attributes from API.
-
#assign_api_course_enrollments ⇒ Object
This synchronizes all the course enrollments from the API down locally.
-
#build_enrollment(course:) ⇒ Object
Find or build.
-
#create_enrollment(course:) ⇒ Object
Create.
-
#enrollment(course:) ⇒ Object
Find.
- #refresh!(force: false) ⇒ Object
- #to_s ⇒ Object
- #update_password! ⇒ Object
Instance Method Details
#assign_api_attributes(data = nil) ⇒ Object
Assign my model attributes from API. These should never change.
75 76 77 78 79 80 81 82 |
# File 'app/models/effective/learndash_user.rb', line 75 def assign_api_attributes(data = nil) data ||= EffectiveLearndash.api.find_user(owner) || EffectiveLearndash.api.create_user(owner) # Take special care not to overwrite password. We only get password once. self.password ||= (data[:password].presence || 'unknown') assign_attributes(email: data[:email], user_id: data[:id], username: data[:username], last_synced_at: Time.zone.now) end |
#assign_api_course_enrollments ⇒ Object
This synchronizes all the course enrollments from the API down locally.
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'app/models/effective/learndash_user.rb', line 85 def assign_api_course_enrollments return if EffectiveLearndash.disabled? raise('must be persisted') unless persisted? courses = LearndashCourse.all() EffectiveLearndash.api.user_enrollments(self).each do |data| course = courses.find { |course| course.course_id == data[:course] } raise("unable to find local persisted learndash course for id #{data[:course]}. Run Effective::LearndashCourse.sync!") unless course.present? enrollment = build_enrollment(course: course) enrollment.assign_api_attributes(data) end assign_attributes(last_synced_at: Time.zone.now) end |
#build_enrollment(course:) ⇒ Object
Find or build
63 64 65 |
# File 'app/models/effective/learndash_user.rb', line 63 def build_enrollment(course:) enrollment(course: course) || learndash_enrollments.build(learndash_course: course) end |
#create_enrollment(course:) ⇒ Object
Create
68 69 70 71 72 |
# File 'app/models/effective/learndash_user.rb', line 68 def create_enrollment(course:) enrollment = build_enrollment(course: course) enrollment.save! enrollment end |
#enrollment(course:) ⇒ Object
Find
58 59 60 |
# File 'app/models/effective/learndash_user.rb', line 58 def enrollment(course:) learndash_enrollments.find { |enrollment| enrollment.learndash_course_id == course.id } end |
#refresh!(force: false) ⇒ Object
47 48 49 50 51 52 53 54 55 |
# File 'app/models/effective/learndash_user.rb', line 47 def refresh!(force: false) unless force return if last_synced_at.present? && (Time.zone.now - last_synced_at) < 5 return if learndash_enrollments.none? { |enrollment| !enrollment.completed? } end assign_api_course_enrollments save! end |
#to_s ⇒ Object
43 44 45 |
# File 'app/models/effective/learndash_user.rb', line 43 def to_s owner&.to_s || username.presence || 'learndash user' end |
#update_password! ⇒ Object
103 104 105 106 107 |
# File 'app/models/effective/learndash_user.rb', line 103 def update_password! save! EffectiveLearndash.api.update_password(owner, password) true end |