vol1ura/Sat_9am_5km

View on GitHub
app/models/badge.rb

Summary

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

class Badge < ApplicationRecord
  BADGE_TYPES = %w[result volunteer].freeze
  ASSOCIATION_TYPE_MAPPING = {
    'result' => :results,
    'volunteer' => :volunteering,
  }.freeze

  has_many :trophies, dependent: :destroy
  has_many :athletes, through: :trophies

  has_one_attached :image do |attachable|
    attachable.variant :thumb, resize_to_limit: [100, 100], preprocessed: true
    attachable.variant :web, resize_to_limit: [200, 200], preprocessed: true
  end

  validates :kind, :name, presence: true
  validates :image, attached: true,
                    content_type: :png,
                    aspect_ratio: :square,
                    dimension: { width: { in: 200..400 } },
                    size: { less_than: 300.kilobytes }

  enum :kind, {
    funrun: 0, full_profile: 1, participating: 10, home_participating: 11, jubilee_participating: 12,
    tourist: 20, breaking: 30, rage: 40, five_plus: 41, minute_bingo: 50, record: 100
  }, suffix: true

  def self.dataset_of(kind:, type:)
    public_send(:"#{kind}_kind").where("info->>'type' = ?", type).order(Arel.sql("info->'threshold'"))
  end

  def self.ransackable_attributes(_auth_object = nil)
    %w[conditions kind name received_date]
  end
end