Sha256: 82d2c7be4659505e70e62e2c369bf271b31022916bdd7de71e6d35b5d073b61f

Contents?: true

Size: 946 Bytes

Versions: 10

Compression:

Stored size: 946 Bytes

Contents

class Invitation < ApplicationRecord
  include Syncable

  belongs_to :course
  validates_uniqueness_of :code

  defaults do
    self.code ||= self.class.generate_code
  end

  def import_from_resource_h!(json)
    update! json.merge(course: Course.locate!(json[:course]))
  end

  def organization
    course.organization
  end

  def course_slug
    course.slug
  end

  def navigable_name
    I18n.t(:invitation_for, course: course_name)
  end

  def to_resource_h
    { code: code, course: course_slug, expiration_date: expiration_date }
  end

  def navigation_end?
    true
  end

  def to_param
    code
  end

  def expired?
    Time.now > expiration_date
  end

  def unexpired
    raise RecordNotFound, "This invitation has already expired" if expired?
    self
  end

  def self.generate_code
    SecureRandom.urlsafe_base64 4
  end

  private

  def course_name
    course.name
  end

  def self.sync_key_id_field
    :code
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
mumuki-domain-6.5.1 app/models/invitation.rb
mumuki-domain-6.5.0 app/models/invitation.rb
mumuki-domain-6.4.3 app/models/invitation.rb
mumuki-domain-6.4.2 app/models/invitation.rb
mumuki-domain-6.4.1 app/models/invitation.rb
mumuki-domain-6.4.0 app/models/invitation.rb
mumuki-domain-6.2.2 app/models/invitation.rb
mumuki-domain-6.2.1 app/models/invitation.rb
mumuki-domain-6.3.0 app/models/invitation.rb
mumuki-domain-6.2.0 app/models/invitation.rb