Skip to content

Commit 7893041

Browse files
author
Olexii Kasianenko
committed
Implement mandatory fields validation and enhance agency information form
1 parent 389dfe9 commit 7893041

8 files changed

Lines changed: 478 additions & 46 deletions

File tree

app/models/partner.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class Partner < ApplicationRecord
4848

4949
has_many_attached :documents
5050

51-
validates :name, presence: true, uniqueness: { scope: :organization }
51+
# validates :name, presence: true, uniqueness: { scope: :organization }
5252

5353
validates :email, presence: true, uniqueness: { case_sensitive: false }, format: { with: URI::MailTo::EMAIL_REGEXP }
5454

app/models/partners/profile.rb

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,6 @@ class Profile < Base
135135
twoycollege: "2YCOLLEGE",
136136
wic: "WIC"
137137

138-
validate :check_social_media, on: :edit
139-
140138
validate :client_share_is_0_or_100
141139
validate :has_at_least_one_request_setting
142140
validate :pick_up_email_addresses
@@ -193,17 +191,42 @@ def self.agency_types_for_selection
193191
agency_types.keys.map(&:to_sym).sort_by { |sym| I18n.t(sym, scope: :partners_profile) }.partition { |v| v != :other }.flatten
194192
end
195193

196-
private
197-
198194
def check_social_media
195+
return if organization.one_step_partner_invite
199196
return if website.present? || twitter.present? || facebook.present? || instagram.present?
200197
return if partner.partials_to_show.exclude?("media_information")
201198

202199
unless no_social_media_presence
203-
errors.add(:no_social_media_presence, "must be checked if you have not provided any of Website, Twitter, Facebook, or Instagram.")
200+
errors.add(:no_social_media_presence, "No social media presence must be checked if you have not provided any of Website, Twitter, Facebook, or Instagram.")
201+
end
202+
errors
203+
end
204+
205+
def check_mandatory_fields
206+
return if organization.one_step_partner_invite
207+
208+
mandatory_fields = [
209+
:agency_type,
210+
:address1,
211+
:city,
212+
:state,
213+
:zip_code,
214+
:program_name,
215+
:program_description
216+
]
217+
messages = []
218+
messages << "Name can't be blank" if partner.name.blank?
219+
mandatory_fields.each do |field|
220+
if send(field).blank?
221+
messages << "#{field.to_s.humanize.capitalize} can't be blank"
222+
end
204223
end
224+
errors.add(:base, messages.join(", ")) if messages.any?
225+
errors
205226
end
206227

228+
private
229+
207230
def client_share_is_0_or_100
208231
# business logic: the client share has to be 0 or 100 -- although it is an estimate only, making it 0 (not
209232
# specified at all) or 100 means we won't have people overallocating (> 100) and that they think about what

app/services/partner_profile_update_service.rb

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,13 @@ def initialize(old_partner, new_partner_params, new_profile_params)
1010
end
1111

1212
def call
13-
@return_value = false
13+
return self unless validation
14+
1415
perform_profile_service do
1516
@partner.update(@partner_params)
16-
@return_value = @partner.valid?
17-
18-
if @return_value
19-
@profile.served_areas.destroy_all
20-
@profile.attributes = @profile_params
21-
@profile.save!(context: :edit)
22-
else
23-
@error = "Partner '#{@partner.name}' had error(s) preventing the profile from being updated: #{@partner.errors.full_messages.join(", ")}"
24-
end
17+
@profile.served_areas.destroy_all
18+
@profile.attributes = @profile_params
19+
@profile.save!(context: :edit)
2520
end
2621
end
2722

