Class: Effective::LearndashCourse
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Effective::LearndashCourse
- Defined in:
- app/models/effective/learndash_course.rb
Class Method Summary collapse
-
.refresh! ⇒ Object
Syncs all courses.
Instance Method Summary collapse
- #body ⇒ Object
-
#course_fee_histories_warnings ⇒ Object
Non-blocking warnings about the price timeline, shown on the admin edit screen.
-
#draft? ⇒ Boolean
Todo.
- #member_price(date: nil) ⇒ Object
-
#prices(date: nil) ⇒ Object
The CourseFeeHistory (price list) in effect on the given date — today by default.
-
#regular_price(date: nil) ⇒ Object
Backwards-compatible readers so existing call sites keep working after prices moved to dated histories.
- #to_s ⇒ Object
Class Method Details
.refresh! ⇒ Object
Syncs all courses
91 92 93 94 95 96 97 98 99 100 |
# File 'app/models/effective/learndash_course.rb', line 91 def self.refresh! courses = all() EffectiveLearndash.api.courses.each do |data| course = courses.find { |course| course.course_id == data[:id] } || new() course.update!(course_id: data[:id], title: data.dig(:title, :rendered), status: data[:status], link: data[:link]) end true end |
Instance Method Details
#body ⇒ Object
106 107 108 |
# File 'app/models/effective/learndash_course.rb', line 106 def body rich_text_body end |
#course_fee_histories_warnings ⇒ Object
Non-blocking warnings about the price timeline, shown on the admin edit screen. Prices should form one continuous timeline with a single open-ended (current) history.
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'app/models/effective/learndash_course.rb', line 143 def course_fee_histories_warnings histories = course_fee_histories.sort_by(&:start_on) return ['No prices have been set — add a course fee history.'] if histories.empty? warnings = [] current = histories.select { |history| history.end_on.blank? } warnings << 'There are no current prices — the most recent history should have no end date.' if current.empty? warnings << 'There is more than one current history (more than one with no end date).' if current.size > 1 # Each history should pick up the day after the previous one ends — no gaps, no overlaps. histories.each_cons(2) do |earlier, later| next if earlier.end_on.blank? # an open-ended history in the middle is flagged above if later.start_on > earlier.end_on + 1.day warnings << "Gap in prices between #{earlier.end_on} and #{later.start_on}." elsif later.start_on <= earlier.end_on warnings << "Overlapping prices around #{later.start_on}." end end warnings end |
#draft? ⇒ Boolean
Todo
111 112 113 |
# File 'app/models/effective/learndash_course.rb', line 111 def draft? false end |
#member_price(date: nil) ⇒ Object
137 138 139 |
# File 'app/models/effective/learndash_course.rb', line 137 def member_price(date: nil) prices(date: date).member_fee end |
#prices(date: nil) ⇒ Object
The CourseFeeHistory (price list) in effect on the given date — today by default. Prices used to live in flat columns here; read them off this record now:
course.prices.regular_fee # price right now
course.prices(date: date).member_fee # price in effect on date
Raises if no history covers the date so misconfiguration surfaces loudly.
122 123 124 125 126 127 128 129 |
# File 'app/models/effective/learndash_course.rb', line 122 def prices(date: nil) date = (date || Time.zone.now).to_date # course_fee_histories is ordered start_on desc — pick the newest window covering the # date. end_on nil makes an endless range (start_on..), i.e. the current prices. course_fee_histories.find { |history| (history.start_on..history.end_on).cover?(date) } || raise("No course_fee_history prices available for #{self} on #{date.strftime('%F')}, add a course fee history covering that date") end |
#regular_price(date: nil) ⇒ Object
Backwards-compatible readers so existing call sites keep working after prices moved to dated histories. These override the retired flat columns.
133 134 135 |
# File 'app/models/effective/learndash_course.rb', line 133 def regular_price(date: nil) prices(date: date).regular_fee end |
#to_s ⇒ Object
102 103 104 |
# File 'app/models/effective/learndash_course.rb', line 102 def to_s title.presence || 'learndash course' end |