add default awakening, sorting, filtering scopes to CollectionCharacter
This commit is contained in:
parent
5bc179afa8
commit
e97b0ade55
1 changed files with 28 additions and 0 deletions
|
|
@ -3,6 +3,8 @@ class CollectionCharacter < ApplicationRecord
|
||||||
belongs_to :character
|
belongs_to :character
|
||||||
belongs_to :awakening, optional: true
|
belongs_to :awakening, optional: true
|
||||||
|
|
||||||
|
before_save :add_default_awakening
|
||||||
|
|
||||||
validates :character_id, uniqueness: { scope: :user_id,
|
validates :character_id, uniqueness: { scope: :user_id,
|
||||||
message: "already exists in your collection" }
|
message: "already exists in your collection" }
|
||||||
validates :uncap_level, inclusion: { in: 0..5 }
|
validates :uncap_level, inclusion: { in: 0..5 }
|
||||||
|
|
@ -26,6 +28,26 @@ class CollectionCharacter < ApplicationRecord
|
||||||
scope :transcended, -> { where('transcendence_step > 0') }
|
scope :transcended, -> { where('transcendence_step > 0') }
|
||||||
scope :with_awakening, -> { where.not(awakening_id: nil) }
|
scope :with_awakening, -> { where.not(awakening_id: nil) }
|
||||||
|
|
||||||
|
# Sorting scopes
|
||||||
|
scope :sorted_by, ->(sort_key) {
|
||||||
|
case sort_key
|
||||||
|
when 'name_asc'
|
||||||
|
joins(:character).order('characters.name_en ASC NULLS LAST')
|
||||||
|
when 'name_desc'
|
||||||
|
joins(:character).order('characters.name_en DESC NULLS LAST')
|
||||||
|
when 'element_asc'
|
||||||
|
joins(:character).order('characters.element ASC')
|
||||||
|
when 'element_desc'
|
||||||
|
joins(:character).order('characters.element DESC')
|
||||||
|
when 'proficiency_asc'
|
||||||
|
joins(:character).order('characters.proficiency1 ASC')
|
||||||
|
when 'proficiency_desc'
|
||||||
|
joins(:character).order('characters.proficiency1 DESC')
|
||||||
|
else
|
||||||
|
order(created_at: :desc) # Default: newest first
|
||||||
|
end
|
||||||
|
}
|
||||||
|
|
||||||
def blueprint
|
def blueprint
|
||||||
Api::V1::CollectionCharacterBlueprint
|
Api::V1::CollectionCharacterBlueprint
|
||||||
end
|
end
|
||||||
|
|
@ -61,4 +83,10 @@ class CollectionCharacter < ApplicationRecord
|
||||||
errors.add(:transcendence_step, "requires uncap level 5 (current: #{uncap_level})")
|
errors.add(:transcendence_step, "requires uncap level 5 (current: #{uncap_level})")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def add_default_awakening
|
||||||
|
return unless awakening.nil?
|
||||||
|
|
||||||
|
self.awakening = Awakening.where(slug: 'character-balanced').sole
|
||||||
|
end
|
||||||
end
|
end
|
||||||
Loading…
Reference in a new issue