Sha256: 44c96d0ada4fbddfc8dfb84755dce7d9397efefa09ba0b99ce76a3bfc3023bdc

Contents?: true

Size: 1.08 KB

Versions: 34

Compression:

Stored size: 1.08 KB

Contents

class Invitation < ApplicationRecord
  include Mumuki::Domain::Syncable

  belongs_to :course

  validate :ensure_not_expired, on: :create
  validates_uniqueness_of :code

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

  def ensure_not_expired
    errors.add(:base, :invitation_expired) if expired?
  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 Mumuki::Domain::GoneError, "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

34 entries across 34 versions & 2 rubygems

Version Path
mumuki-domain-8.6.1 app/models/invitation.rb
mumuki-domain-8.6.0 app/models/invitation.rb
mumuki-domain-8.5.0 app/models/invitation.rb
mumuki-domain-8.4.0 app/models/invitation.rb
mumuki-domain-8.3.1 app/models/invitation.rb
mumuki-domain-8.2.0 app/models/invitation.rb
mumuki-domain-8.1.3 app/models/invitation.rb
mumuki-domain-8.1.2 app/models/invitation.rb
mumuki-domain-8.1.1 app/models/invitation.rb
mumuki-domain-8.1.0 app/models/invitation.rb
mumuki-domain-8.0.0 app/models/invitation.rb
mumuki-domain-7.12.2 app/models/invitation.rb
mumuki-domain-7.12.1 app/models/invitation.rb
mumuki-domain-7.12.0 app/models/invitation.rb
mumuki-domain-7.11.1 app/models/invitation.rb
mumuki-laboratory-7.11.1 vendor/bundle/ruby/2.6.0/bundler/gems/mumuki-domain-c92f2a7212e2/app/models/invitation.rb
mumuki-laboratory-7.11.0 vendor/bundle/ruby/2.6.0/bundler/gems/mumuki-domain-2e15c3330133/app/models/invitation.rb
mumuki-domain-7.11.0 app/models/invitation.rb
mumuki-domain-7.10.1 app/models/invitation.rb
mumuki-domain-7.10.0 app/models/invitation.rb