@@ -48,4 +43,31 @@ def success?
4843
def set_error(error)
4944
@error = error.to_s
5045
end
46+
47+
private
48+
49+
def validation
50+
return true unless %w[awaiting_review approved].include?(@partner.status)
51+
return true if @partner.organization.one_step_partner_invite
52+
53+
check_social_media
54+
check_mandatory_fields
55+
@error.nil?
56+
end
57+
58+
def check_mandatory_fields
59+
mandatory_fields = %i[agency_type address1 city state zip_code program_name program_description]
60+
missing_fields = mandatory_fields.select { |field| @profile_params[field].blank? }
61+
missing_fields.prepend :agency_name if @partner_params[:name].blank?
62+
if missing_fields.any?
63+
@error = "Missing mandatory fields: #{missing_fields.join(', ')}"
64+
end
65+
end
66+
67+
def check_social_media
68+
social_media_fields = %i[website facebook twitter instagram]
69+
if social_media_fields.all? { |field| @profile_params[field].blank? } && @profile_params[:no_social_media_presence] == '0'
70+
@error = "At least one social media field must be filled out or 'No social media presence' must be checked."
71+
end
72+
end
5173
end

app/services/partners/request_approval_service.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ def valid?
3333
errors.add :base, partner.profile.errors.full_messages.join('. ')
3434
end
3535

36+
errors.merge! partner.profile.errors if partner.profile.check_social_media
37+
errors.merge! partner.profile.errors if partner.profile.check_mandatory_fields
38+
3639
errors.none?
3740
end
3841
end

app/views/partners/profiles/edit/_agency_information.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<div class="container-fluid">
33
<div class="row">
44
<div class="col-md-12">
5-
<div class="card">
5+
<div id="agency_information" class="card">
66
<div class="card-header bg-white">
77
<h3 class="card-title"><strong>Agency Information</strong></h3>
88
</div>

spec/system/partner_system_spec.rb

Lines changed: 162 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393

9494
click_on 'New Partner Agency'
9595

96-
fill_in 'Name *', with: partner_attributes[:name]
96+
fill_in 'Name', with: partner_attributes[:name]
9797
fill_in 'E-mail *', with: partner_attributes[:email]
9898
fill_in 'Quota', with: partner_attributes[:quota]
9999
fill_in 'Notes', with: partner_attributes[:notes]
@@ -125,7 +125,7 @@
125125
assert page.has_content? "Partner Agencies for #{organization.name}"
126126
click_on 'New Partner Agency'
127127

128-
fill_in 'Name *', with: partner_attributes[:name]
128+
fill_in 'Name', with: partner_attributes[:name]
129129

130130
find('button', text: 'Add Partner Agency').click
131131
end
@@ -384,14 +384,6 @@
384384
expect(partner.name).to eq(name)
385385
end
386386

387-
it "prevents a user from updating a partner with empty name" do
388-
visit subject
389-
fill_in "Name", with: ""
390-
click_button "Update Partner"
391-
392-
expect(page.find(".alert")).to have_content "Something didn't work quite right -- try again?"
393-
end
394-
395387
it "User can uncheck send_reminders" do
396388
visit subject
397389
uncheck 'send_reminders'
@@ -458,12 +450,11 @@
458450

459451
describe "#edit_profile" do
460452
let!(:partner) { create(:partner, name: "Frank") }
461-
subject { edit_profile_path(partner.id) }
462453

463454
context "when step-wise editing is enabled" do
464455
before do
465456
Flipper.enable(:partner_step_form)
466-
visit subject
457+
visit edit_profile_path(partner.id)
467458
end
468459

