LafayetteCollegeLibraries/spot

View on GitHub
app/models/featured_collection.rb

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
# frozen_string_literal: true
class FeaturedCollection < ApplicationRecord
  FEATURE_LIMIT = 4

  validate :count_within_limit, on: :create
  validates :order, inclusion: { in: proc { 0..FEATURE_LIMIT } }

  # @return [true,false]
  def self.can_create_another?
    count < FEATURE_LIMIT
  end

  private

  # @return [void]
  def count_within_limit
    return if FeaturedCollection.can_create_another?
    errors.add(:base, "Limited to #{FEATURE_LIMIT} featured collections.")
  end
end