Commit f79b51ec authored by James Edwards-Jones's avatar James Edwards-Jones

Added more complicated columns to Issues CSV

Milestone and Labels columns needed ruby code to build instead of using `pluck` directly on the relation.
Instead it now calls a lambda for every value in a row.
This approach can be less efficient especially if data isn’t eager loaded.
parent e06037cf
...@@ -3,17 +3,28 @@ columns = { ...@@ -3,17 +3,28 @@ columns = {
'Title' => 'title', 'Title' => 'title',
'State' => 'state', 'State' => 'state',
'Description' => 'description', 'Description' => 'description',
'Author Id' => 'author_id', 'Author' => 'author_name',
'Assignee Id' => 'assignee_id', 'Assignee' => 'assignee_name',
'Confidential' => 'confidential',
'Due Date' => 'due_date', 'Due Date' => 'due_date',
'Created At' => 'created_at', 'Created At' => 'created_at',
'Updated At' => 'updated_at' 'Updated At' => 'updated_at',
'Milestone' => -> (issue) { issue.milestone&.title },
'Labels' => -> (issue) { issue.label_names.join(',').presence },
} }
CSV.generate do |csv| CSV.generate do |csv|
csv << columns.keys csv << columns.keys
@issues.pluck(*columns.values).each do |row| @issues.each do |issue|
row = columns.values.map do |attribute|
if attribute.respond_to?(:call)
attribute.call(issue)
else
issue.send(attribute)
end
end
csv << row csv << row
end end
end end
...@@ -5,10 +5,7 @@ describe 'Issues csv', feature: true do ...@@ -5,10 +5,7 @@ describe 'Issues csv', feature: true do
let(:project) { create(:project, :public) } let(:project) { create(:project, :public) }
let!(:issue) { create(:issue, project: project) } let!(:issue) { create(:issue, project: project) }
before do before { login_as(user) }
login_as(user)
visit namespace_project_issues_path(project.namespace, project, format: :csv)
end
it "downloads from a project's issue index" do it "downloads from a project's issue index" do
visit namespace_project_issues_path(project.namespace, project) visit namespace_project_issues_path(project.namespace, project)
...@@ -16,11 +13,49 @@ describe 'Issues csv', feature: true do ...@@ -16,11 +13,49 @@ describe 'Issues csv', feature: true do
expect(page.response_headers['Content-Type']).to include('csv') expect(page.response_headers['Content-Type']).to include('csv')
end end
it 'includes title' do context 'includes' do
let(:label1) { create(:label, project: project, title: 'Feature') }
let(:label2) { create(:label, project: project, title: 'labels') }
let(:milestone) { create(:milestone, title: "v1.0", project: project) }
before do
issue.update!(milestone: milestone, assignee: user, description: 'Issue with details', labels: [label1, label2])
visit namespace_project_issues_path(project.namespace, project, format: :csv)
end
specify 'title' do
expect(csv[0]['Title']).to eq issue.title expect(csv[0]['Title']).to eq issue.title
end end
it 'includes description' do specify 'description' do
expect(csv[0]['Description']).to eq issue.description expect(csv[0]['Description']).to eq issue.description
end end
specify 'author name' do
expect(csv[0]['Author']).to eq issue.author_name
end
specify 'assignee name' do
expect(csv[0]['Assignee']).to eq issue.assignee_name
end
specify 'confidential' do
expect(csv[0]['Confidential']).to eq 'false'
end
specify 'milestone' do
expect(csv[0]['Milestone']).to eq issue.milestone.title
end
specify 'labels' do
expect(csv[0]['Labels']).to eq 'Feature,labels'
end
end
context 'with minimal details' do
it 'renders labels as nil' do
visit namespace_project_issues_path(project.namespace, project, format: :csv)
expect(csv[0]['Labels']).to eq nil
end
end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment