Sha256: 3fcedf88af2b7709a59ef208ad8cbe1139a1089b4bfeaa0fcdd444760301c056

Contents?: true

Size: 1.88 KB

Versions: 16

Compression:

Stored size: 1.88 KB

Contents

class Course < ApplicationRecord
  include Mumuki::Domain::Syncable
  include Mumuki::Domain::Helpers::Course
  include Mumuki::Domain::Area

  validates_presence_of :slug, :period, :code, :description, :organization_id
  validates_uniqueness_of :slug
  belongs_to :organization

  has_many :invitations

  alias_attribute :name, :code

  resource_fields :slug, :shifts, :code, :days, :period, :description, :period_start, :period_end

  def current_invitation
    invitations.where('expiration_date > ?', Time.now).first
  end

  def import_from_resource_h!(resource_h)
    update! self.class.slice_resource_h(resource_h)
  end

  def slug=(slug)
    s = slug.to_mumukit_slug
    self[:slug] = slug.to_s
    self[:organization_id] = Organization.locate!(s.organization).id
  end

  def invite!(expiration_date)
    if closed?
      generate_invitation! expiration_date
    else
      current_invitation
    end
  end

  def ended?
    period_end.present? && period_end.past?
  end

  def started?
    period_start.present? && period_start.past?
  end

  def infer_period_range!
    return if period_start || period_end

    period =~ /^(\d{4})?/
    year = $1.to_i

    return nil unless year.between? 2014, (DateTime.now.year + 1)

    self.period_start = DateTime.new(year).beginning_of_year
    self.period_end = DateTime.new(year).end_of_year
  end

  def canonical_code
    "#{period}-#{code}".downcase
  end

  def closed?
    current_invitation.blank? || current_invitation.expired?
  end

  def generate_invitation!(expiration_date)
    invitations.create expiration_date: expiration_date, course: self
    current_invitation
  end

  def self.sync_key_id_field
    :slug
  end

  def to_organization
    organization
  end

  def to_s
    slug.to_s
  end

  def self.allowed_for(user, organization = Organization.current)
    where(organization: organization).select { |course| user.teacher_of? course.slug }
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
mumuki-domain-9.8.1 app/models/course.rb
mumuki-domain-9.8.0 app/models/course.rb
mumuki-domain-9.7.0 app/models/course.rb
mumuki-domain-9.6.0 app/models/course.rb
mumuki-domain-9.5.0 app/models/course.rb
mumuki-domain-9.4.0 app/models/course.rb
mumuki-domain-9.3.0 app/models/course.rb
mumuki-domain-9.2.1 app/models/course.rb
mumuki-domain-9.2.0 app/models/course.rb
mumuki-domain-9.1.2 app/models/course.rb
mumuki-domain-9.1.1 app/models/course.rb
mumuki-domain-9.1.0 app/models/course.rb
mumuki-domain-9.0.6 app/models/course.rb
mumuki-domain-9.0.5 app/models/course.rb
mumuki-domain-9.0.4 app/models/course.rb
mumuki-domain-9.0.3 app/models/course.rb