DEV: there is no need anymore to wrap export methods into enumerators (#22567)

After fbe0e4c we always pass a block into these methods.
So yield inside the export methods works and there is no need 
anymore to wrap them into enumerators.
This commit is contained in:
Andrei Prigorshnev 2023-08-17 22:09:58 +04:00 committed by GitHub
parent af30f9945a
commit 54092833b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 26 deletions

View File

@ -120,8 +120,6 @@ module Jobs
end end
def user_list_export def user_list_export
return enum_for(:user_list_export) unless block_given?
user_field_ids = UserField.pluck(:id) user_field_ids = UserField.pluck(:id)
condition = {} condition = {}
@ -148,8 +146,6 @@ module Jobs
end end
def staff_action_export def staff_action_export
return enum_for(:staff_action_export) unless block_given?
staff_action_data = staff_action_data =
if @current_user.admin? if @current_user.admin?
UserHistory.only_staff_actions UserHistory.only_staff_actions
@ -163,24 +159,18 @@ module Jobs
end end
def screened_email_export def screened_email_export
return enum_for(:screened_email_export) unless block_given?
ScreenedEmail.find_each(order: :desc) do |screened_email| ScreenedEmail.find_each(order: :desc) do |screened_email|
yield get_screened_email_fields(screened_email) yield get_screened_email_fields(screened_email)
end end
end end
def screened_ip_export def screened_ip_export
return enum_for(:screened_ip_export) unless block_given?
ScreenedIpAddress.find_each(order: :desc) do |screened_ip| ScreenedIpAddress.find_each(order: :desc) do |screened_ip|
yield get_screened_ip_fields(screened_ip) yield get_screened_ip_fields(screened_ip)
end end
end end
def screened_url_export def screened_url_export
return enum_for(:screened_url_export) unless block_given?
ScreenedUrl ScreenedUrl
.select( .select(
"domain, sum(match_count) as match_count, max(last_match_at) as last_match_at, min(created_at) as created_at", "domain, sum(match_count) as match_count, max(last_match_at) as last_match_at, min(created_at) as created_at",
@ -191,8 +181,6 @@ module Jobs
end end
def report_export def report_export
return enum_for(:report_export) unless block_given?
# If dates are invalid consider then `nil` # If dates are invalid consider then `nil`
if @extra[:start_date].is_a?(String) if @extra[:start_date].is_a?(String)
@extra[:start_date] = begin @extra[:start_date] = begin

View File

@ -81,9 +81,9 @@ RSpec.describe Jobs::ExportCsvFile do
it "works with single-column reports" do it "works with single-column reports" do
user.user_visits.create!(visited_at: "2010-01-01", posts_read: 42) user.user_visits.create!(visited_at: "2010-01-01", posts_read: 42)
Fabricate(:user).user_visits.create!(visited_at: "2010-01-03", posts_read: 420) Fabricate(:user).user_visits.create!(visited_at: "2010-01-03", posts_read: 420)
exporter.extra["name"] = "dau_by_mau" exporter.extra["name"] = "dau_by_mau"
report = exporter.report_export.to_a
report = export_report
expect(report.first).to contain_exactly("Day", "Percent") expect(report.first).to contain_exactly("Day", "Percent")
expect(report.second).to contain_exactly("2010-01-01", "100.0") expect(report.second).to contain_exactly("2010-01-01", "100.0")
@ -95,12 +95,13 @@ RSpec.describe Jobs::ExportCsvFile do
group = Fabricate(:group) group = Fabricate(:group)
user1 = Fabricate(:user) user1 = Fabricate(:user)
group_user = Fabricate(:group_user, group: group, user: user1) Fabricate(:group_user, group: group, user: user1)
user1.user_visits.create!(visited_at: "2010-01-03", posts_read: 420) user1.user_visits.create!(visited_at: "2010-01-03", posts_read: 420)
exporter.extra["name"] = "visits" exporter.extra["name"] = "visits"
exporter.extra["group"] = group.id exporter.extra["group"] = group.id
report = exporter.report_export.to_a
report = export_report
expect(report.length).to eq(2) expect(report.length).to eq(2)
expect(report.first).to contain_exactly("Day", "Count") expect(report.first).to contain_exactly("Day", "Count")
@ -110,9 +111,9 @@ RSpec.describe Jobs::ExportCsvFile do
it "works with single-column reports with default label" do it "works with single-column reports with default label" do
user.user_visits.create!(visited_at: "2010-01-01") user.user_visits.create!(visited_at: "2010-01-01")
Fabricate(:user).user_visits.create!(visited_at: "2010-01-03") Fabricate(:user).user_visits.create!(visited_at: "2010-01-03")
exporter.extra["name"] = "visits" exporter.extra["name"] = "visits"
report = exporter.report_export.to_a
report = export_report
expect(report.first).to contain_exactly("Day", "Count") expect(report.first).to contain_exactly("Day", "Count")
expect(report.second).to contain_exactly("2010-01-01", "1") expect(report.second).to contain_exactly("2010-01-01", "1")
@ -126,9 +127,9 @@ RSpec.describe Jobs::ExportCsvFile do
client_ip: "1.1.1.1", client_ip: "1.1.1.1",
created_at: "2010-01-01", created_at: "2010-01-01",
) )
exporter.extra["name"] = "staff_logins" exporter.extra["name"] = "staff_logins"
report = exporter.report_export.to_a
report = export_report
expect(report.first).to contain_exactly("User", "Location", "Login at") expect(report.first).to contain_exactly("User", "Location", "Login at")
expect(report.second).to contain_exactly(user.username, "Earth", "2010-01-01 00:00:00 UTC") expect(report.second).to contain_exactly(user.username, "Earth", "2010-01-01 00:00:00 UTC")
@ -139,7 +140,7 @@ RSpec.describe Jobs::ExportCsvFile do
exporter.extra["name"] = "top_referred_topics" exporter.extra["name"] = "top_referred_topics"
post1 = Fabricate(:post) post1 = Fabricate(:post)
post2 = Fabricate(:post) Fabricate(:post)
IncomingLink.add( IncomingLink.add(
host: "a.com", host: "a.com",
referer: "http://twitter.com", referer: "http://twitter.com",
@ -147,7 +148,7 @@ RSpec.describe Jobs::ExportCsvFile do
ip_address: "1.1.1.1", ip_address: "1.1.1.1",
) )
report = exporter.report_export.to_a report = export_report
expect(report.first).to contain_exactly("Topic", "Clicks") expect(report.first).to contain_exactly("Topic", "Clicks")
expect(report.second).to contain_exactly(post1.topic.id.to_s, "1") expect(report.second).to contain_exactly(post1.topic.id.to_s, "1")
@ -167,7 +168,8 @@ RSpec.describe Jobs::ExportCsvFile do
ApplicationRequest.create!(date: "2010-01-03", req_type: "page_view_crawler", count: 9) ApplicationRequest.create!(date: "2010-01-03", req_type: "page_view_crawler", count: 9)
exporter.extra["name"] = "consolidated_page_views" exporter.extra["name"] = "consolidated_page_views"
report = exporter.report_export.to_a
report = export_report
expect(report[0]).to contain_exactly("Day", "Logged in users", "Anonymous users", "Crawlers") expect(report[0]).to contain_exactly("Day", "Logged in users", "Anonymous users", "Crawlers")
expect(report[1]).to contain_exactly("2010-01-01", "1", "4", "7") expect(report[1]).to contain_exactly("2010-01-01", "1", "4", "7")
@ -193,15 +195,25 @@ RSpec.describe Jobs::ExportCsvFile do
exporter.extra["name"] = "posts" exporter.extra["name"] = "posts"
exporter.extra["category"] = category.id exporter.extra["category"] = category.id
report = exporter.report_export.to_a
report = export_report
expect(report[0]).to contain_exactly("Count", "Day") expect(report[0]).to contain_exactly("Count", "Day")
expect(report[1]).to contain_exactly("1", "2010-01-01") expect(report[1]).to contain_exactly("1", "2010-01-01")
exporter.extra["include_subcategories"] = true exporter.extra["include_subcategories"] = true
report = exporter.report_export.to_a
report = export_report
expect(report[0]).to contain_exactly("Count", "Day") expect(report[0]).to contain_exactly("Count", "Day")
expect(report[1]).to contain_exactly("2", "2010-01-01") expect(report[1]).to contain_exactly("2", "2010-01-01")
end end
def export_report
report = []
exporter.report_export { |entry| report << entry }
report
end
end end
let(:user_list_header) do let(:user_list_header) do
@ -244,7 +256,11 @@ RSpec.describe Jobs::ExportCsvFile do
] ]
end end
let(:user_list_export) { Jobs::ExportCsvFile.new.user_list_export } let(:user_list_export) do
exported_data = []
Jobs::ExportCsvFile.new.user_list_export { |entry| exported_data << entry }
exported_data
end
def to_hash(row) def to_hash(row)
Hash[*user_list_header.zip(row).flatten] Hash[*user_list_header.zip(row).flatten]