Sha256: 51c0df6945f27a60b18706a46e26a8807da3b44b8475290826eed679e0dea4cf

Contents?: true

Size: 841 Bytes

Versions: 6

Compression:

Stored size: 841 Bytes

Contents

module Ecm::Courses
  class CourseDate < ApplicationRecord
    # associations
    belongs_to :course

    has_one :course_category,
            through: :course

    # callbacks
    after_initialize :set_defaults, if: :new_record?

    # validations
    validates :end_at,   presence: true
    validates :start_at, presence: true

    validates :end_at, timeliness: { after: :start_at }

    def self.for_month(date)
      date ||= Time.zone.now.to_date
      where(start_at: (date.beginning_of_month..(date.end_of_month + 1.day)))
    end

    def duration_in_minutes
      (end_at - start_at).to_i / 60
    end

    def to_s
      "#{I18n.l(start_at)} - #{I18n.l(end_at)}"
    end

    protected

    def set_defaults
      self.start_at ||= 6.hours.from_now.change(min: 0)
      self.end_at   ||= self.start_at + 1.hours
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
ecm_courses2-1.1.4 app/models/ecm/courses/course_date.rb
ecm_courses2-1.1.3 app/models/ecm/courses/course_date.rb
ecm_courses2-1.1.2 app/models/ecm/courses/course_date.rb
ecm_courses2-1.1.1 app/models/ecm/courses/course_date.rb
ecm_courses2-1.1.0 app/models/ecm/courses/course_date.rb
ecm_courses2-1.0.3 app/models/ecm/courses/course_date.rb