Skip to content

Commit

Permalink
Restore framework flags to CSV downloads (#2467)
Browse files Browse the repository at this point in the history
* Add index framework_flags#title

* Add all framework flags to the CSV download
  • Loading branch information
tobyprivett authored Jan 10, 2025
1 parent febf6c9 commit d467e5c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 48 deletions.
17 changes: 9 additions & 8 deletions app/services/moves/exporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

module Moves
class Exporter
attr_reader :alert_columns, :moves
attr_reader :moves

STATIC_HEADINGS = [
'Status',
Expand Down Expand Up @@ -65,16 +65,15 @@ class Exporter

def initialize(moves)
@moves = moves
@alert_columns = ENV.fetch('FEATURE_FLAG_CSV_ALERT_COLUMNS', 'false') == 'true'
end

def call
Tempfile.new('export', Rails.root.join('tmp')).tap do |file|
csv = CSV.new(file)
headings = STATIC_HEADINGS
headings += flags_by_section if alert_columns
headings += flags_by_section
csv << headings
moves.includes(:cancellation_events).find_each do |move|
moves.includes(:cancellation_events, :person_escort_record, :youth_risk_assessment).find_each do |move|
csv << attributes_row(move)
end
file.flush
Expand Down Expand Up @@ -132,10 +131,11 @@ def attributes_row(move)
move.supplier&.name,
]

if alert_columns
move_flags = move.person_escort_record&.framework_flags&.pluck(:title)
row += flags_by_section.map { move_flags&.include?(_1) ? 'TRUE' : '' }
end
move_flags = []
move_flags += Array(move.person_escort_record&.framework_flags&.pluck(:title))
move_flags += Array(move.youth_risk_assessment&.framework_flags&.pluck(:title))

row += flags_by_section.map { (move_flags&.include?(_1) ? 'TRUE' : '') }

row.flatten # Expand answer_details column pairs into individual columns
end
Expand All @@ -154,6 +154,7 @@ def answer_details(answers, key)
def flags_by_section
@flags_by_section ||= FrameworkFlag
.select('DISTINCT ON (title) *')
.includes(:framework_question)
.joins(:framework_question)
.sort { |a, b|
a.framework_question.section <=> b.framework_question.section
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddIndexTitleToFrameworkFlags < ActiveRecord::Migration[8.0]
def change
add_index :framework_flags, :title
end
end
3 changes: 2 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[8.0].define(version: 2024_11_25_114943) do
ActiveRecord::Schema[8.0].define(version: 2025_01_09_102311) do
# These are extensions that must be enabled in order to support this database
enable_extension "citext"
enable_extension "pg_catalog.plpgsql"
Expand Down Expand Up @@ -161,6 +161,7 @@
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["framework_question_id"], name: "index_framework_flags_on_framework_question_id"
t.index ["title"], name: "index_framework_flags_on_title"
end

create_table "framework_flags_responses", id: false, force: :cascade do |t|
Expand Down
60 changes: 21 additions & 39 deletions spec/services/moves/exporter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@
expect(row).to include("Yikes!\n\nBam!")
end

context 'with PER' do
context 'with PER and YRA' do
let(:from_location) { create(:location, :stc) }
let(:framework_questions) do
[
create(:framework_question, section: 'risk-information'),
Expand All @@ -125,55 +126,36 @@
person_escort_record = create(:person_escort_record)
create(:string_response, framework_question: framework_questions.first, responded: true, assessmentable: person_escort_record)
create(:string_response, framework_question: framework_questions.second, responded: true, framework_flags: [flag], assessmentable: person_escort_record)
create(:string_response, framework_question: framework_questions.third, responded: true, framework_flags: [flag2], assessmentable: person_escort_record)

person_escort_record
end

before { move.person_escort_record = person_escort_record }
let(:youth_risk_assessment) do
youth_risk_assessment = create(:youth_risk_assessment)
create(:string_response, framework_question: framework_questions.third, responded: true, framework_flags: [flag2], assessmentable: youth_risk_assessment)

context 'when feature flag enabled' do
around do |example|
ClimateControl.modify(FEATURE_FLAG_CSV_ALERT_COLUMNS: 'true') do
example.run
end
end

it 'includes correct header names' do
expect(header).to eq(described_class::STATIC_HEADINGS + ['Flag 2', 'Flag 1'])
end

it 'has correct number of header columns' do
expect(header.count).to eq(56)
end

it 'has correct number of body columns' do
expect(row.count).to eq(56)
end
youth_risk_assessment
end

it 'has the correct rows' do
expect(row.last(2)).to eq(%w[TRUE TRUE])
end
before do
move.person_escort_record = person_escort_record
move.youth_risk_assessment = youth_risk_assessment
end

context 'when feature flag disabled' do
around do |example|
ClimateControl.modify(FEATURE_FLAG_CSV_ALERT_COLUMNS: 'false') do
example.run
end
end
it 'includes correct header names' do
expect(header).to eq(described_class::STATIC_HEADINGS + ['Flag 2', 'Flag 1'])
end

it 'includes correct header names' do
expect(header).to eq(described_class::STATIC_HEADINGS)
end
it 'has correct number of header columns' do
expect(header.count).to eq(56)
end

it 'has correct number of header columns' do
expect(header.count).to eq(54)
end
it 'has correct number of body columns' do
expect(row.count).to eq(56)
end

it 'has correct number of body columns' do
expect(row.count).to eq(54)
end
it 'has the correct rows' do
expect(row.last(2)).to eq(%w[TRUE TRUE])
end
end
end

0 comments on commit d467e5c

Please sign in to comment.