Skip to content

Commit

Permalink
Finished adding Scope and Default Filtering
Browse files Browse the repository at this point in the history
* Added scope to Families with unit tests
* Added default filter to families controller
* Updated the CSV output on the families controller
* Added `Include Archived?` option on the families view
* Added request test to ensure default only returns the non-archived
* Added test for `Include Archived?` check on index
  • Loading branch information
grantmca committed May 26, 2023
1 parent cce2de1 commit 3f42ae2
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 18 deletions.
3 changes: 2 additions & 1 deletion app/controllers/partners/families_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ def index
@filterrific = initialize_filterrific(
current_partner.families
.order(sort_order),
params[:filterrific]
params[:filterrific],
default_filter_params: {"include_archived"=>0},
) || return

@families = @filterrific.find
Expand Down
8 changes: 4 additions & 4 deletions app/models/partners/family.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class Family < Base

scope :search_guardian_names, ->(query) { where('guardian_first_name ilike ? OR guardian_last_name ilike ?', "%#{query}%", "%#{query}%") }
scope :search_agency_guardians, ->(query) { where('case_manager ilike ?', "%#{query}%") }
scope :include_archived, ->(bool) { bool == 1 ? all : where(archived: false) }
scope :include_archived, ->(query) { query.zero? ? where(archived: false) : all }


INCOME_TYPES = ['SSI', 'SNAP/FOOD Stamps', 'TANF', 'WIC', 'Housing/subsidized', 'Housing/unsubsidized', 'N/A'].freeze
Expand Down Expand Up @@ -80,14 +80,13 @@ def self.csv_export_headers
id guardian_first_name guardian_last_name guardian_zip_code guardian_county
guardian_phone case_manager home_adult_count home_child_count home_young_child_count
sources_of_income guardian_employed guardian_employment_type guardian_monthly_pay
guardian_health_insurance comments created_at updated_at partner_id military
guardian_health_insurance comments created_at updated_at partner_id military archived
].freeze
end

def csv_export_attributes
[
id,
archived,
guardian_first_name,
guardian_last_name,
guardian_zip_code,
Expand All @@ -106,7 +105,8 @@ def csv_export_attributes
created_at,
updated_at,
partner_id,
military
military,
archived,
]
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/views/partners/children/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
class: 'filterrific-periodically-observed form-control'
) %>
</div>
<div class="col-3">
<div class="col-2" align="center">
<%= f.label :search_comments, "Show Active Only?" %>
<%= f.check_box(
:search_active,
Expand Down
2 changes: 1 addition & 1 deletion app/views/partners/families/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
<%= f.label :include_archived, "Include Archived?" %>
<%= f.check_box(
:include_archived,
class: 'filterrific-periodically-observed form-control'
class: 'filterrific-periodically-observed form-control',
) %>
</div>
<div class="col-3">
Expand Down
25 changes: 20 additions & 5 deletions spec/models/partners/family_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,32 @@
end

describe "#archived_children" do
subject do
subject do
partners_family = FactoryBot.build(:partners_family)
child1 = FactoryBot.create(:partners_child, family: partners_family)
child2 = FactoryBot.create(:partners_child, family: partners_family)
FactoryBot.create(:partners_child, family: partners_family)
FactoryBot.create(:partners_child, family: partners_family)
partners_family
end
end
it "should archive children when family is archived" do
expect(subject.children.pluck(:archived).any?).to be false
subject.update(archived: true)
expect(subject.archived).to be true
expect(subject.children.pluck(:archived).all?).to be true
expect(subject.children.pluck(:archived).all?).to be true
end
end

describe "#search_non_archived" do
let!(:partners_family_1) { FactoryBot.create(:partners_family) }
let!(:partners_family_2) { FactoryBot.create(:partners_family) }
let!(:partners_family_3) { FactoryBot.create(:partners_family, archived: true) }
let!(:partners_family_4) { FactoryBot.create(:partners_family, archived: true) }

it "should return all families if one is passed in" do
expect(Partners::Family.include_archived(1)).to contain_exactly(partners_family_1, partners_family_2, partners_family_3, partners_family_4)
end

it "should return non-archived families if zero is passed in" do
expect(Partners::Family.include_archived(0)).to contain_exactly(partners_family_1, partners_family_2)
end
end
end
22 changes: 16 additions & 6 deletions spec/requests/partners/family_requests_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,36 @@
guardian_health_insurance: "Medicaid",
comments: "Some comment 2",
military: true,
partner: partner)
partner: partner,
archived: true,)
end

describe "GET #index" do
before do
sign_in(partner_user)
end

it "should render without any issues" do
it "should render without any issues and display unarchived families by default" do
get partners_families_path
expect(response).to render_template(:index)
expect(assigns[:families].count).to eq(1)
expect(assigns[:families].pluck(:archived)).to all(be(false))
end

it "should render without any issues and present all families" do
get partners_families_path, params: {"filterrific"=>{"include_archived"=>"1"}}
expect(response).to render_template(:index)
expect(assigns[:families].count).to eq(2)
end

it "should export CSV" do
headers = {"Accept" => "text/csv", "Content-Type" => "text/csv"}
get partners_families_path, headers: headers
params = {"filterrific"=>{"search_guardian_names"=>"", "search_agency_guardians"=>"", "include_archived"=>"1"}}
get partners_families_path, headers: headers, params: params
csv = <<~CSV
id,guardian_first_name,guardian_last_name,guardian_zip_code,guardian_county,guardian_phone,case_manager,home_adult_count,home_child_count,home_young_child_count,sources_of_income,guardian_employed,guardian_employment_type,guardian_monthly_pay,guardian_health_insurance,comments,created_at,updated_at,partner_id,military
#{family1.id},John,Smith,90210,Franklin,416-555-2345,Jane Smith,2,3,1,"SSI,TANF",true,Part-time,4.0,Medicaid,Some comment,#{family1.created_at},#{family1.updated_at},#{partner.id},false
#{family2.id},Mark,Smith,90210,Jefferson,416-555-0987,Jane Smith,1,2,2,TANF,false,Part-time,4.0,Medicaid,Some comment 2,#{family2.created_at},#{family2.updated_at},#{partner.id},true
id,guardian_first_name,guardian_last_name,guardian_zip_code,guardian_county,guardian_phone,case_manager,home_adult_count,home_child_count,home_young_child_count,sources_of_income,guardian_employed,guardian_employment_type,guardian_monthly_pay,guardian_health_insurance,comments,created_at,updated_at,partner_id,military,archived
#{family1.id},John,Smith,90210,Franklin,416-555-2345,Jane Smith,2,3,1,"SSI,TANF",true,Part-time,4.0,Medicaid,Some comment,#{family1.created_at},#{family1.updated_at},#{partner.id},false,false
#{family2.id},Mark,Smith,90210,Jefferson,416-555-0987,Jane Smith,1,2,2,TANF,false,Part-time,4.0,Medicaid,Some comment 2,#{family2.created_at},#{family2.updated_at},#{partner.id},true,true
CSV
expect(response.body).to eq(csv)
end
Expand Down

0 comments on commit 3f42ae2

Please sign in to comment.