Skip to content

Commit

Permalink
Refactor out for_families attribute from FamilyRequestCreateService (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
coalest authored Dec 18, 2024
1 parent 8f8d685 commit 75af9ad
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 17 deletions.
4 changes: 2 additions & 2 deletions app/controllers/partners/family_requests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 4 additions & 2 deletions app/controllers/partners/individuals_requests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
10 changes: 3 additions & 7 deletions app/services/partners/family_request_create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
12 changes: 6 additions & 6 deletions spec/services/partners/family_request_create_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
{
partner_user_id: partner_user.id,
comments: comments,
for_families: for_families,
request_type: request_type,
family_requests_attributes: family_requests_attributes
}
end
let(:organization) { create(:organization) }
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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 75af9ad

Please sign in to comment.