-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding feature limit class attribute (#6680)
* Adding feature limit class attribute * 🐛 Fix subject of spec
- Loading branch information
Showing
2 changed files
with
21 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,29 @@ | ||
# frozen_string_literal: true | ||
class FeaturedWork < ActiveRecord::Base | ||
FEATURE_LIMIT = 5 | ||
## | ||
# @!group Class Attributes | ||
# | ||
# @!attribute feature_limit [r|w] | ||
# @return [Integer] | ||
class_attribute :feature_limit, default: 5 | ||
# @!endgroup Class Attributes | ||
## | ||
|
||
validate :count_within_limit, on: :create | ||
validates :order, inclusion: { in: proc { 0..FEATURE_LIMIT } } | ||
validates :order, inclusion: { in: proc { 0..feature_limit } } | ||
|
||
default_scope { order(:order) } | ||
|
||
def count_within_limit | ||
return if FeaturedWork.can_create_another? | ||
errors.add(:base, "Limited to #{FEATURE_LIMIT} featured works.") | ||
errors.add(:base, "Limited to #{feature_limit} featured works.") | ||
end | ||
|
||
attr_accessor :presenter | ||
|
||
class << self | ||
def can_create_another? | ||
FeaturedWork.count < FEATURE_LIMIT | ||
FeaturedWork.count < feature_limit | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters