Sha256: e3842921038560aad60e0b6f6d6c9cde8746da957a2936fb7bffb577f991d661

Contents?: true

Size: 784 Bytes

Versions: 2

Compression:

Stored size: 784 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

    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

2 entries across 2 versions & 1 rubygems

Version Path
ecm_courses2-1.0.2 app/models/ecm/courses/course_date.rb
ecm_courses2-1.0.1 app/models/ecm/courses/course_date.rb