469460
it "displays all sections in a closed state by default" do
@@ -554,6 +545,165 @@
554545
expect(page).to have_css("#pick_up_person.accordion-collapse.collapse.show", visible: true)
555546
expect(page).to have_css("#partner_settings.accordion-collapse.collapse.show", visible: true)
556547
end
548+
549+
context "Mandatory fields validation" do
550+
subject { all("input[type='submit'][value='Save Progress']").last.click }
551+
552+
before do
553+
find("button[data-bs-target='#agency_information']").click
554+
within "#agency_information" do
555+
fill_in "Agency Name", with: "Agency1"
556+
select "Basic Needs Bank", from: "Agency Type"
557+
fill_in "Address (line 1)", with: "123 Main St"
558+
fill_in "City", with: "Metropolis"
559+
fill_in "State", with: "CA"
560+
fill_in "Zip Code", with: "90210"
561+
end
562+
find("button[data-bs-target='#agency_stability']").click
563+
within "#agency_stability" do
564+
fill_in "Program Name(s)", with: "Program 1"
565+
fill_in "Program Description(s)", with: "Really great program"
566+
end
567+
end
568+
569+
context "No social media filled" do
570+
before do
571+
find("button[data-bs-target='#media_information']").click
572+
within "#media_information" do
573+
fill_in "Website", with: ""
574+
uncheck "No Social Media Presence"
575+
end
576+
end
577+
578+
context "partner status is invited" do
579+
before do
580+
partner.invited!
581+
end
582+
583+
it "displays success message" do
584+
expect {
585+
subject
586+
}.to change { partner.reload.updated_at }
587+
588+
expect(page).to have_content("Details were successfully updated.")
589+
end
590+
end
591+
592+
context "partner status is awaiting_review" do
593+
before do
594+
partner.awaiting_review!
595+
end
596+
597+
it "displays validation errors" do
598+
expect {
599+
subject
600+
}.not_to change { partner.reload.updated_at }
601+
expect(page).to have_content("At least one social media field must be filled out or 'No social media presence' must be checked.")
602+
end
603+
end
604+
605+
context "partner status is approved" do
606+
before do
607+
partner.approved!
608+
end
609+
610+
it "displays validation errors" do
611+
expect {
612+
subject
613+
}.not_to change { partner.reload.updated_at }
614+
615+
expect(page).to have_content("At least one social media field must be filled out or 'No social media presence' must be checked.")
616+
end
617+
618+
context "partner's organization one_step_partner_invite is true" do
619+
before do
620+
partner.organization.update!(one_step_partner_invite: true)
621+
end
622+
623+
it "displays success message" do
624+
expect {
625+
subject
626+
}.to change { partner.reload.updated_at }
627+
628+
expect(page).to have_content("Details were successfully updated.")
629+
end
630+
end
631+
end
632+
end
633+
634+
context "Mandatory fields empty" do
635+
before do
636+
# find("button[data-bs-target='#agency_information']").click
637+
within "#agency_information" do
638+
fill_in "Agency Name", with: ""
639+
select "", from: "Agency Type"
640+
fill_in "Address (line 1)", with: ""
641+
fill_in "City", with: ""
642+
fill_in "State", with: ""
643+
fill_in "Zip Code", with: ""
644+
end
645+
# find("button[data-bs-target='#agency_stability']").click
646+
within "#agency_stability" do
647+
fill_in "Program Name(s)", with: ""
648+
fill_in "Program Description(s)", with: ""
649+
end
650+
end
651+
context "partner status is invited" do
652+
before do
653+
partner.invited!
654+
end
655+
656+
it "displays success message" do
657+
expect {
658+
subject
659+
}.to change { partner.reload.updated_at }
660+
661+
expect(page).to have_content("Details were successfully updated.")
662+
end
663+
end
664+
665+
context "partner status is awaiting_review" do
666+
before do
667+
partner.awaiting_review!
668+
end
669+
670+
it "displays validation errors" do
671+
expect {
672+
subject
673+
}.not_to change { partner.reload.updated_at }
674+
expect(page).to have_content("Missing mandatory fields: agency_name, agency_type, address1, city, state, zip_code, program_name, program_description")
675+
end
676+
end
677+
678+
context "partner status is approved" do
679+
before do
680+
partner.approved!
681+
end
682+
683+
it "displays validation errors" do
684+
expect {
685+
subject
686+
}.not_to change { partner.reload.updated_at }
687+
688+
expect(page).to have_content("Missing mandatory fields: agency_name, agency_type, address1, city, state, zip_code, program_name, program_description")
689+
end
690+
691+
context "partner's organization one_step_partner_invite is true" do
692+
before do
693+
partner.organization.update!(one_step_partner_invite: true)
694+
end
695+
696+
it "displays success message" do
697+
expect {
698+
subject
699+
}.to change { partner.reload.updated_at }
700+
701+
expect(page).to have_content("Details were successfully updated.")
702+
end
703+
end
704+
end
705+
end
706+
end
557707
end
558708
end
559709

0 commit comments

Comments
 (0)