Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/controllers/partners/children_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ def child_params
:first_name,
:gender,
:health_insurance,
:item_needed_diaperid,
:last_name,
:race,
:archived,
child_lives_with: []
child_lives_with: [],
requested_item_ids: []
)
end

Expand Down
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 @@ -23,9 +23,9 @@ def create
end
end

children = current_partner.children.active.where(id: children_ids).where.not(item_needed_diaperid: [nil, 0])
children = current_partner.children.active.where(id: children_ids).joins(:requested_items).select('children.*', :item_id)

children_grouped_by_item_id = children.group_by(&:item_needed_diaperid)
children_grouped_by_item_id = children.group_by(&:item_id)
family_requests_attributes = children_grouped_by_item_id.map do |item_id, item_requested_children|
{ item_id: item_id, person_count: item_requested_children.size, children: item_requested_children }
end
Expand Down
7 changes: 7 additions & 0 deletions app/helpers/partners_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Encapsulates methods that need some business logic
module PartnersHelper
def display_requested_items(partner, child)
ids = child.requested_item_ids
ids.map do |item_id|
partner.organization.item_id_to_display_string_map[item_id]
end.join(', ')
end

def show_header_column_class(partner, additional_classes: "")
if partner.quota.present?
"col-sm-3 col-3 #{additional_classes}"
Expand Down
5 changes: 3 additions & 2 deletions app/models/partners/child.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Child < Base
serialize :child_lives_with, Array
belongs_to :family
has_many :child_item_requests, dependent: :destroy
has_and_belongs_to_many :requested_items, class_name: 'Item'

include Filterable
include Exportable
Expand Down Expand Up @@ -88,7 +89,7 @@ def display_name
def self.csv_export_headers
%w[
id first_name last_name date_of_birth gender child_lives_with race agency_child_id
health_insurance comments created_at updated_at family_id item_needed_diaperid active archived
health_insurance comments created_at updated_at family_id requested_item_ids active archived
].freeze
end

Expand All @@ -107,7 +108,7 @@ def csv_export_attributes
created_at,
updated_at,
family_id,
item_needed_diaperid,
requested_item_ids,
active,
archived
]
Expand Down
2 changes: 1 addition & 1 deletion app/views/partners/children/_child.json.jbuilder
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
json.extract! child, :id, :first_name, :last_name, :date_of_birth, :gender, :child_lives_with, :race, :agency_child_id, :health_insurance, :item_needed_diaperid, :comments, :created_at, :updated_at
json.extract! child, :id, :first_name, :last_name, :date_of_birth, :gender, :child_lives_with, :race, :agency_child_id, :health_insurance, :requested_item_ids, :comments, :created_at, :updated_at
json.url child_url(child, format: :json)
32 changes: 21 additions & 11 deletions app/views/partners/children/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,22 @@
<%= form.label :last_name, "Last Name" %>
<%= form.text_field :last_name, class: "form-control" %>

<%= form.label :item_needed, "Diaper/Item Used" %>
<%= form.select :item_needed_diaperid, @requestable_items,
{include_blank: 'Select an item'},
{class: 'form-control'} %>
<%= @requestable_items.map(&:last).intersect?(child.requested_item_ids) %>
<div class="requestable-items-container">
<%= form.label :item_needed, "Item(s) Requested" %>
<%= form.select(
:requested_item_ids,
@requestable_items,
{ include_hidden: true },
{
multiple: true,
class: "form-control custom-select",
"data-controller": "select2",
"data-select2-config-value": "{}",
},
) %>
<br>
</div>

<%= form.input :date_of_birth, as: :date, start_year: 20.years.ago.year, end_year: Time.current.year, label: "Date of Birth" %>
<%= form.input :gender, collection: ["Male", "Female"], class: "form-control" %>
Expand Down Expand Up @@ -53,16 +64,15 @@
<%= form.label "Archived?" %>&nbsp;
<%= form.check_box :archived %>

</div>
<div class="card-footer">
<%= form.submit(class: 'btn btn-primary') %>
</div>

