Class: Effective::CourseFeeHistory

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/effective/course_fee_history.rb

Overview

A dated set of prices for a learndash course.

Prices used to live in flat columns (regular_price, member_price) directly on learndash_courses. They now live here so a price change can be scheduled ahead of time: each CourseFeeHistory covers a date range (start_on..end_on), and the price for a given date is read from whichever history covers it.

There should always be exactly one open-ended (end_on: nil) history per course — the currently active prices. Read them via LearndashCourse#prices.

Instance Method Summary collapse

Instance Method Details

#duplicate!Object

Schedule next year's prices: close the current (open-ended) prices at the end of this year and open a copy for next year, ready to have its new prices set.



44
45
46
47
48
49
50
51
52
# File 'app/models/effective/course_fee_history.rb', line 44

def duplicate!
  transaction do
    copy = dup
    copy.assign_attributes(start_on: (Time.zone.now + 1.year).beginning_of_year, end_on: nil)
    copy.save!

    update!(end_on: Time.zone.now.end_of_year)
  end
end

#to_sObject



38
39
40
# File 'app/models/effective/course_fee_history.rb', line 38

def to_s
  "#{learndash_course} Fees #{start_on&.year}#{" - #{end_on.year}" if end_on.present?}"
end