vol1ura/Sat_9am_5km

View on GitHub
app/models/event.rb

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
# frozen_string_literal: true

class Event < ApplicationRecord
  belongs_to :country
  has_many :activities, dependent: :destroy
  has_many :athletes, dependent: :nullify
  has_many :contacts, dependent: :destroy
  has_many :volunteering_positions, dependent: :destroy

  validates :name, :code_name, :town, :place, presence: true
  validates :code_name, uniqueness: true, format: { with: /\A[a-z_]+\z/ }

  default_scope { order(:visible_order, :name) }

  scope :in_country, ->(country_code) { joins(:country).where(country: { code: country_code }) }
  scope :without_friends, -> { where.not(id: 4) }

  def self.ransackable_attributes(_auth_object = nil)
    %w[code_name country_id name place town]
  end

  def self.authorized_for(user)
    return all if user.admin?

    where(id: user.permissions.where(subject_class: 'Activity').select(:event_id))
  end

  def to_combobox_display
    name
  end
end