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

Moved Issue CSV label lookup from view to model

parent 431d4b77
......@@ -37,6 +37,8 @@ class Projects::IssuesController < Projects::ApplicationController
if params[:label_name].present?
@labels = LabelsFinder.new(current_user, project_id: @project.id, title: params[:label_name]).execute
elsif request.format.csv?
@labels = @issues.labels_hash
end
@users = []
......
......@@ -178,6 +178,13 @@ module Issuable
end
end
def labels_hash
eager_load(:labels).pluck(:id, 'labels.title').inject(Hash.new([])) do |memo, (issue_id, label)|
memo[issue_id] += [label]
memo
end
end
# Includes table keys in group by clause when sorting
# preventing errors in postgres
#
......
labels = @issues.eager_load(:labels).
pluck(:id, 'labels.title').
inject(Hash.new([])) do |memo, (issue_id, label)|
memo[issue_id] += [label]
memo
end
columns = {
'Issue ID' => 'iid',
'Title' => 'title',
......@@ -17,7 +10,7 @@ columns = {
'Created At (UTC)' => -> (issue) { issue.created_at&.to_s(:csv) },
'Updated At (UTC)' => -> (issue) { issue.updated_at&.to_s(:csv) },
'Milestone' => -> (issue) { issue.milestone&.title },
'Labels' => -> (issue) { labels[issue.id].sort.join(',').presence }
'Labels' => -> (issue) { @labels[issue.id].sort.join(',').presence }
}
CsvBuilder.new(@issues.includes(:author, :assignee), columns).render
......@@ -331,6 +331,16 @@ describe Issue, "Issuable" do
end
end
describe '.labels_hash' do
let(:feature_label) { create(:label, title: 'Feature') }
let!(:issues) { create_list(:labeled_issue, 3, labels: [feature_label]) }
it 'maps issue ids to labels titles' do
issue_id = issues.first.id
expect(Issue.labels_hash[issue_id]).to eq ['Feature']
end
end
describe '#user_notes_count' do
let(:project) { create(:empty_project) }
let(:issue1) { create(:issue, project: project) }
......
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