Module: EffectiveLearndashOwner

Extended by:
ActiveSupport::Concern
Defined in:
app/models/concerns/effective_learndash_owner.rb

Overview

EffectiveLearndashOwner

Mark your user model with effective_learndash_owner to get all the includes

Defined Under Namespace

Modules: Base, ClassMethods

Instance Method Summary collapse

Instance Method Details

#completed_learndash_coursesObject



54
55
56
# File 'app/models/concerns/effective_learndash_owner.rb', line 54

def completed_learndash_courses
  learndash_enrollments.select(&:completed?).map(&:course).sort_by(&:id)
end

#completed_learndash_enrollmentsObject



50
51
52
# File 'app/models/concerns/effective_learndash_owner.rb', line 50

def completed_learndash_enrollments
  learndash_enrollments.select(&:completed?).sort_by(&:created_at)
end

#create_learndash_enrollment(course:) ⇒ Object

Find or create



73
74
75
76
# File 'app/models/concerns/effective_learndash_owner.rb', line 73

def create_learndash_enrollment(course:)
  raise('expected a persisted learndash_user') unless learndash_user&.persisted?
  learndash_user.create_enrollment(course: course)
end

#create_learndash_userObject

Find or create



63
64
65
# File 'app/models/concerns/effective_learndash_owner.rb', line 63

def create_learndash_user
  learndash_user || learndash_users.create!(owner: self)
end

#in_progress_learndash_enrollmentsObject



46
47
48
# File 'app/models/concerns/effective_learndash_owner.rb', line 46

def in_progress_learndash_enrollments
  learndash_enrollments.reject(&:completed?).sort_by(&:created_at)
end

#learndash_completed?(course:) ⇒ Boolean

Find or sync and check completed?

Returns:

  • (Boolean)


79
80
81
82
83
84
85
86
87
88
89
# File 'app/models/concerns/effective_learndash_owner.rb', line 79

def learndash_completed?(course:)
  enrollment = learndash_enrollment(course: course)

  # We haven't been enrolled
  return false if enrollment.blank?

  # Return completed right away if previously marked completed
  enrollment.completed?

  # DO NOT check API
end

#learndash_enroll!(courses:) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/models/concerns/effective_learndash_owner.rb', line 91

def learndash_enroll!(courses:)
  courses = Array(courses)

  # Find or create the user
  create_learndash_user
  raise('expected a persisted learndash user') unless learndash_user&.persisted?

  courses.each do |course|
    # Find or Enroll in the course
    create_learndash_enrollment(course: course)
    raise('expected a persisted learndash enrollment') unless learndash_enrollment(course: course)&.persisted?
  end

  # This syncs the learndash enrollment locally
  learndash_enrollments.select { |enrollment| courses.include?(enrollment.learndash_course) }.each do |enrollment|
    enrollment.refresh! unless enrollment.completed?
  end

  save!

  after_learndash_enroll() if respond_to?(:after_learndash_enroll)

  true
end

#learndash_enrolled_coursesObject



58
59
60
# File 'app/models/concerns/effective_learndash_owner.rb', line 58

def learndash_enrolled_courses
  learndash_enrollments.map { |enrollment| enrollment.learndash_course }
end

#learndash_enrollment(course:) ⇒ Object

Find



68
69
70
# File 'app/models/concerns/effective_learndash_owner.rb', line 68

def learndash_enrollment(course:)
  learndash_user&.enrollment(course: course)
end

#learndash_refresh!(force: false) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'app/models/concerns/effective_learndash_owner.rb', line 116

def learndash_refresh!(force: false)
  raise('expected a previously persisted learndash user') if learndash_user.blank?

  learndash_enrollments.each do |enrollment|
    enrollment.refresh!(force: force) unless (force || enrollment.completed?)
  end

  save!

  after_learndash_refresh() if respond_to?(:after_learndash_refresh)

  true
end

#learndash_userObject

Find



42
43
44
# File 'app/models/concerns/effective_learndash_owner.rb', line 42

def learndash_user
  learndash_users.first
end