Admin reports with accounts (#2092)
* Add a ReportFilter class * Add reports and targeted_reports relationships to Account * Use ReportFilter from admin/reports controller * Link to admin/reports filtered views from admin account show view * Add indexes to reports.account_id and reports.target_account_idth-downstream
parent
7958d32b64
commit
4b6be67e85
@ -0,0 +1,30 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class ReportFilter
|
||||
attr_reader :params
|
||||
|
||||
def initialize(params)
|
||||
@params = params
|
||||
end
|
||||
|
||||
def results
|
||||
scope = Report.unresolved
|
||||
params.each do |key, value|
|
||||
scope = scope.merge scope_for(key, value)
|
||||
end
|
||||
scope
|
||||
end
|
||||
|
||||
def scope_for(key, value)
|
||||
case key.to_sym
|
||||
when :resolved
|
||||
Report.resolved
|
||||
when :account_id
|
||||
Report.where(account_id: value)
|
||||
when :target_account_id
|
||||
Report.where(target_account_id: value)
|
||||
else
|
||||
raise "Unknown filter: #{key}"
|
||||
end
|
||||
end
|
||||
end
|
@ -0,0 +1,6 @@
|
||||
class AddIndexesToReportsForAccounts < ActiveRecord::Migration[5.0]
|
||||
def change
|
||||
add_index :reports, :account_id
|
||||
add_index :reports, :target_account_id
|
||||
end
|
||||
end
|
@ -0,0 +1,31 @@
|
||||
require 'rails_helper'
|
||||
|
||||
describe ReportFilter do
|
||||
describe 'with empty params' do
|
||||
it 'defaults to unresolved reports list' do
|
||||
filter = ReportFilter.new({})
|
||||
|
||||
expect(filter.results).to eq Report.unresolved
|
||||
end
|
||||
end
|
||||
|
||||
describe 'with invalid params' do
|
||||
it 'raises with key error' do
|
||||
filter = ReportFilter.new(wrong: true)
|
||||
|
||||
expect { filter.results }.to raise_error(/wrong/)
|
||||
end
|
||||
end
|
||||
|
||||
describe 'with valid params' do
|
||||
it 'combines filters on Report' do
|
||||
filter = ReportFilter.new(account_id: '123', resolved: true)
|
||||
|
||||
allow(Report).to receive(:where).and_return(Report.none)
|
||||
allow(Report).to receive(:resolved).and_return(Report.none)
|
||||
filter.results
|
||||
expect(Report).to have_received(:where).with(account_id: '123')
|
||||
expect(Report).to have_received(:resolved)
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in new issue