Sha256: b5e5395836cc5d3f0463751c2bed1fbb1f06a0abe3340e11586c9c44fb7b2fd7

Contents?: true

Size: 824 Bytes

Versions: 7

Compression:

Stored size: 824 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 :course,   presence: true
    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

7 entries across 7 versions & 1 rubygems

Version Path
ecm_courses2-1.0.0 app/models/ecm/courses/course_date.rb
ecm_courses2-0.2.4 app/models/ecm/courses/course_date.rb
ecm_courses2-0.2.3 app/models/ecm/courses/course_date.rb
ecm_courses2-0.2.2 app/models/ecm/courses/course_date.rb
ecm_courses2-0.2.1 app/models/ecm/courses/course_date.rb
ecm_courses2-0.2.0 app/models/ecm/courses/course_date.rb
ecm_courses2-0.1.0 app/models/ecm/courses/course_date.rb