add sorting and filtering to collection weapons

This commit is contained in:
Justin Edmund 2025-12-16 21:15:44 -08:00
parent 0c9d1d8e06
commit 75862aec03
2 changed files with 24 additions and 0 deletions

View file

@ -19,6 +19,10 @@ module Api
@collection_weapons = @collection_weapons.by_weapon(params[:weapon_id]) if params[:weapon_id]
@collection_weapons = @collection_weapons.by_element(params[:element]) if params[:element]
@collection_weapons = @collection_weapons.by_rarity(params[:rarity]) if params[:rarity]
@collection_weapons = @collection_weapons.by_proficiency(params[:proficiency]) if params[:proficiency]
@collection_weapons = @collection_weapons.by_series(params[:series]) if params[:series]
@collection_weapons = @collection_weapons.sorted_by(params[:sort])
@collection_weapons = @collection_weapons.paginate(page: params[:page], per_page: params[:limit] || 50)

View file

@ -28,9 +28,29 @@ class CollectionWeapon < ApplicationRecord
scope :with_ax, -> { where.not(ax_modifier1: nil) }
scope :by_element, ->(element) { joins(:weapon).where(weapons: { element: element }) }
scope :by_rarity, ->(rarity) { joins(:weapon).where(weapons: { rarity: rarity }) }
scope :by_proficiency, ->(proficiency) { joins(:weapon).where(weapons: { proficiency: proficiency }) }
scope :transcended, -> { where('transcendence_step > 0') }
scope :with_awakening, -> { where.not(awakening_id: nil) }
scope :sorted_by, ->(sort_key) {
case sort_key
when 'name_asc'
joins(:weapon).order('weapons.name_en ASC NULLS LAST')
when 'name_desc'
joins(:weapon).order('weapons.name_en DESC NULLS LAST')
when 'element_asc'
joins(:weapon).order('weapons.element ASC')
when 'element_desc'
joins(:weapon).order('weapons.element DESC')
when 'proficiency_asc'
joins(:weapon).order('weapons.proficiency ASC')
when 'proficiency_desc'
joins(:weapon).order('weapons.proficiency DESC')
else
order(created_at: :desc)
end
}
def blueprint
Api::V1::CollectionWeaponBlueprint
end