add max_exorcism_level to weapons (#205)
* add max_exorcism_level to weapons - migration to add column (nullable integer) - expose in blueprint - permit in controller - add spec for create/update * default exorcism_level=1 for befoulment weapons - set default on create for GridWeapon and CollectionWeapon - data migration to populate existing befoulment weapons - add specs for default behavior
This commit is contained in:
parent
f0b2d1fff7
commit
34e3bbd03b
10 changed files with 303 additions and 3 deletions
|
|
@ -12,8 +12,8 @@ module Api
|
||||||
|
|
||||||
# Primary information
|
# Primary information
|
||||||
fields :granblue_id, :element, :proficiency,
|
fields :granblue_id, :element, :proficiency,
|
||||||
:max_level, :max_skill_level, :max_awakening_level, :limit, :rarity,
|
:max_level, :max_skill_level, :max_awakening_level, :max_exorcism_level,
|
||||||
:ax, :ax_type, :gacha, :promotions, :forge_order, :extra
|
:limit, :rarity, :ax, :ax_type, :gacha, :promotions, :forge_order, :extra
|
||||||
|
|
||||||
# Series - returns full object with flags if weapon_series is present, fallback to legacy integer
|
# Series - returns full object with flags if weapon_series is present, fallback to legacy integer
|
||||||
field :series do |w|
|
field :series do |w|
|
||||||
|
|
|
||||||
|
|
@ -219,7 +219,7 @@ module Api
|
||||||
:flb, :ulb, :transcendence, :extra, :extra_prerequisite, :limit, :ax, :gacha,
|
:flb, :ulb, :transcendence, :extra, :extra_prerequisite, :limit, :ax, :gacha,
|
||||||
:min_hp, :max_hp, :max_hp_flb, :max_hp_ulb,
|
:min_hp, :max_hp, :max_hp_flb, :max_hp_ulb,
|
||||||
:min_atk, :max_atk, :max_atk_flb, :max_atk_ulb,
|
:min_atk, :max_atk, :max_atk_flb, :max_atk_ulb,
|
||||||
:max_level, :max_skill_level, :max_awakening_level,
|
:max_level, :max_skill_level, :max_awakening_level, :max_exorcism_level,
|
||||||
:release_date, :flb_date, :ulb_date, :transcendence_date,
|
:release_date, :flb_date, :ulb_date, :transcendence_date,
|
||||||
:wiki_en, :wiki_ja, :wiki_raw, :gamewith, :kamigame,
|
:wiki_en, :wiki_ja, :wiki_raw, :gamewith, :kamigame,
|
||||||
:recruits, :forged_from, :forge_chain_id, :forge_order,
|
:recruits, :forged_from, :forge_chain_id, :forge_order,
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,7 @@ class CollectionWeapon < ApplicationRecord
|
||||||
has_many :grid_weapons, dependent: :nullify
|
has_many :grid_weapons, dependent: :nullify
|
||||||
|
|
||||||
before_destroy :orphan_grid_items
|
before_destroy :orphan_grid_items
|
||||||
|
before_validation :set_default_exorcism_level, on: :create
|
||||||
|
|
||||||
# Set defaults before validation so database defaults don't cause validation failures
|
# Set defaults before validation so database defaults don't cause validation failures
|
||||||
attribute :awakening_level, :integer, default: 1
|
attribute :awakening_level, :integer, default: 1
|
||||||
|
|
@ -174,4 +175,16 @@ class CollectionWeapon < ApplicationRecord
|
||||||
def orphan_grid_items
|
def orphan_grid_items
|
||||||
grid_weapons.update_all(orphaned: true, collection_weapon_id: nil)
|
grid_weapons.update_all(orphaned: true, collection_weapon_id: nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
##
|
||||||
|
# Sets default exorcism_level to 1 for befoulment weapons if not provided.
|
||||||
|
#
|
||||||
|
# @return [void]
|
||||||
|
def set_default_exorcism_level
|
||||||
|
return unless weapon.present?
|
||||||
|
return unless exorcism_level.nil?
|
||||||
|
return unless weapon.weapon_series&.augment_type == 'befoulment'
|
||||||
|
|
||||||
|
self.exorcism_level = 1
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
@ -56,6 +56,7 @@ class GridWeapon < ApplicationRecord
|
||||||
validate :no_conflicts, on: :create
|
validate :no_conflicts, on: :create
|
||||||
|
|
||||||
before_save :assign_mainhand
|
before_save :assign_mainhand
|
||||||
|
before_validation :set_default_exorcism_level, on: :create
|
||||||
|
|
||||||
##### Amoeba configuration
|
##### Amoeba configuration
|
||||||
amoeba do
|
amoeba do
|
||||||
|
|
@ -245,4 +246,16 @@ class GridWeapon < ApplicationRecord
|
||||||
def assign_mainhand
|
def assign_mainhand
|
||||||
self.mainhand = (position == -1)
|
self.mainhand = (position == -1)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
##
|
||||||
|
# Sets default exorcism_level to 1 for befoulment weapons if not provided.
|
||||||
|
#
|
||||||
|
# @return [void]
|
||||||
|
def set_default_exorcism_level
|
||||||
|
return unless weapon.present?
|
||||||
|
return unless exorcism_level.nil?
|
||||||
|
return unless weapon.weapon_series&.augment_type == 'befoulment'
|
||||||
|
|
||||||
|
self.exorcism_level = 1
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
20
db/data/20260104000002_populate_max_exorcism_level.rb
Normal file
20
db/data/20260104000002_populate_max_exorcism_level.rb
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
class PopulateMaxExorcismLevel < ActiveRecord::Migration[8.0]
|
||||||
|
def up
|
||||||
|
# Set max_exorcism_level = 5 for all weapons that belong to a series with befoulment augment type
|
||||||
|
updated = Weapon
|
||||||
|
.joins(:weapon_series)
|
||||||
|
.where(weapon_series: { augment_type: :befoulment })
|
||||||
|
.update_all(max_exorcism_level: 5)
|
||||||
|
|
||||||
|
puts " Updated #{updated} weapons with max_exorcism_level = 5"
|
||||||
|
end
|
||||||
|
|
||||||
|
def down
|
||||||
|
Weapon
|
||||||
|
.joins(:weapon_series)
|
||||||
|
.where(weapon_series: { augment_type: :befoulment })
|
||||||
|
.update_all(max_exorcism_level: nil)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
@ -0,0 +1,5 @@
|
||||||
|
class AddMaxExorcismLevelToWeapons < ActiveRecord::Migration[8.0]
|
||||||
|
def change
|
||||||
|
add_column :weapons, :max_exorcism_level, :integer, default: nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
@ -85,5 +85,15 @@ FactoryBot.define do
|
||||||
trait :ax_weapon do
|
trait :ax_weapon do
|
||||||
ax { true }
|
ax { true }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
trait :odiant do
|
||||||
|
weapon_series { WeaponSeries.find_by(slug: 'odiant') || create(:weapon_series, :odiant) }
|
||||||
|
max_exorcism_level { 5 }
|
||||||
|
end
|
||||||
|
|
||||||
|
trait :with_befoulment do
|
||||||
|
weapon_series { WeaponSeries.find_by(slug: 'odiant') || create(:weapon_series, :odiant) }
|
||||||
|
max_exorcism_level { 5 }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
@ -279,4 +279,63 @@ RSpec.describe 'Collection Weapons API', type: :request do
|
||||||
expect(response).to have_http_status(:not_found)
|
expect(response).to have_http_status(:not_found)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe 'default exorcism_level for befoulment weapons' do
|
||||||
|
let(:odiant_series) { create(:weapon_series, :odiant) }
|
||||||
|
let(:befoulment_weapon) { create(:weapon, weapon_series: odiant_series, max_exorcism_level: 5) }
|
||||||
|
let(:regular_weapon) { create(:weapon) }
|
||||||
|
|
||||||
|
it 'sets exorcism_level to 1 when creating with befoulment weapon and no exorcism_level provided' do
|
||||||
|
attributes = {
|
||||||
|
collection_weapon: {
|
||||||
|
weapon_id: befoulment_weapon.id,
|
||||||
|
uncap_level: 3,
|
||||||
|
transcendence_step: 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
post '/api/v1/collection/weapons', params: attributes.to_json, headers: headers
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:created)
|
||||||
|
json = JSON.parse(response.body)
|
||||||
|
expect(json['exorcismLevel']).to eq(1)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'respects provided exorcism_level for befoulment weapon' do
|
||||||
|
befoulment_modifier = create(:weapon_stat_modifier, :befoulment)
|
||||||
|
|
||||||
|
attributes = {
|
||||||
|
collection_weapon: {
|
||||||
|
weapon_id: befoulment_weapon.id,
|
||||||
|
uncap_level: 3,
|
||||||
|
transcendence_step: 0,
|
||||||
|
exorcism_level: 3,
|
||||||
|
befoulment_modifier_id: befoulment_modifier.id,
|
||||||
|
befoulment_strength: 5.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
post '/api/v1/collection/weapons', params: attributes.to_json, headers: headers
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:created)
|
||||||
|
json = JSON.parse(response.body)
|
||||||
|
expect(json['exorcismLevel']).to eq(3)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'does not set exorcism_level for non-befoulment weapons' do
|
||||||
|
attributes = {
|
||||||
|
collection_weapon: {
|
||||||
|
weapon_id: regular_weapon.id,
|
||||||
|
uncap_level: 3,
|
||||||
|
transcendence_step: 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
post '/api/v1/collection/weapons', params: attributes.to_json, headers: headers
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:created)
|
||||||
|
json = JSON.parse(response.body)
|
||||||
|
expect(json['exorcismLevel']).to be_nil
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
@ -345,6 +345,71 @@ RSpec.describe 'GridWeapons API', type: :request do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe 'default exorcism_level for befoulment weapons' do
|
||||||
|
let(:odiant_series) { create(:weapon_series, :odiant) }
|
||||||
|
let(:befoulment_weapon) { create(:weapon, weapon_series: odiant_series, max_exorcism_level: 5) }
|
||||||
|
let(:regular_weapon) { create(:weapon) }
|
||||||
|
|
||||||
|
it 'sets exorcism_level to 1 when creating with befoulment weapon and no exorcism_level provided' do
|
||||||
|
params = {
|
||||||
|
weapon: {
|
||||||
|
party_id: party.id,
|
||||||
|
weapon_id: befoulment_weapon.id,
|
||||||
|
position: 1,
|
||||||
|
uncap_level: 3,
|
||||||
|
transcendence_step: 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
post '/api/v1/grid_weapons', params: params.to_json, headers: headers
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:created)
|
||||||
|
json = JSON.parse(response.body)
|
||||||
|
expect(json['grid_weapon']['exorcismLevel']).to eq(1)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'respects provided exorcism_level for befoulment weapon' do
|
||||||
|
befoulment_modifier = create(:weapon_stat_modifier, :befoulment)
|
||||||
|
|
||||||
|
params = {
|
||||||
|
weapon: {
|
||||||
|
party_id: party.id,
|
||||||
|
weapon_id: befoulment_weapon.id,
|
||||||
|
position: 1,
|
||||||
|
uncap_level: 3,
|
||||||
|
transcendence_step: 0,
|
||||||
|
exorcism_level: 4,
|
||||||
|
befoulment_modifier_id: befoulment_modifier.id,
|
||||||
|
befoulment_strength: 5.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
post '/api/v1/grid_weapons', params: params.to_json, headers: headers
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:created)
|
||||||
|
json = JSON.parse(response.body)
|
||||||
|
expect(json['grid_weapon']['exorcismLevel']).to eq(4)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'does not set exorcism_level for non-befoulment weapons' do
|
||||||
|
params = {
|
||||||
|
weapon: {
|
||||||
|
party_id: party.id,
|
||||||
|
weapon_id: regular_weapon.id,
|
||||||
|
position: 1,
|
||||||
|
uncap_level: 3,
|
||||||
|
transcendence_step: 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
post '/api/v1/grid_weapons', params: params.to_json, headers: headers
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:created)
|
||||||
|
json = JSON.parse(response.body)
|
||||||
|
expect(json['grid_weapon']['exorcismLevel']).to be_nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# Debug hook: if any example fails and a response exists, print the error message.
|
# Debug hook: if any example fails and a response exists, print the error message.
|
||||||
after(:each) do |example|
|
after(:each) do |example|
|
||||||
if example.exception && defined?(response) && response.present?
|
if example.exception && defined?(response) && response.present?
|
||||||
|
|
|
||||||
115
spec/requests/weapons_controller_spec.rb
Normal file
115
spec/requests/weapons_controller_spec.rb
Normal file
|
|
@ -0,0 +1,115 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
RSpec.describe 'Weapons API', type: :request do
|
||||||
|
let(:editor_user) { create(:user, role: 7) }
|
||||||
|
let(:regular_user) { create(:user, role: 3) }
|
||||||
|
|
||||||
|
let(:editor_token) do
|
||||||
|
Doorkeeper::AccessToken.create!(resource_owner_id: editor_user.id, expires_in: 30.days, scopes: 'public')
|
||||||
|
end
|
||||||
|
let(:regular_token) do
|
||||||
|
Doorkeeper::AccessToken.create!(resource_owner_id: regular_user.id, expires_in: 30.days, scopes: 'public')
|
||||||
|
end
|
||||||
|
let(:editor_headers) do
|
||||||
|
{ 'Authorization' => "Bearer #{editor_token.token}", 'Content-Type' => 'application/json' }
|
||||||
|
end
|
||||||
|
let(:regular_headers) do
|
||||||
|
{ 'Authorization' => "Bearer #{regular_token.token}", 'Content-Type' => 'application/json' }
|
||||||
|
end
|
||||||
|
|
||||||
|
let!(:weapon_series) { create(:weapon_series, :odiant) }
|
||||||
|
let!(:weapon) { create(:weapon, weapon_series: weapon_series, max_exorcism_level: 5) }
|
||||||
|
|
||||||
|
describe 'GET /api/v1/weapons/:id' do
|
||||||
|
it 'returns the weapon with max_exorcism_level' do
|
||||||
|
get "/api/v1/weapons/#{weapon.id}"
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:ok)
|
||||||
|
json = JSON.parse(response.body)
|
||||||
|
expect(json['maxExorcismLevel']).to eq(5)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns null for max_exorcism_level when not set' do
|
||||||
|
weapon_without_exorcism = create(:weapon, max_exorcism_level: nil)
|
||||||
|
|
||||||
|
get "/api/v1/weapons/#{weapon_without_exorcism.id}"
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:ok)
|
||||||
|
json = JSON.parse(response.body)
|
||||||
|
expect(json['maxExorcismLevel']).to be_nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'POST /api/v1/weapons' do
|
||||||
|
let(:valid_params) do
|
||||||
|
{
|
||||||
|
weapon: {
|
||||||
|
granblue_id: '1040000001',
|
||||||
|
name_en: 'Test Weapon',
|
||||||
|
rarity: 4,
|
||||||
|
element: 1,
|
||||||
|
proficiency: 1,
|
||||||
|
max_exorcism_level: 5
|
||||||
|
}
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'creates a weapon with max_exorcism_level when editor' do
|
||||||
|
post '/api/v1/weapons', params: valid_params.to_json, headers: editor_headers
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:created)
|
||||||
|
json = JSON.parse(response.body)
|
||||||
|
expect(json['maxExorcismLevel']).to eq(5)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'creates a weapon with null max_exorcism_level' do
|
||||||
|
params = valid_params.deep_dup
|
||||||
|
params[:weapon][:max_exorcism_level] = nil
|
||||||
|
params[:weapon][:granblue_id] = '1040000002'
|
||||||
|
|
||||||
|
post '/api/v1/weapons', params: params.to_json, headers: editor_headers
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:created)
|
||||||
|
json = JSON.parse(response.body)
|
||||||
|
expect(json['maxExorcismLevel']).to be_nil
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'rejects creation from non-editor' do
|
||||||
|
post '/api/v1/weapons', params: valid_params.to_json, headers: regular_headers
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:unauthorized)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'PATCH /api/v1/weapons/:id' do
|
||||||
|
it 'updates max_exorcism_level' do
|
||||||
|
patch "/api/v1/weapons/#{weapon.id}",
|
||||||
|
params: { weapon: { max_exorcism_level: 3 } }.to_json,
|
||||||
|
headers: editor_headers
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:ok)
|
||||||
|
json = JSON.parse(response.body)
|
||||||
|
expect(json['maxExorcismLevel']).to eq(3)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'clears max_exorcism_level when set to null' do
|
||||||
|
patch "/api/v1/weapons/#{weapon.id}",
|
||||||
|
params: { weapon: { max_exorcism_level: nil } }.to_json,
|
||||||
|
headers: editor_headers
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:ok)
|
||||||
|
json = JSON.parse(response.body)
|
||||||
|
expect(json['maxExorcismLevel']).to be_nil
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'rejects update from non-editor' do
|
||||||
|
patch "/api/v1/weapons/#{weapon.id}",
|
||||||
|
params: { weapon: { max_exorcism_level: 3 } }.to_json,
|
||||||
|
headers: regular_headers
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:unauthorized)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
Loading…
Reference in a new issue