<% end %>
</div>
<!-- /.card -->
</div>
</div>
</div>
<!-- /.row -->
</div><!-- /.container-fluid -->
</div>
<!-- /.card -->
</div>
<!-- /.row -->
</div><!-- /.container-fluid -->
</section>
4 changes: 2 additions & 2 deletions app/views/partners/children/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@
<dt>Health insurance:</dt>
<dd><%= @child.health_insurance %></dd>

<dt>Item needed:</dt>
<dd><%= current_partner.organization.item_id_to_display_string_map[@child.item_needed_diaperid] %></dd>
<dt>Item(s) needed:</dt>
<dd><%= display_requested_items(current_partner, @child) %></dd>

<dt>Comments:</dt>
<dd><%= @child.comments %></dd>
Expand Down
6 changes: 3 additions & 3 deletions app/views/partners/family_requests/_list.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
<%= child.first_name %> <%= child.last_name %>
</td>
<td>
<% if child.item_needed_diaperid %>
<%= current_partner.organization.item_id_to_display_string_map[child.item_needed_diaperid] %>
<% if child.requested_item_ids.any? %>
<%= display_requested_items(current_partner, child) %>
<% else %>
<%= "N/A" %>
<% end %>
</td>
<td>
<div class="custom-control custom-switch form-switch">
<% if child.item_needed_diaperid %>
<% if child.requested_item_ids.any? %>
<%= check_box_tag "child-#{child.id}", child.active, child.active,
class: "custom-control-input",
id: "child-#{child.id}" %>
Expand Down
8 changes: 8 additions & 0 deletions db/migrate/20240703174254_create_join_table_children_items.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class CreateJoinTableChildrenItems < ActiveRecord::Migration[7.1]
def change
create_join_table :children, :items do |t|
t.index [:child_id, :item_id], unique: true
t.index [:item_id, :child_id], unique: true
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class BackfillPartnerChildRequestedItems < ActiveRecord::Migration[7.1]
def change
Partners::Child.unscoped.where.not(item_needed_diaperid: nil).each do |child|
child.requested_items << Item.find_by(id: child.item_needed_diaperid)
end
end
end
9 changes: 8 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.1].define(version: 2024_06_24_185108) do
ActiveRecord::Schema[7.1].define(version: 2024_07_04_214509) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

Expand Down Expand Up @@ -188,6 +188,13 @@
t.index ["family_id"], name: "index_children_on_family_id"
end

create_table "children_items", id: false, force: :cascade do |t|
t.bigint "child_id", null: false
t.bigint "item_id", null: false
t.index ["child_id", "item_id"], name: "index_children_items_on_child_id_and_item_id", unique: true
t.index ["item_id", "child_id"], name: "index_children_items_on_item_id_and_child_id", unique: true
end

create_table "counties", force: :cascade do |t|
t.string "name"
t.string "region"
Expand Down
6 changes: 4 additions & 2 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,8 @@ def random_record_for_org(org, klass)
)
end

requestable_items = PartnerFetchRequestableItemsService.new(partner_id: p.id).call.map(&:last)

families.each do |family|
Partners::AuthorizedFamilyMember.create!(
first_name: Faker::Name.first_name,
Expand All @@ -316,7 +318,7 @@ def random_record_for_org(org, klass)
comments: Faker::Lorem.paragraph,
active: Faker::Boolean.boolean,
archived: false,
item_needed_diaperid: p.organization.item_id_to_display_string_map.key(Partners::Child::CHILD_ITEMS.sample)
requested_item_ids: requestable_items.sample(rand(4))
)
end

Expand All @@ -334,7 +336,7 @@ def random_record_for_org(org, klass)
comments: Faker::Lorem.paragraph,
active: Faker::Boolean.boolean,
archived: false,
item_needed_diaperid: p.organization.item_id_to_display_string_map.key(Partners::Child::CHILD_ITEMS.sample)
requested_item_ids: requestable_items.sample(rand(4))
)
end
end
Expand Down
3 changes: 1 addition & 2 deletions spec/factories/partners/child.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
first_name { Faker::Name.first_name }
last_name { Faker::Name.last_name }
gender { Faker::Gender.binary_type }
# TODO: change when closing #4199
item_needed_diaperid { Item.all.sample&.id || create(:item, organization: family.partner.organization).id }
requested_item_ids { [create(:item, organization: family.partner.organization).id] }
end
end
1 change: 1 addition & 0 deletions spec/models/partners/child_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
describe 'associations' do
it { should belong_to(:family) }
it { should have_many(:child_item_requests).dependent(:destroy) }
it { should have_and_belong_to_many(:requested_items).class_name('Item') }
end

