From 488c34388d82c191129b10a65b9ff09310dca08b Mon Sep 17 00:00:00 2001 From: Justin Edmund Date: Thu, 20 Apr 2023 02:36:11 -0700 Subject: [PATCH] Update party-related files for 1-index guidebooks --- app/blueprints/api/v1/party_blueprint.rb | 4 ++-- app/controllers/api/v1/parties_controller.rb | 10 ++++---- app/models/party.rb | 24 ++++++++++++++++---- 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/app/blueprints/api/v1/party_blueprint.rb b/app/blueprints/api/v1/party_blueprint.rb index 556188d..fe6674d 100644 --- a/app/blueprints/api/v1/party_blueprint.rb +++ b/app/blueprints/api/v1/party_blueprint.rb @@ -42,9 +42,9 @@ module Api field :guidebooks do |p| { - '0' => !p.guidebook0.nil? ? GuidebookBlueprint.render_as_hash(p.guidebook0) : nil, '1' => !p.guidebook1.nil? ? GuidebookBlueprint.render_as_hash(p.guidebook1) : nil, - '2' => !p.guidebook2.nil? ? GuidebookBlueprint.render_as_hash(p.guidebook2) : nil + '2' => !p.guidebook2.nil? ? GuidebookBlueprint.render_as_hash(p.guidebook2) : nil, + '3' => !p.guidebook3.nil? ? GuidebookBlueprint.render_as_hash(p.guidebook3) : nil } end diff --git a/app/controllers/api/v1/parties_controller.rb b/app/controllers/api/v1/parties_controller.rb index 44f8e6b..8bbd66a 100644 --- a/app/controllers/api/v1/parties_controller.rb +++ b/app/controllers/api/v1/parties_controller.rb @@ -55,8 +55,8 @@ module Api # TODO: Validate accessory with job - return render json: PartyBlueprint.render(@party, view: :full, root: :party) if @party.save! - + return render json: PartyBlueprint.render(@party, view: :full, root: :party) if @party.save + render_validation_error_response(@party) end @@ -241,8 +241,6 @@ module Api end def party_params - ap "Params are..." - ap params return unless params[:party].present? params.require(:party).permit( @@ -266,9 +264,9 @@ module Api :button_count, :turn_count, :chain_count, - :guidebook0_id, :guidebook1_id, - :guidebook2_id + :guidebook2_id, + :guidebook3_id ) end end diff --git a/app/models/party.rb b/app/models/party.rb index 3bbc2e0..5729698 100644 --- a/app/models/party.rb +++ b/app/models/party.rb @@ -41,11 +41,6 @@ class Party < ApplicationRecord class_name: 'JobSkill', optional: true - belongs_to :guidebook0, - foreign_key: 'guidebook0_id', - class_name: 'Guidebook', - optional: true - belongs_to :guidebook1, foreign_key: 'guidebook1_id', class_name: 'Guidebook', @@ -56,6 +51,11 @@ class Party < ApplicationRecord class_name: 'Guidebook', optional: true + belongs_to :guidebook3, + foreign_key: 'guidebook3_id', + class_name: 'Guidebook', + optional: true + has_many :characters, foreign_key: 'party_id', class_name: 'GridCharacter', @@ -95,6 +95,7 @@ class Party < ApplicationRecord ##### ActiveRecord Validations validate :skills_are_unique + validate :guidebooks_are_unique attr_accessor :favorited @@ -145,4 +146,17 @@ class Party < ApplicationRecord errors.add(:job_skills, 'must be unique') end + + def guidebooks_are_unique + guidebooks = [guidebook1, guidebook2, guidebook3].compact + return if guidebooks.uniq.length == guidebooks.length + + guidebooks.each_with_index do |book, index| + next if index.zero? + + errors.add(:"guidebook#{index + 1}", 'must be unique') if guidebooks[0...index].include?(book) + end + + errors.add(:guidebooks, 'must be unique') + end end