From cd9978023a9b4b54ce40926e20573b59037c89dd Mon Sep 17 00:00:00 2001 From: Cory Streiff Date: Mon, 16 Dec 2024 15:15:20 +0100 Subject: [PATCH] Refactor out for_families attribute from FamilyRequestCreateService --- .../partners/family_requests_controller.rb | 4 ++-- .../partners/individuals_requests_controller.rb | 6 ++++-- .../partners/family_request_create_service.rb | 10 +++------- .../partners/family_request_create_service_spec.rb | 12 ++++++------ 4 files changed, 15 insertions(+), 17 deletions(-) diff --git a/app/controllers/partners/family_requests_controller.rb b/app/controllers/partners/family_requests_controller.rb index 6f4f94b34c..f1ba5f0f3c 100644 --- a/app/controllers/partners/family_requests_controller.rb +++ b/app/controllers/partners/family_requests_controller.rb @@ -19,7 +19,7 @@ def create create_service = Partners::FamilyRequestCreateService.new( partner_user_id: current_user.id, family_requests_attributes: family_requests_attributes, - for_families: true + request_type: "child" ) create_service.call @@ -37,7 +37,7 @@ def validate @partner_request = Partners::FamilyRequestCreateService.new( partner_user_id: current_user.id, family_requests_attributes: family_requests_attributes, - for_families: true + request_type: "child" ).initialize_only if @partner_request.valid? @total_items = @partner_request.total_items diff --git a/app/controllers/partners/individuals_requests_controller.rb b/app/controllers/partners/individuals_requests_controller.rb index 9432bdc16f..bd85254e04 100644 --- a/app/controllers/partners/individuals_requests_controller.rb +++ b/app/controllers/partners/individuals_requests_controller.rb @@ -12,7 +12,8 @@ def create create_service = Partners::FamilyRequestCreateService.new( partner_user_id: current_user.id, comments: individuals_request_params[:comments], - family_requests_attributes: individuals_request_params[:items_attributes]&.values + family_requests_attributes: individuals_request_params[:items_attributes]&.values, + request_type: "individual" ) create_service.call @@ -36,7 +37,8 @@ def validate @partner_request = Partners::FamilyRequestCreateService.new( partner_user_id: current_user.id, comments: individuals_request_params[:comments], - family_requests_attributes: individuals_request_params[:items_attributes]&.values + family_requests_attributes: individuals_request_params[:items_attributes]&.values, + request_type: "individual" ).initialize_only if @partner_request.valid? @total_items = @partner_request.total_items diff --git a/app/services/partners/family_request_create_service.rb b/app/services/partners/family_request_create_service.rb index 8e018530db..c84da76a8a 100644 --- a/app/services/partners/family_request_create_service.rb +++ b/app/services/partners/family_request_create_service.rb @@ -6,13 +6,13 @@ module Partners class FamilyRequestCreateService include ServiceObjectErrorsMixin - attr_reader :partner_user_id, :comments, :family_requests_attributes, :partner_request + attr_reader :partner_user_id, :comments, :family_requests_attributes, :partner_request, :request_type - def initialize(partner_user_id:, family_requests_attributes:, comments: nil, for_families: false) + def initialize(partner_user_id:, family_requests_attributes:, request_type:, comments: nil) @partner_user_id = partner_user_id @comments = comments @family_requests_attributes = family_requests_attributes.presence || [] - @for_families = for_families + @request_type = request_type end def call @@ -81,9 +81,5 @@ def convert_person_count_to_item_quantity(item_id:, person_count:) def included_items_by_id @included_items_by_id ||= Item.where(id: family_requests_attributes.pluck(:item_id)).index_by(&:id) end - - def request_type - @for_families ? "child" : "individual" - end end end diff --git a/spec/services/partners/family_request_create_service_spec.rb b/spec/services/partners/family_request_create_service_spec.rb index 1cbe16d5bf..1ae4348f18 100644 --- a/spec/services/partners/family_request_create_service_spec.rb +++ b/spec/services/partners/family_request_create_service_spec.rb @@ -5,7 +5,7 @@ { partner_user_id: partner_user.id, comments: comments, - for_families: for_families, + request_type: request_type, family_requests_attributes: family_requests_attributes } end @@ -13,7 +13,7 @@ let(:partner) { create(:partner, organization: organization) } let(:partner_user) { partner.primary_user } let(:comments) { Faker::Lorem.paragraph } - let(:for_families) { false } + let(:request_type) { "individual" } context 'when the arguments are incorrect' do context 'because no family_requests_attributes or comments were defined' do @@ -97,8 +97,8 @@ expect(second_item_request.quantity.to_i).to eq(second_item_request.item.default_quantity * 4) end - context "with for_families false" do - let(:for_families) { false } + context "with request_type as individual" do + let(:request_type) { "individual" } it "creates a request of type individual" do expect { subject }.to change { Request.count }.by(1) @@ -108,8 +108,8 @@ end end - context "with for_families true" do - let(:for_families) { true } + context "with request_type as child" do + let(:request_type) { "child" } it "creates a request of type child" do expect { subject }.to change { Request.count }.by(1)