-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2746 from bitzesty/csv-report
Adds new CSV report for user data
- Loading branch information
Showing
15 changed files
with
222 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class Admin::UsersReportsController < Admin::BaseController | ||
def assessors_judges_admins_data | ||
authorize :reports, :show? | ||
|
||
@report = Reports::Admin::AssessorJudgeAdminDataReport.new() | ||
|
||
respond_to do |format| | ||
format.csv do | ||
send_data @report.as_csv, filename: @report.csv_filename | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
app/models/reports/admin/assessor_judge_admin_data_report.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
class Reports::Admin::AssessorJudgeAdminDataReport | ||
include Reports::CsvHelper | ||
|
||
MAPPING = [ | ||
{ | ||
label: "ID", | ||
method: :id | ||
}, { | ||
label: "First name", | ||
method: :first_name | ||
}, { | ||
label: "Last name", | ||
method: :last_name | ||
}, { | ||
label: "Company", | ||
method: :company | ||
}, { | ||
label: "Email", | ||
method: :email | ||
}, { | ||
label: "Telephone", | ||
method: :telephone_number | ||
}, { | ||
label: "User type", | ||
method: :user_type | ||
}, { | ||
label: "User creation date", | ||
method: :created_at | ||
}, { | ||
label: "Last sign in date", | ||
method: :last_sign_in_at | ||
}, { | ||
label: "Awards assigned", | ||
method: :awards_assigned | ||
} | ||
] | ||
|
||
def as_csv | ||
CSV.generate(encoding: "UTF-8", force_quotes: true) do |csv| | ||
csv << headers | ||
|
||
Admin.find_each do |user| | ||
u = Reports::User.new(user) | ||
|
||
csv << mapping.map do |m| | ||
sanitize_string( | ||
u.call_method(m[:method]) | ||
) | ||
end | ||
end | ||
|
||
Assessor.find_each do |user| | ||
u = Reports::User.new(user) | ||
|
||
csv << mapping.map do |m| | ||
sanitize_string( | ||
u.call_method(m[:method]) | ||
) | ||
end | ||
end | ||
|
||
Judge.find_each do |user| | ||
u = Reports::User.new(user) | ||
|
||
csv << mapping.map do |m| | ||
sanitize_string( | ||
u.call_method(m[:method]) | ||
) | ||
end | ||
end | ||
end | ||
end | ||
|
||
def csv_filename | ||
"aassessors_judges_admins_data.csv" | ||
end | ||
|
||
private | ||
|
||
def mapping | ||
MAPPING | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
class Reports::User | ||
|
||
def initialize(user) | ||
@user = user | ||
end | ||
|
||
def call_method(methodname) | ||
return "not implemented" if methodname.blank? | ||
|
||
if respond_to?(methodname, true) | ||
send(methodname) | ||
elsif @user.respond_to?(methodname) | ||
@user.send(methodname) | ||
else | ||
"N/A" | ||
end | ||
end | ||
|
||
private | ||
|
||
def user_type | ||
@user.class.name | ||
end | ||
|
||
def awards_assigned | ||
if @user.is_a?(Assessor) || @user.is_a?(Judge) | ||
@user.roles.reject{|e| e=~ /promotion/}.join(", ") | ||
else | ||
"N/A" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
spec/models/reports/admin/assessor_judge_admin_data_report_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
require "rails_helper" | ||
|
||
describe Reports::Admin::AssessorJudgeAdminDataReport do | ||
let! (:admin) { create(:admin) } | ||
let! (:assessor) { create(:assessor, :lead_for_trade) } | ||
let! (:judge) { create(:judge, :innovation) } | ||
|
||
let(:output) { described_class.new().as_csv } | ||
it "creates the output with relevant data for Admins" do | ||
expect(output).to include(admin.id.to_s) | ||
expect(output).to include(admin.first_name) | ||
expect(output).to include(admin.last_name) | ||
expect(output).to include(admin.email) | ||
expect(output).to include("Admin") | ||
expect(output).to include(admin.created_at.to_s) | ||
expect(output).to include(admin.last_sign_in_at.to_s) | ||
end | ||
|
||
it "creates the output with relevant data for Assessors" do | ||
expect(output).to include(assessor.id.to_s) | ||
expect(output).to include(assessor.first_name) | ||
expect(output).to include(assessor.last_name) | ||
expect(output).to include(assessor.email) | ||
expect(output).to include(assessor.telephone_number.to_s) | ||
expect(output).to include(assessor.company.to_s) | ||
expect(output).to include("Assessor") | ||
expect(output).to include(assessor.created_at.to_s) | ||
expect(output).to include(assessor.last_sign_in_at.to_s) | ||
expect(output).to include("trade") | ||
end | ||
|
||
it "creates the output with relevant data for Judges" do | ||
expect(output).to include(judge.id.to_s) | ||
expect(output).to include(judge.first_name) | ||
expect(output).to include(judge.last_name) | ||
expect(output).to include(judge.email) | ||
expect(output).to include("Judge") | ||
expect(output).to include(judge.created_at.to_s) | ||
expect(output).to include(judge.last_sign_in_at.to_s) | ||
expect(output).to include("innovation") | ||
end | ||
end |