Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: removes scoping
Browse files Browse the repository at this point in the history
After talking with PMs alphabetical scoping doesn't make sense.

Topics should be set in the order they are on organization, maybe
in the future they should even be swappable in order.
elasticspoon committed Mar 8, 2024
1 parent 3358747 commit 99151a8
Showing 7 changed files with 52 additions and 35 deletions.
1 change: 0 additions & 1 deletion app/models/contact_topic.rb
Original file line number Diff line number Diff line change
@@ -10,7 +10,6 @@ class ContactTopic < ApplicationRecord
validates :details, presence: true

scope :active, -> { where(active: true, soft_delete: false) }
default_scope { order(:question) }

class << self
def generate_for_org!(casa_org)
2 changes: 0 additions & 2 deletions app/models/contact_topic_answer.rb
Original file line number Diff line number Diff line change
@@ -3,8 +3,6 @@ class ContactTopicAnswer < ApplicationRecord
belongs_to :contact_topic

validates :selected, inclusion: [true, false]

default_scope { joins(:contact_topic).order("contact_topics.question") }
end

# == Schema Information
11 changes: 11 additions & 0 deletions spec/factories/case_contacts.rb
Original file line number Diff line number Diff line change
@@ -61,5 +61,16 @@
casa_case { nil }
draft_case_ids { [1] }
end

trait :with_org_topics do
after(:create) do |case_contact, _|
return if case_contact.casa_case.nil?

casa_org = case_contact.casa_case.casa_org
casa_org.contact_topics.active.each do |contact_topic|
case_contact.contact_topic_answers << build(:contact_topic_answer, contact_topic: contact_topic)
end
end
end
end
end
16 changes: 9 additions & 7 deletions spec/models/case_contact_spec.rb
Original file line number Diff line number Diff line change
@@ -142,13 +142,15 @@
end

describe "#create_with_answers" do
let(:inactive_list) { create_list(:contact_topic, 2, active: false) }
let(:active_list) { create_list(:contact_topic, 2, active: true) }
let(:contact_topics) { [*active_list, *inactive_list] }
let(:contact_topics) { [
build(:contact_topic, active: true, soft_delete: false),
build(:contact_topic, active: false, soft_delete: false),
build(:contact_topic, active: true, soft_delete: true),
build(:contact_topic, active: false, soft_delete: true),
] }
let(:org) { create(:casa_org, contact_topics:) }
let(:admin) { create(:casa_admin, casa_org: org) }
let(:casa_case) { create(:casa_case, casa_org: org) }
# let(:case_contact) { create(:case_contact, casa_case:) }

context "when creation is successful" do
it "create a case_contact" do
@@ -158,16 +160,16 @@
}.to change(CaseContact, :count).from(0).to(1)
end

it "creates contact_topic_answers" do
it "creates only active and non-deleted contact_topic_answers" do
org
expect {
CaseContact.create_with_answers(org, creator: admin)
}.to change(ContactTopicAnswer, :count).from(0).to(2)
}.to change(ContactTopicAnswer, :count).from(0).to(1)

case_contact = CaseContact.last
topics = case_contact.contact_topic_answers.map(&:contact_topic)

expect(topics).to match_array(active_list)
expect(topics).to include(contact_topics.first)
end
end

20 changes: 19 additions & 1 deletion spec/requests/case_contacts/form_spec.rb
Original file line number Diff line number Diff line change
@@ -58,7 +58,25 @@
end
end

# TODO: Test that contact topics are shown

context "when the org has topics assigned" do
let(:contact_topics) { [
build(:contact_topic, active: true, soft_delete: false),
build(:contact_topic, active: false, soft_delete: false),
build(:contact_topic, active: true, soft_delete: true),
build(:contact_topic, active: false, soft_delete: true),
] }
let(:organization) { create(:casa_org, contact_topics:) }
let!(:case_contact) { create(:case_contact, :details_status, :with_org_topics, casa_case: casa_case) }

it "shows contact topics" do
page = request.parsed_body.to_html
expect(page).to include(contact_topics[0].question)
expect(page).to_not include(contact_topics[1].question)
expect(page).to_not include(contact_topics[2].question)
expect(page).to_not include(contact_topics[3].question)
end
end
end
end

18 changes: 13 additions & 5 deletions spec/requests/case_contacts_spec.rb
Original file line number Diff line number Diff line change
@@ -53,13 +53,21 @@
end

context "when current org has contact topics" do
let(:contact_topics) { build_list(:contact_topic, 3) }
let(:contact_topics) { [
build(:contact_topic, active: true, soft_delete: false),
build(:contact_topic, active: false, soft_delete: false),
build(:contact_topic, active: true, soft_delete: true),
build(:contact_topic, active: false, soft_delete: true),
] }
let(:organization) { create(:casa_org, contact_topics:) }

it "should set empty contact topic answers for new case contact to org topics" do
expect { request }.to change(ContactTopicAnswer, :count).by(3)
expect(CaseContact.last.contact_topic_answers.count).to eq(3)
expect(CaseContact.last.contact_topic_answers.pluck(:value)).to be_all(nil)
it "should set empty contact topic answers for new case contact to active/non-softdelet org topics" do
expect { request }.to change(ContactTopicAnswer, :count).by(1)

got = CaseContact.last.contact_topic_answers.first.contact_topic.question
expect(got).to eq(contact_topics[0].question)

expect(CaseContact.last.contact_topic_answers.first.value).to be_nil
end
end
end
19 changes: 0 additions & 19 deletions spec/system/casa_org/edit_spec.rb
Original file line number Diff line number Diff line change
@@ -75,25 +75,6 @@
message = find("#casa_org_twilio_phone_number").native.attribute("validationMessage")
expect(message).to eq "Please fill out this field."
end

it "deleting a contact topic soft deletes the record and does not show it", js: true do
contact_topics = build_list(:contact_topic, 2).sort_by(&:question)
organization = create(:casa_org, contact_topics:)
admin = create(:casa_admin, casa_org_id: organization.id)

sign_in admin
visit edit_casa_org_path(organization)

expect(page).to have_text(contact_topics.first.question)
expect(page).to have_text(contact_topics.last.question)

find_all("#contact-topics .dropdown-toggle").last.click
within("#contact-topics") { click_link "Delete" }
click_link "Delete Court Report Topic"

expect(page).to have_text(contact_topics.first.question)
expect(page).to_not have_text(contact_topics.last.question)
end
end

def stub_twilio

0 comments on commit 99151a8

Please sign in to comment.