describe "#display_name" do
Expand Down
10 changes: 6 additions & 4 deletions spec/requests/partners/children_requests_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
let(:partner_user) { partner.primary_user }
let(:partner) { create(:partner) }
let(:family) { create(:partners_family, partner: partner) }
let(:item1) { create(:item, organization: partner.organization) }
let(:item2) { create(:item, organization: partner.organization) }
let!(:child1) do
create(:partners_child,
first_name: "John",
Expand All @@ -15,7 +17,7 @@
agency_child_id: "Agency McAgence",
health_insurance: "Private insurance",
comments: "Some comment",
item_needed_diaperid: nil,
requested_item_ids: nil,
active: true,
archived: false,
family: family)
Expand All @@ -31,9 +33,9 @@
agency_child_id: "Agency McAgence",
health_insurance: "Private insurance",
comments: "Some comment",
item_needed_diaperid: nil,
active: true,
archived: false,
requested_item_ids: [item1.id, item2.id],
family: family)
end

Expand All @@ -51,9 +53,9 @@
headers = {"Accept" => "text/csv", "Content-Type" => "text/csv"}
get partners_children_path, headers: headers
csv = <<~CSV
id,first_name,last_name,date_of_birth,gender,child_lives_with,race,agency_child_id,health_insurance,comments,created_at,updated_at,family_id,item_needed_diaperid,active,archived
id,first_name,last_name,date_of_birth,gender,child_lives_with,race,agency_child_id,health_insurance,comments,created_at,updated_at,family_id,requested_item_ids,active,archived
#{child1.id},John,Smith,2019-01-01,Male,"mother,grandfather",Other,Agency McAgence,Private insurance,Some comment,#{child1.created_at},#{child1.updated_at},#{family.id},"",true,false
#{child2.id},Jane,Smith,2018-01-01,Female,father,Hispanic,Agency McAgence,Private insurance,Some comment,#{child2.created_at},#{child2.updated_at},#{family.id},"",true,false
#{child2.id},Jane,Smith,2018-01-01,Female,father,Hispanic,Agency McAgence,Private insurance,Some comment,#{child2.created_at},#{child2.updated_at},#{family.id},"#{item1.id},#{item2.id}",true,false
CSV
expect(response.body).to eq(csv)
end
Expand Down
2 changes: 1 addition & 1 deletion spec/requests/partners/family_requests_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
# Set one child as deactivated and the other as active but
# without a item_needed_diaperid
children[0].update(active: false)
children[1].update(item_needed_diaperid: nil)
children[1].update(requested_item_ids: [])
end
subject { post partners_family_requests_path, params: params }

Expand Down
36 changes: 36 additions & 0 deletions spec/system/partners/children_system_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
RSpec.describe "Creating a parner child", type: :system, js: true do
let(:organization) { create(:organization) }
let(:partner) { FactoryBot.create(:partner, organization: organization) }
let(:partner_user) { partner.primary_user }
let(:family) { create(:partners_family, guardian_first_name: "Main", guardian_last_name: "Family", partner: partner) }

before do
partner.update(status: :approved)
login_as(partner_user)
create(:item, name: "Item 1", organization: organization)
create(:item, name: "Item 2", organization: organization)
end

describe "creating a child for a family" do
it "creates a child with correct info" do
visit new_partners_child_path(family_id: family.id)
fill_in "First Name", with: "Child First Name"
fill_in "Last Name", with: "Child Last Name"
select "Other", from: "Race"
fill_in "Agency Child ID", with: "01234"
fill_in "Comments", with: "Some Comment"

select2(page, "requestable-items-container", "Item 2")
select2(page, "requestable-items-container", "Item 1")

click_button "Create Child"

expect(page).to have_text("Child was successfully created.")
expect(page).to have_text("Child First Name")
expect(page).to have_text("Child Last Name")
expect(page).to have_text("01234")
expect(page).to have_text("Some Comment")
expect(page).to have_text("Item 1, Item 2")
end
end
end
Loading